feat(set): adds --file flag to input from a file path

This commit is contained in:
Lewis Wynne 2026-02-11 18:27:22 +00:00
parent 59cb09a8e7
commit b89db8dc48
5 changed files with 30 additions and 2 deletions

View file

@ -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

View file

@ -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")
}

View file

@ -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

3
testdata/set-file-conflict-err.ct vendored Normal file
View file

@ -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

4
testdata/set-file.ct vendored Normal file
View file

@ -0,0 +1,4 @@
$ fecho myfile hello from file
$ pda set key@sf --file myfile
$ pda get key@sf
hello from file