diff options
| author | Paul Buetow <paul@buetow.org> | 2026-04-18 08:14:23 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2026-04-18 08:14:23 +0300 |
| commit | dfd6bc30afefba57a34aa2d3d2e384701c3cf28e (patch) | |
| tree | d5ca36d41868dc53d5ae0bd84d4d2dfd945a08c5 /internal/cli/cli_test.go | |
| parent | e9ba8589491fcaae475627f0dd613b4ba21ee442 (diff) | |
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 <noreply@anthropic.com>
Diffstat (limited to 'internal/cli/cli_test.go')
| -rw-r--r-- | internal/cli/cli_test.go | 60 |
1 files changed, 60 insertions, 0 deletions
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. |
