feat(get): adds --exists flag for checking existence of a key
This commit is contained in:
parent
ad98a1e6c4
commit
6e1af5ba28
4 changed files with 18 additions and 0 deletions
|
|
@ -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
|
||||
```
|
||||
|
||||
<p align="center"></p><!-- spacer -->
|
||||
|
|
|
|||
10
cmd/get.go
10
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")
|
||||
|
|
|
|||
3
testdata/get-exists.ct
vendored
Normal file
3
testdata/get-exists.ct
vendored
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
$ pda set found@ge "hello"
|
||||
$ pda get found@ge --exists
|
||||
$ pda get missing@ge --exists --> FAIL
|
||||
2
testdata/help-get.ct
vendored
2
testdata/help-get.ct
vendored
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue