feat(Set/Get): added Long descriptions

This commit is contained in:
Lewis Wynne 2025-11-20 01:01:02 +00:00
parent 0a70169adb
commit 9fdc831832
2 changed files with 45 additions and 5 deletions

View file

@ -26,6 +26,7 @@ import (
"fmt"
"os"
"os/exec"
"slices"
"strings"
"text/template"
@ -37,6 +38,16 @@ import (
var getCmd = &cobra.Command{
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).

View file

@ -31,7 +31,26 @@ 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.",
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,