diff --git a/cmd/get.go b/cmd/get.go index d6e737a..39ddb7e 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -26,6 +26,7 @@ import ( "fmt" "os" "os/exec" + "slices" "strings" "text/template" @@ -35,8 +36,18 @@ import ( // getCmd represents the get command var getCmd = &cobra.Command{ - Use: "get KEY[@DB]", - Short: "Get a value for a key. Optionally specify a db.", + Use: "get KEY[@DB]", + Short: "Get a value for a key. Optionally specify a db.", + Long: `Get a value for a key. Optionally specify a db. + +[[ .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 get greeting NAME=World + +Further reading: Go's text/template documentation.`, Aliases: []string{"g"}, Args: cobra.MinimumNArgs(1), RunE: get, @@ -136,9 +147,19 @@ func applyTemplate(tplBytes []byte, substitutions []string) ([]byte, error) { return s }, "env": os.Getenv, + "enum": func(v any, allowed ...string) (string, error) { + s := fmt.Sprint(v) + if s == "" { + return "", fmt.Errorf("enum value is missing or empty") + } + if slices.Contains(allowed, s) { + return s, nil + } + return "", fmt.Errorf("invalid value %q (allowed: %v)", s, allowed) + }, } tpl, err := template.New("cmd"). - Delims("[[", "]]"). + Delims("{{", "}}"). // Render missing map keys as zero values so the default helper can decide on fallbacks. Option("missingkey=zero"). Funcs(funcMap). diff --git a/cmd/set.go b/cmd/set.go index 29989cf..056e286 100644 --- a/cmd/set.go +++ b/cmd/set.go @@ -30,8 +30,27 @@ import ( // setCmd represents the set command var setCmd = &cobra.Command{ - Use: "set KEY[@DB] [VALUE]", - Short: "Set a value for a key by passing VALUE or from Stdin. Optionally specify a db.", + Use: "set KEY[@DB] [VALUE]", + Short: "Set a value for a key by passing VALUE or Stdin. Optionally specify a db.", + Long: `Set a value for a key by passing VALUE or Stdin. Optionally specify a db. + +PDA supports parsing Go templates. Actions are delimited with {{ }}. + +For example: + 'Hello, {{ .NAME }}' can be substituted with NAME="John Doe". + 'Hello, {{ env "USER" }}' will fetch the USER env variable. + 'Hello, {{ default "World" .NAME }}' will default to World if NAME is blank. + 'Hello, {{ require .NAME }}' will error if NAME is blank. + '{{ enum .NAME "Alice" "Bob" }}' allows only NAME=Alice or NAME=Bob. + +[[ .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 get greeting NAME=World + +Further reading: Go's text/template documentation.`, Aliases: []string{"s"}, Args: cobra.RangeArgs(1, 2), RunE: set,