Add a rule builder screen with a live glob preview
Writing a glob by hand meant guessing what it would catch, then running retag to find out. The new screen, on 4, puts the glob, account and tag fields on the left and every still-untagged description on the right, sorted alphabetically and grouped by description with an occurrence count. Matches are marked as the glob is typed, along with a count, so the effect of a rule is visible before it is written. Enter appends it to rules.toml via config.AppendRule, reloads the engine from disk and retags, so the rows it caught leave the list immediately. Rules are appended rather than prepended, keeping the precedence of anything already in the file. Since the preview only lists transactions no existing rule has tagged, it reflects that precedence for free. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
@@ -66,6 +67,76 @@ func LoadRules(root string) (*Rules, error) {
|
||||
return &r, nil
|
||||
}
|
||||
|
||||
// AppendRule adds a rule to the end of rules.toml, creating the file if it is
|
||||
// not there yet. Appending rather than inserting means an existing rule always
|
||||
// keeps precedence, since the first match wins.
|
||||
//
|
||||
// The file is rewritten through a temporary file so a failure part-way cannot
|
||||
// leave the user with a truncated config.
|
||||
func AppendRule(root string, r Rule) error {
|
||||
if r.Match == "" && r.Counterparty == "" && r.Type == "" {
|
||||
return fmt.Errorf("a rule needs a match, counterparty or type pattern")
|
||||
}
|
||||
if r.Tag == "" && !r.Transfer {
|
||||
return fmt.Errorf("a rule needs a tag or transfer = true")
|
||||
}
|
||||
|
||||
path := filepath.Join(root, RulesFile)
|
||||
existing, err := os.ReadFile(path)
|
||||
if err != nil && !os.IsNotExist(err) {
|
||||
return fmt.Errorf("read %s: %w", path, err)
|
||||
}
|
||||
|
||||
var b strings.Builder
|
||||
b.Write(existing)
|
||||
if len(existing) > 0 && !strings.HasSuffix(string(existing), "\n") {
|
||||
b.WriteString("\n")
|
||||
}
|
||||
b.WriteString("\n")
|
||||
b.WriteString(formatRule(r))
|
||||
|
||||
tmp, err := os.CreateTemp(root, ".rules-*.toml")
|
||||
if err != nil {
|
||||
return fmt.Errorf("write %s: %w", path, err)
|
||||
}
|
||||
defer os.Remove(tmp.Name())
|
||||
|
||||
if _, err := tmp.WriteString(b.String()); err != nil {
|
||||
tmp.Close()
|
||||
return fmt.Errorf("write %s: %w", path, err)
|
||||
}
|
||||
if err := tmp.Close(); err != nil {
|
||||
return fmt.Errorf("write %s: %w", path, err)
|
||||
}
|
||||
if err := os.Chmod(tmp.Name(), 0o644); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.Rename(tmp.Name(), path); err != nil {
|
||||
return fmt.Errorf("replace %s: %w", path, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// formatRule renders a rule as a TOML table, omitting empty fields.
|
||||
func formatRule(r Rule) string {
|
||||
var b strings.Builder
|
||||
b.WriteString("[[rule]]\n")
|
||||
write := func(key, value string) {
|
||||
if value != "" {
|
||||
fmt.Fprintf(&b, "%-12s = %s\n", key, strconv.Quote(value))
|
||||
}
|
||||
}
|
||||
write("match", r.Match)
|
||||
write("counterparty", r.Counterparty)
|
||||
write("type", r.Type)
|
||||
write("account", r.Account)
|
||||
write("tag", r.Tag)
|
||||
if r.Transfer {
|
||||
b.WriteString("transfer = true\n")
|
||||
}
|
||||
return b.String()
|
||||
}
|
||||
|
||||
// Column locates one field in a CSV row.
|
||||
type Column struct {
|
||||
Col int `toml:"col"`
|
||||
|
||||
@@ -114,6 +114,87 @@ func TestResolveRootPrecedence(t *testing.T) {
|
||||
})
|
||||
}
|
||||
|
||||
func TestAppendRule(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
existing := "[[rule]]\ntag = \"salary\"\nmatch = \"*PAYROLL*\"\n"
|
||||
if err := os.WriteFile(filepath.Join(root, RulesFile), []byte(existing), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if err := AppendRule(root, Rule{Match: "*LIDL*", Tag: "groceries", Account: "checking"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
loaded, err := LoadRules(root)
|
||||
if err != nil {
|
||||
t.Fatalf("the file no longer parses after appending: %v", err)
|
||||
}
|
||||
if len(loaded.Rule) != 2 {
|
||||
t.Fatalf("got %d rules, want 2: %+v", len(loaded.Rule), loaded.Rule)
|
||||
}
|
||||
|
||||
// Appending, not prepending: the existing rule keeps precedence.
|
||||
if loaded.Rule[0].Tag != "salary" {
|
||||
t.Errorf("first rule = %+v, want the pre-existing salary rule", loaded.Rule[0])
|
||||
}
|
||||
added := loaded.Rule[1]
|
||||
if added.Match != "*LIDL*" || added.Tag != "groceries" || added.Account != "checking" {
|
||||
t.Errorf("appended rule = %+v", added)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendRuleCreatesFile(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := AppendRule(root, Rule{Match: "*LIDL*", Tag: "groceries"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := LoadRules(root)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(loaded.Rule) != 1 || loaded.Rule[0].Match != "*LIDL*" {
|
||||
t.Errorf("rules = %+v, want the one appended rule", loaded.Rule)
|
||||
}
|
||||
// An omitted account must not be written as an empty string.
|
||||
if loaded.Rule[0].Account != "" {
|
||||
t.Errorf("account = %q, want empty", loaded.Rule[0].Account)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAppendRuleRejectsIncomplete(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
cases := map[string]Rule{
|
||||
"no pattern": {Tag: "groceries"},
|
||||
"no verdict": {Match: "*LIDL*"},
|
||||
}
|
||||
for name, r := range cases {
|
||||
if err := AppendRule(root, r); err == nil {
|
||||
t.Errorf("%s: expected an error", name)
|
||||
}
|
||||
}
|
||||
if _, err := os.Stat(filepath.Join(root, RulesFile)); !os.IsNotExist(err) {
|
||||
t.Error("a rejected rule must not create the file")
|
||||
}
|
||||
}
|
||||
|
||||
// Globs and tags containing quotes must survive the round trip.
|
||||
func TestAppendRuleQuotesValues(t *testing.T) {
|
||||
root := t.TempDir()
|
||||
if err := AppendRule(root, Rule{Match: `*"ODD" PAYEE*`, Tag: "misc", Transfer: true}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
loaded, err := LoadRules(root)
|
||||
if err != nil {
|
||||
t.Fatalf("quoted values broke the file: %v", err)
|
||||
}
|
||||
if loaded.Rule[0].Match != `*"ODD" PAYEE*` {
|
||||
t.Errorf("match = %q, want the quotes preserved", loaded.Rule[0].Match)
|
||||
}
|
||||
if !loaded.Rule[0].Transfer {
|
||||
t.Error("transfer flag was not written")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserConfigPathFollowsXDG(t *testing.T) {
|
||||
t.Setenv("XDG_CONFIG_HOME", "/custom/config")
|
||||
path, err := UserConfigPath()
|
||||
|
||||
Reference in New Issue
Block a user