From 6e1af5ba2825a1528b67167e3d145d85a9396e05 Mon Sep 17 00:00:00 2001 From: lew Date: Wed, 11 Feb 2026 17:36:49 +0000 Subject: [PATCH] feat(get): adds --exists flag for checking existence of a key --- README.md | 3 +++ cmd/get.go | 10 ++++++++++ testdata/get-exists.ct | 3 +++ testdata/help-get.ct | 2 ++ 4 files changed, 18 insertions(+) create mode 100644 testdata/get-exists.ct diff --git a/README.md b/README.md index 6a2df64..1a1ed3e 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,9 @@ pda get name # Or run it directly. pda run name # same as: pda get name --run + +# Check if a key exists (exit 0 if found, exit 1 if not). +pda get name --exists ```

diff --git a/cmd/get.go b/cmd/get.go index 54c3b7c..116404f 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -87,6 +87,15 @@ func get(cmd *cobra.Command, args []string) error { return fmt.Errorf("cannot get '%s': %v", args[0], err) } idx := findEntry(entries, spec.Key) + + existsOnly, _ := cmd.Flags().GetBool("exists") + if existsOnly { + if idx < 0 { + os.Exit(1) + } + return nil + } + if idx < 0 { keys := make([]string, len(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().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("exists", false, "exit 0 if the key exists, exit 1 if not (no output)") rootCmd.AddCommand(getCmd) runCmd.Flags().BoolP("base64", "b", false, "view binary data as base64") diff --git a/testdata/get-exists.ct b/testdata/get-exists.ct new file mode 100644 index 0000000..a975b42 --- /dev/null +++ b/testdata/get-exists.ct @@ -0,0 +1,3 @@ +$ pda set found@ge "hello" +$ pda get found@ge --exists +$ pda get missing@ge --exists --> FAIL diff --git a/testdata/help-get.ct b/testdata/help-get.ct index c429da8..3fa513d 100644 --- a/testdata/help-get.ct +++ b/testdata/help-get.ct @@ -17,6 +17,7 @@ Aliases: Flags: -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 --no-template directly output template syntax -c, --run execute the result as a shell command @@ -37,6 +38,7 @@ Aliases: Flags: -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 --no-template directly output template syntax -c, --run execute the result as a shell command