diff --git a/README.md b/README.md index 550b72d..a1d6a66 100644 --- a/README.md +++ b/README.md @@ -19,14 +19,14 @@
`pda!` is a command-line key-value store tool with: -- [templates](https://github.com/Llywelwyn/pda#templates), +- [templates](https://github.com/Llywelwyn/pda#templates) supporting arbitrary shell execution, conditionals, loops, more, - [encryption](https://github.com/Llywelwyn/pda#encryption) at rest using [age](https://github.com/FiloSottile/age), -- Git-backed [version control](https://github.com/Llywelwyn/pda#git), -- [search and filtering](https://github.com/Llywelwyn/pda#filtering) by key and/or value, -- plaintext exports in multiple formats, +- Git-backed [version control](https://github.com/Llywelwyn/pda#git) with automatic syncing, +- [search and filtering](https://github.com/Llywelwyn/pda#filtering) by key, value, or store, +- plaintext exports in 7 different formats, - support for all [binary data](https://github.com/Llywelwyn/pda#binary), -- [time-to-live](https://github.com/Llywelwyn/pda#ttl)/expiry support, -- built-in [diagnostics](https://github.com/Llywelwyn/pda#doctor), +- expiring keys with a [time-to-live](https://github.com/Llywelwyn/pda#ttl), +- built-in [diagnostics](https://github.com/Llywelwyn/pda#doctor) and [configuration](https://github.com/Llywelwyn/pda#config), and more, written in pure Go, and inspired by [skate](https://github.com/charmbracelet/skate) and [nb](https://github.com/xwmx/nb). @@ -394,7 +394,7 @@ Values support effectively all of Go's `text/template` syntax. Templates are eva `text/template` is a Turing-complete templating library that supports most of what you'd expect in a scripting language. Actions are given with ``{{ action }}`` syntax and support pipelines and nested templates, along with a lot more. I recommend reading the documentation if you want to do anything more complicated than described here. -To fit `text/template` nicely into this tool, pda has a sparse set of additional functions built-in. For example, `default` values, `enum`s, `require`d values, `time`, `lists`, among others. These same functions are also available in `git.default_commit_message` templates, along with `{{ summary }}` which returns the action that triggered the commit (e.g. "set foo", "removed bar"). +To fit `text/template` nicely into this tool, pda has a sparse set of additional functions built-in. For example, `default` values, `enum`s, `require`d values, `time`, `lists`, arbitrary `shell` execution, and getting other `pda` keys (recursively!). These same functions are also available in `git.default_commit_message` templates, along with `summary` which returns the action that triggered the commit (e.g. "set foo", "removed bar"). Below is more detail on the extra functions added by this tool. @@ -481,6 +481,35 @@ pda get names NAMES=Bob,Alice +`shell` executes a command and returns stdout. +```bash +pda set rev '{{ shell "git rev-parse --short HEAD" }}' +pda get rev +# a1b2c3d + +pda set today '{{ shell "date +%Y-%m-%d" }}' +pda get today +# 2025-06-15 +``` + + + +`pda` gets another key. +```bash +pda set base_url "https://api.example.com" +pda set endpoint '{{ pda "base_url" }}/users/{{ require .ID }}' +pda get endpoint ID=42 +# https://api.example.com/users/42 + +# Cross-store references work too. +pda set host@urls "https://example.com" +pda set api '{{ pda "host@urls" }}/api' +pda get api +# https://example.com/api +``` + + + pass `no-template` to output literally without templating. ```bash pda set hello "{{ if .MORNING }}Good morning.{{ end }}" diff --git a/cmd/get.go b/cmd/get.go index 5a3a85d..e4b6b1c 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -148,18 +148,22 @@ func applyTemplate(tplBytes []byte, substitutions []string) ([]byte, error) { val := parts[1] vars[key] = val } + funcMap := templateFuncMap() + funcMap["pda"] = func(key string) (string, error) { + return pdaGet(key, substitutions) + } tpl, err := template.New("cmd"). Delims("{{", "}}"). // Render missing map keys as zero values so the default helper can decide on fallbacks. Option("missingkey=zero"). - Funcs(templateFuncMap()). + Funcs(funcMap). Parse(string(tplBytes)) if err != nil { return nil, err } var buf bytes.Buffer if err := tpl.Execute(&buf, vars); err != nil { - return nil, err + return nil, cleanTemplateError(err) } return buf.Bytes(), nil } diff --git a/cmd/template.go b/cmd/template.go index 0bd1209..d8cef0d 100644 --- a/cmd/template.go +++ b/cmd/template.go @@ -25,6 +25,7 @@ package cmd import ( "fmt" "os" + "os/exec" "slices" "strconv" "strings" @@ -81,5 +82,76 @@ func templateFuncMap() template.FuncMap { return parts }, "time": func() string { return time.Now().UTC().Format(time.RFC3339) }, + "shell": func(command string) (string, error) { + sh := os.Getenv("SHELL") + if sh == "" { + sh = "/bin/sh" + } + out, err := exec.Command(sh, "-c", command).Output() + if err != nil { + if exitErr, ok := err.(*exec.ExitError); ok && len(exitErr.Stderr) > 0 { + return "", fmt.Errorf("shell %q: %s", command, strings.TrimSpace(string(exitErr.Stderr))) + } + return "", fmt.Errorf("shell %q: %w", command, err) + } + return strings.TrimRight(string(out), "\n"), nil + }, + "pda": func(key string) (string, error) { + return pdaGet(key, nil) + }, } } + +// cleanTemplateError strips Go template engine internals from function call +// errors, returning just the inner error message. Template execution errors +// look like: "template: cmd:1:3: executing "cmd" at