Skip to content

Commit 78ccd40

Browse files
committed
feat(key): add command
1 parent 67ad350 commit 78ccd40

File tree

3 files changed

+75
-1
lines changed

3 files changed

+75
-1
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,13 +256,17 @@ numbers = [1, 1, 2, 3, 5]
256256
EOF
257257
```
258258

259-
You can then retrieve individual values from the TOML entry using the `-k`/`--key` option flag with the commands `show`, `clip`, and `pick`.
259+
You can then retrieve individual values from the TOML entry using the `-k`/`--key` option flag with the commands `show`, `clip`, and `pick`, or with the `key` command.
260260

261261
```shell
262262
# Show the user from the TOML entry.
263263
pago show -k user services/my-api
264264
# => jdoe
265265

266+
# The `key` command is a shortcut for `show --key`.
267+
pago key services/my-api user
268+
# => jdoe
269+
266270
# Show an array.
267271
pago show -k numbers services/my-api
268272
# => [1, 1, 2, 3, 5]

cmd/pago/main.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ type CLI struct {
6464
Generate GenerateCmd `cmd:"" aliases:"g,gen" help:"Generate and print password"`
6565
Info InfoCmd `cmd:"" hidden:"" help:"Show information"`
6666
Init InitCmd `cmd:"" help:"Create a new password store"`
67+
Key KeyCmd `cmd:"" aliases:"k" help:"Show a key from a TOML entry. A shortcut for \"show --key\"."`
6768
Pick PickCmd `cmd:"" aliases:"p" help:"Show password entry picked with a fuzzy finder. A shortcut for \"show --pick\"."`
6869
Rekey RekeyCmd `cmd:"" help:"Reencrypt all password entries with the recipients file"`
6970
Rename RenameCmd `cmd:"" aliases:"mv,r" help:"Rename or move a password entry"`
@@ -748,6 +749,20 @@ func (cmd *InitCmd) Run(config *Config) error {
748749
return nil
749750
}
750751

752+
type KeyCmd struct {
753+
Name string `arg:"" help:"Name of the password entry"`
754+
Key string `arg:"" help:"Key to retrieve from the TOML entry"`
755+
}
756+
757+
func (cmd *KeyCmd) Run(config *Config) error {
758+
if config.Verbose {
759+
printRepr(cmd)
760+
}
761+
762+
showCmd := &ShowCmd{Name: cmd.Name, Key: cmd.Key}
763+
return showCmd.Run(config)
764+
}
765+
751766
type PickCmd struct {
752767
Name string `arg:"" optional:"" help:"Name of the password entry"`
753768
Key string `short:"k" help:"Retrieve a key from a TOML entry"`

test/e2e_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,61 @@ qux = {"key" = "value"}
777777
}
778778
}
779779

780+
func TestKeyCmd(t *testing.T) {
781+
var buf bytes.Buffer
782+
783+
_, err := withPagoDir(func(dataDir string) (string, error) {
784+
// Add a TOML entry.
785+
cmd := exec.Command(commandPago, "--dir", dataDir, "add", "toml-test", "--multiline")
786+
cmd.Stdin = strings.NewReader(`foo = "string"`)
787+
var stdout, stderr bytes.Buffer
788+
cmd.Stdout = &stdout
789+
cmd.Stderr = &stderr
790+
err := cmd.Run()
791+
if err != nil {
792+
return stdout.String() + "\n" + stderr.String(), err
793+
}
794+
795+
// Show a key from the entry using the 'key' command.
796+
c, err := expect.NewConsole()
797+
if err != nil {
798+
return "", fmt.Errorf("failed to create console: %w", err)
799+
}
800+
defer c.Close()
801+
802+
buf.Reset()
803+
804+
cmd = exec.Command(commandPago, "--dir", dataDir, "--socket", "", "key", "toml-test", "foo")
805+
cmd.Stdin = c.Tty()
806+
cmd.Stdout = &buf
807+
cmd.Stderr = c.Tty()
808+
809+
err = cmd.Start()
810+
if err != nil {
811+
return "", fmt.Errorf("failed to start command for key %q: %w", "foo", err)
812+
}
813+
814+
_, _ = c.ExpectString("Enter password")
815+
_, _ = c.SendLine(password)
816+
817+
err = cmd.Wait()
818+
if err != nil {
819+
return "", fmt.Errorf("command failed for key %q: %w", "foo", err)
820+
}
821+
822+
output := strings.TrimSpace(buf.String())
823+
expected := "string"
824+
if output != expected {
825+
return "", fmt.Errorf("for key %q, expected %q, got %q", "foo", expected, output)
826+
}
827+
828+
return "", nil
829+
})
830+
if err != nil {
831+
t.Errorf("Command `key` failed: %v (%q)", err, buf)
832+
}
833+
}
834+
780835
func TestShowOTP(t *testing.T) {
781836
_, err := withPagoDir(func(dataDir string) (string, error) {
782837
// Add a TOML entry with a 6-digit otpauth URI.

0 commit comments

Comments
 (0)