From dfd6bc30afefba57a34aa2d3d2e384701c3cf28e Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 18 Apr 2026 08:14:23 +0300 Subject: feat: add --kdbx-path CLI flag to override KeePass database path at runtime Allows specifying the .kdbx file path without editing the config file: foostore --backend keepass --kdbx-path /path/to/db.kdbx ls The flag overrides cfg.KDBXPath for the process lifetime. Implemented by extracting a shared parseFlagValue helper used by both parseBackendFlag and the new parseKDBXPathFlag. Help text updated to document both global flags. Table-driven tests added for parseKDBXPathFlag (mirrors TestParseBackendFlag). Co-Authored-By: Claude Sonnet 4.6 --- internal/cli/cli_test.go | 60 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) (limited to 'internal/cli/cli_test.go') diff --git a/internal/cli/cli_test.go b/internal/cli/cli_test.go index 3cf9503..1661283 100644 --- a/internal/cli/cli_test.go +++ b/internal/cli/cli_test.go @@ -802,6 +802,66 @@ func TestParseBackendFlag(t *testing.T) { } } +// ---- parseKDBXPathFlag ------------------------------------------------------ + +// TestParseKDBXPathFlag covers the --kdbx-path flag extraction. +func TestParseKDBXPathFlag(t *testing.T) { + cases := []struct { + name string + argv []string + wantPath string + wantArgv []string + }{ + { + name: "no flag", + argv: []string{"ls"}, + wantPath: "", + wantArgv: []string{"ls"}, + }, + { + name: "flag at start", + argv: []string{"--kdbx-path", "/tmp/db.kdbx", "ls"}, + wantPath: "/tmp/db.kdbx", + wantArgv: []string{"ls"}, + }, + { + name: "flag at end", + argv: []string{"cat", "foo", "--kdbx-path", "/home/user/db.kdbx"}, + wantPath: "/home/user/db.kdbx", + wantArgv: []string{"cat", "foo"}, + }, + { + name: "flag alone", + argv: []string{"--kdbx-path", "/tmp/db.kdbx"}, + wantPath: "/tmp/db.kdbx", + wantArgv: []string{}, + }, + { + name: "flag without value (treated as no flag)", + argv: []string{"--kdbx-path"}, + wantPath: "", + wantArgv: []string{"--kdbx-path"}, + }, + } + + for _, tc := range cases { + t.Run(tc.name, func(t *testing.T) { + gotPath, gotArgv := parseKDBXPathFlag(tc.argv) + if gotPath != tc.wantPath { + t.Errorf("path = %q; want %q", gotPath, tc.wantPath) + } + if len(gotArgv) != len(tc.wantArgv) { + t.Fatalf("argv len = %d; want %d (%v vs %v)", len(gotArgv), len(tc.wantArgv), gotArgv, tc.wantArgv) + } + for i := range gotArgv { + if gotArgv[i] != tc.wantArgv[i] { + t.Errorf("argv[%d] = %q; want %q", i, gotArgv[i], tc.wantArgv[i]) + } + } + }) + } +} + // ---- resolveBackend --------------------------------------------------------- // TestResolveBackend covers the flag-over-config priority logic. -- cgit v1.2.3