feat(Run): adds explicit Run command

This commit is contained in:
Lewis Wynne 2025-12-23 10:00:14 +00:00
parent c5aeb16e16
commit 26871decd0
5 changed files with 42 additions and 13 deletions

View file

@ -81,6 +81,7 @@ Key commands:
list List the contents of a store list List the contents of a store
move Move a key move Move a key
remove Delete one or more keys remove Delete one or more keys
run Get the value of a key and execute it
set Set a key to a given value set Set a key to a given value
Store commands: Store commands:
@ -143,7 +144,8 @@ pda get name
# Alice # Alice
# Or run it directly. # Or run it directly.
pda get name --run pda run name
# same as: pda get name --run
``` ```
<p align="center"></p><!-- spacer --> <p align="center"></p><!-- spacer -->
@ -651,14 +653,14 @@ PDA_DATA=/tmp/stores pda set key value
<p align="center"></p><!-- spacer --> <p align="center"></p><!-- spacer -->
`pda get --run` uses `SHELL` for command execution. `pda run` (or `pda get --run`) uses `SHELL` for command execution.
```bash ```bash
# SHELL is usually your current shell. # SHELL is usually your current shell.
pda get script --run pda run script
# An empty SHELL falls back to using 'sh'. # An empty SHELL falls back to using 'sh'.
export SHELL="" export SHELL=""
pda get script --run pda run script
``` ```
<p align="center"></p><!-- spacer --> <p align="center"></p><!-- spacer -->

View file

@ -54,6 +54,22 @@ For example:
SilenceUsage: true, 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 { func get(cmd *cobra.Command, args []string) error {
store := &Store{} store := &Store{}
@ -91,11 +107,6 @@ 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)
} }
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") noTemplate, err := cmd.Flags().GetBool("no-template")
if err != nil { if err != nil {
return fmt.Errorf("cannot get '%s': %v", args[0], err) return fmt.Errorf("cannot get '%s': %v", args[0], err)
@ -112,8 +123,8 @@ func get(cmd *cobra.Command, args []string) error {
} }
} }
if run { if runFlag {
return runCmd(string(v)) return runShellCommand(string(v))
} }
store.Print("%s", binary, v) store.Print("%s", binary, v)
@ -194,7 +205,7 @@ func applyTemplate(tplBytes []byte, substitutions []string) ([]byte, error) {
return buf.Bytes(), nil return buf.Bytes(), nil
} }
func runCmd(command string) error { func runShellCommand(command string) error {
shell := os.Getenv("SHELL") shell := os.Getenv("SHELL")
if shell == "" { if shell == "" {
shell = "/bin/sh" shell = "/bin/sh"
@ -218,10 +229,22 @@ func runCmd(command string) error {
return nil return nil
} }
func run(cmd *cobra.Command, args []string) error {
runFlag = true
return get(cmd, args)
}
var runFlag bool
func init() { func init() {
getCmd.Flags().BoolP("include-binary", "b", false, "include binary data in text output") 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().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") getCmd.Flags().Bool("no-template", false, "directly output template syntax")
rootCmd.AddCommand(getCmd) 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)
} }

View file

@ -52,6 +52,7 @@ func init() {
setCmd.GroupID = "keys" setCmd.GroupID = "keys"
getCmd.GroupID = "keys" getCmd.GroupID = "keys"
runCmd.GroupID = "keys"
mvCmd.GroupID = "keys" mvCmd.GroupID = "keys"
cpCmd.GroupID = "keys" cpCmd.GroupID = "keys"
delCmd.GroupID = "keys" delCmd.GroupID = "keys"

View file

@ -18,6 +18,7 @@ Key commands:
list List the contents of a store list List the contents of a store
move Move a key move Move a key
remove Delete one or more keys remove Delete one or more keys
run Get the value of a key and execute it
set Set a key to a given value set Set a key to a given value
Store commands: Store commands:
@ -58,6 +59,7 @@ Key commands:
list List the contents of a store list List the contents of a store
move Move a key move Move a key
remove Delete one or more keys remove Delete one or more keys
run Get the value of a key and execute it
set Set a key to a given value set Set a key to a given value
Store commands: Store commands:

View file

@ -17,6 +17,7 @@ Key commands:
list List the contents of a store list List the contents of a store
move Move a key move Move a key
remove Delete one or more keys remove Delete one or more keys
run Get the value of a key and execute it
set Set a key to a given value set Set a key to a given value
Store commands: Store commands: