feat(get): adds --exists flag for checking existence of a key

This commit is contained in:
Lewis Wynne 2026-02-11 17:36:49 +00:00
parent ad98a1e6c4
commit 6e1af5ba28
4 changed files with 18 additions and 0 deletions

View file

@ -151,6 +151,9 @@ pda get name
# Or run it directly. # Or run it directly.
pda run name pda run name
# same as: pda get name --run # same as: pda get name --run
# Check if a key exists (exit 0 if found, exit 1 if not).
pda get name --exists
``` ```
<p align="center"></p><!-- spacer --> <p align="center"></p><!-- spacer -->

View file

@ -87,6 +87,15 @@ func get(cmd *cobra.Command, args []string) error {
return fmt.Errorf("cannot get '%s': %v", args[0], err) return fmt.Errorf("cannot get '%s': %v", args[0], err)
} }
idx := findEntry(entries, spec.Key) idx := findEntry(entries, spec.Key)
existsOnly, _ := cmd.Flags().GetBool("exists")
if existsOnly {
if idx < 0 {
os.Exit(1)
}
return nil
}
if idx < 0 { if idx < 0 {
keys := make([]string, len(entries)) keys := make([]string, len(entries))
for i, e := range entries { for i, e := range entries {
@ -238,6 +247,7 @@ func init() {
getCmd.Flags().BoolP("base64", "b", false, "view binary data as base64") getCmd.Flags().BoolP("base64", "b", false, "view binary data as base64")
getCmd.Flags().BoolVarP(&runFlag, "run", "c", false, "execute the result as a shell command") getCmd.Flags().BoolVarP(&runFlag, "run", "c", false, "execute the result as a shell command")
getCmd.Flags().Bool("no-template", false, "directly output template syntax") getCmd.Flags().Bool("no-template", false, "directly output template syntax")
getCmd.Flags().Bool("exists", false, "exit 0 if the key exists, exit 1 if not (no output)")
rootCmd.AddCommand(getCmd) rootCmd.AddCommand(getCmd)
runCmd.Flags().BoolP("base64", "b", false, "view binary data as base64") runCmd.Flags().BoolP("base64", "b", false, "view binary data as base64")

3
testdata/get-exists.ct vendored Normal file
View file

@ -0,0 +1,3 @@
$ pda set found@ge "hello"
$ pda get found@ge --exists
$ pda get missing@ge --exists --> FAIL

View file

@ -17,6 +17,7 @@ Aliases:
Flags: Flags:
-b, --base64 view binary data as base64 -b, --base64 view binary data as base64
--exists exit 0 if the key exists, exit 1 if not (no output)
-h, --help help for get -h, --help help for get
--no-template directly output template syntax --no-template directly output template syntax
-c, --run execute the result as a shell command -c, --run execute the result as a shell command
@ -37,6 +38,7 @@ Aliases:
Flags: Flags:
-b, --base64 view binary data as base64 -b, --base64 view binary data as base64
--exists exit 0 if the key exists, exit 1 if not (no output)
-h, --help help for get -h, --help help for get
--no-template directly output template syntax --no-template directly output template syntax
-c, --run execute the result as a shell command -c, --run execute the result as a shell command