diff --git a/README.md b/README.md index 05b0ce2..fed376c 100644 --- a/README.md +++ b/README.md @@ -81,6 +81,7 @@ Key commands: list List the contents of a store move Move a key remove Delete one or more keys + run Get the value of a key and execute it set Set a key to a given value Store commands: @@ -143,7 +144,8 @@ pda get name # Alice # Or run it directly. -pda get name --run +pda run name +# same as: pda get name --run ```
@@ -651,14 +653,14 @@ PDA_DATA=/tmp/stores pda set key value -`pda get --run` uses `SHELL` for command execution. +`pda run` (or `pda get --run`) uses `SHELL` for command execution. ```bash # SHELL is usually your current shell. -pda get script --run +pda run script # An empty SHELL falls back to using 'sh'. export SHELL="" -pda get script --run +pda run script ``` diff --git a/cmd/get.go b/cmd/get.go index 28edda2..2c4f8d4 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -54,6 +54,22 @@ For example: SilenceUsage: true, } +var runCmd = &cobra.Command{ + Use: "run KEY[@STORE]", + Short: "Get the value of a key and execute it", + Long: `Get the value of a key and execute it as a shell command. Optionally specify a store. + +{{ .TEMPLATES }} can be filled by passing TEMPLATE=VALUE as an +additional argument after the initial KEY being fetched. + +For example: + pda set greeting 'Hello, {{ .NAME }}!' + pda run greeting NAME=World`, + Args: cobra.MinimumNArgs(1), + RunE: run, + SilenceUsage: true, +} + func get(cmd *cobra.Command, args []string) error { store := &Store{} @@ -91,11 +107,6 @@ func get(cmd *cobra.Command, args []string) error { return fmt.Errorf("cannot get '%s': %v", args[0], err) } - run, err := cmd.Flags().GetBool("run") - if err != nil { - return fmt.Errorf("cannot get '%s': %v", args[0], err) - } - noTemplate, err := cmd.Flags().GetBool("no-template") if err != nil { return fmt.Errorf("cannot get '%s': %v", args[0], err) @@ -112,8 +123,8 @@ func get(cmd *cobra.Command, args []string) error { } } - if run { - return runCmd(string(v)) + if runFlag { + return runShellCommand(string(v)) } store.Print("%s", binary, v) @@ -194,7 +205,7 @@ func applyTemplate(tplBytes []byte, substitutions []string) ([]byte, error) { return buf.Bytes(), nil } -func runCmd(command string) error { +func runShellCommand(command string) error { shell := os.Getenv("SHELL") if shell == "" { shell = "/bin/sh" @@ -218,10 +229,22 @@ func runCmd(command string) error { return nil } +func run(cmd *cobra.Command, args []string) error { + runFlag = true + return get(cmd, args) +} + +var runFlag bool + func init() { getCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output") getCmd.Flags().Bool("secret", false, "display values marked as secret") - getCmd.Flags().BoolP("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") rootCmd.AddCommand(getCmd) + + runCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output") + runCmd.Flags().Bool("secret", false, "display values marked as secret") + runCmd.Flags().Bool("no-template", false, "directly output template syntax") + rootCmd.AddCommand(runCmd) } diff --git a/cmd/root.go b/cmd/root.go index 82a269d..84e1ed8 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -52,6 +52,7 @@ func init() { setCmd.GroupID = "keys" getCmd.GroupID = "keys" + runCmd.GroupID = "keys" mvCmd.GroupID = "keys" cpCmd.GroupID = "keys" delCmd.GroupID = "keys" diff --git a/testdata/help__ok.ct b/testdata/help__ok.ct index 339038a..7065f74 100644 --- a/testdata/help__ok.ct +++ b/testdata/help__ok.ct @@ -18,6 +18,7 @@ Key commands: list List the contents of a store move Move a key remove Delete one or more keys + run Get the value of a key and execute it set Set a key to a given value Store commands: @@ -58,6 +59,7 @@ Key commands: list List the contents of a store move Move a key remove Delete one or more keys + run Get the value of a key and execute it set Set a key to a given value Store commands: diff --git a/testdata/root__ok.ct b/testdata/root__ok.ct index f288cf9..099e74b 100644 --- a/testdata/root__ok.ct +++ b/testdata/root__ok.ct @@ -17,6 +17,7 @@ Key commands: list List the contents of a store move Move a key remove Delete one or more keys + run Get the value of a key and execute it set Set a key to a given value Store commands: