forked from twpayne/chezmoi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvaulttemplatefuncs.go
More file actions
40 lines (36 loc) · 963 Bytes
/
vaulttemplatefuncs.go
File metadata and controls
40 lines (36 loc) · 963 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package cmd
import (
"encoding/json"
"fmt"
"os/exec"
"github.com/twpayne/chezmoi/v2/internal/chezmoi"
)
type vaultConfig struct {
Command string
cache map[string]interface{}
}
func (c *Config) vaultTemplateFunc(key string) interface{} {
if data, ok := c.Vault.cache[key]; ok {
return data
}
name := c.Vault.Command
args := []string{"kv", "get", "-format=json", key}
cmd := exec.Command(name, args...)
cmd.Stdin = c.stdin
cmd.Stderr = c.stderr
output, err := c.baseSystem.IdempotentCmdOutput(cmd)
if err != nil {
returnTemplateError(fmt.Errorf("%s %s: %w\n%s", name, chezmoi.ShellQuoteArgs(args), err, output))
return nil
}
var data interface{}
if err := json.Unmarshal(output, &data); err != nil {
returnTemplateError(fmt.Errorf("%s %s: %w\n%s", name, chezmoi.ShellQuoteArgs(args), err, output))
return nil
}
if c.Vault.cache == nil {
c.Vault.cache = make(map[string]interface{})
}
c.Vault.cache[key] = data
return data
}