From b89db8dc4831f56ec9361cf7059625d4c53bf702 Mon Sep 17 00:00:00 2001 From: lew Date: Wed, 11 Feb 2026 18:27:22 +0000 Subject: [PATCH] feat(set): adds --file flag to input from a file path --- README.md | 4 ++++ cmd/set.go | 19 +++++++++++++++++-- testdata/help-set.ct | 2 ++ testdata/set-file-conflict-err.ct | 3 +++ testdata/set-file.ct | 4 ++++ 5 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 testdata/set-file-conflict-err.ct create mode 100644 testdata/set-file.ct diff --git a/README.md b/README.md index c1390a9..c2cd43a 100644 --- a/README.md +++ b/README.md @@ -140,6 +140,10 @@ echo "Alice" | pda set name cat dogs.txt | pda set dogs pda set kitty < cat.png +# From a file +pda set dogs --file dogs.txt +pda set kitty -f cat.png + # --safe to skip if the key already exists. pda set name "Alice" --safe pda set name "Bob" --safe diff --git a/cmd/set.go b/cmd/set.go index 2fe7558..43d95f9 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -25,6 +25,7 @@ package cmd import ( "fmt" "io" + "os" "strings" "time" @@ -78,10 +79,23 @@ func set(cmd *cobra.Command, args []string) error { return fmt.Errorf("cannot set '%s': %v", args[0], err) } + filePath, err := cmd.Flags().GetString("file") + if err != nil { + return fmt.Errorf("cannot set '%s': %v", args[0], err) + } + var value []byte - if len(args) == 2 { + switch { + case filePath != "" && len(args) == 2: + return fmt.Errorf("cannot set '%s': --file and VALUE argument are mutually exclusive", args[0]) + case filePath != "": + value, err = os.ReadFile(filePath) + if err != nil { + return fmt.Errorf("cannot set '%s': %v", args[0], err) + } + case len(args) == 2: value = []byte(args[1]) - } else { + default: bytes, err := io.ReadAll(cmd.InOrStdin()) if err != nil { return fmt.Errorf("cannot set '%s': %v", args[0], err) @@ -169,4 +183,5 @@ func init() { setCmd.Flags().BoolP("interactive", "i", false, "Prompt before overwriting an existing key") setCmd.Flags().BoolP("encrypt", "e", false, "Encrypt the value at rest using age") setCmd.Flags().Bool("safe", false, "Do not overwrite if the key already exists") + setCmd.Flags().StringP("file", "f", "", "Read value from a file") } diff --git a/testdata/help-set.ct b/testdata/help-set.ct index 2f6c4f3..330ea71 100644 --- a/testdata/help-set.ct +++ b/testdata/help-set.ct @@ -22,6 +22,7 @@ Aliases: Flags: -e, --encrypt Encrypt the value at rest using age + -f, --file string Read value from a file -h, --help help for set -i, --interactive Prompt before overwriting an existing key --safe Do not overwrite if the key already exists @@ -48,6 +49,7 @@ Aliases: Flags: -e, --encrypt Encrypt the value at rest using age + -f, --file string Read value from a file -h, --help help for set -i, --interactive Prompt before overwriting an existing key --safe Do not overwrite if the key already exists diff --git a/testdata/set-file-conflict-err.ct b/testdata/set-file-conflict-err.ct new file mode 100644 index 0000000..19ecd17 --- /dev/null +++ b/testdata/set-file-conflict-err.ct @@ -0,0 +1,3 @@ +$ fecho myfile contents +$ pda set key@sfc value --file myfile --> FAIL +FAIL cannot set 'key@sfc': --file and VALUE argument are mutually exclusive diff --git a/testdata/set-file.ct b/testdata/set-file.ct new file mode 100644 index 0000000..d6b913e --- /dev/null +++ b/testdata/set-file.ct @@ -0,0 +1,4 @@ +$ fecho myfile hello from file +$ pda set key@sf --file myfile +$ pda get key@sf +hello from file