feat(Set/Get): added Long descriptions
This commit is contained in:
parent
0a70169adb
commit
9fdc831832
2 changed files with 45 additions and 5 deletions
23
cmd/get.go
23
cmd/get.go
|
|
@ -26,6 +26,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"os/exec"
|
"os/exec"
|
||||||
|
"slices"
|
||||||
"strings"
|
"strings"
|
||||||
"text/template"
|
"text/template"
|
||||||
|
|
||||||
|
|
@ -37,6 +38,16 @@ import (
|
||||||
var getCmd = &cobra.Command{
|
var getCmd = &cobra.Command{
|
||||||
Use: "get KEY[@DB]",
|
Use: "get KEY[@DB]",
|
||||||
Short: "Get a value for a key. Optionally specify a 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"},
|
Aliases: []string{"g"},
|
||||||
Args: cobra.MinimumNArgs(1),
|
Args: cobra.MinimumNArgs(1),
|
||||||
RunE: get,
|
RunE: get,
|
||||||
|
|
@ -136,9 +147,19 @@ func applyTemplate(tplBytes []byte, substitutions []string) ([]byte, error) {
|
||||||
return s
|
return s
|
||||||
},
|
},
|
||||||
"env": os.Getenv,
|
"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").
|
tpl, err := template.New("cmd").
|
||||||
Delims("[[", "]]").
|
Delims("{{", "}}").
|
||||||
// Render missing map keys as zero values so the default helper can decide on fallbacks.
|
// Render missing map keys as zero values so the default helper can decide on fallbacks.
|
||||||
Option("missingkey=zero").
|
Option("missingkey=zero").
|
||||||
Funcs(funcMap).
|
Funcs(funcMap).
|
||||||
|
|
|
||||||
21
cmd/set.go
21
cmd/set.go
|
|
@ -31,7 +31,26 @@ import (
|
||||||
// setCmd represents the set command
|
// setCmd represents the set command
|
||||||
var setCmd = &cobra.Command{
|
var setCmd = &cobra.Command{
|
||||||
Use: "set KEY[@DB] [VALUE]",
|
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"},
|
Aliases: []string{"s"},
|
||||||
Args: cobra.RangeArgs(1, 2),
|
Args: cobra.RangeArgs(1, 2),
|
||||||
RunE: set,
|
RunE: set,
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue