aboutsummaryrefslogtreecommitdiff
path: root/template.go
diff options
context:
space:
mode:
Diffstat (limited to 'template.go')
-rw-r--r--template.go54
1 files changed, 54 insertions, 0 deletions
diff --git a/template.go b/template.go
new file mode 100644
index 0000000..cb6dc2e
--- /dev/null
+++ b/template.go
@@ -0,0 +1,54 @@
+package main
+
+import (
+ "bytes"
+ "os"
+ "os/exec"
+ "path/filepath"
+ "text/template"
+
+ "github.com/BurntSushi/toml"
+)
+
+type TemplateData struct {
+ Hostname string
+ Vars map[string]interface{}
+}
+
+func buildFuncMap() template.FuncMap {
+ return map[string]interface{}{
+ "pass": lookupPassword,
+ "env": os.Getenv,
+ }
+}
+
+func lookupPassword(name string) (string, error) {
+ out, err := exec.Command("pass", name).CombinedOutput()
+ if err != nil {
+ return "", err
+ }
+ buf := bytes.NewBuffer(out)
+ line, err := buf.ReadBytes('\n')
+ if err != nil {
+ return "", err
+ }
+ return string(line), nil
+}
+
+func getTemplateData() (*TemplateData, error) {
+ var data TemplateData
+ vars, err := readVars()
+ if err != nil {
+ return nil, err
+ }
+ data.Vars = vars
+ return &data, nil
+}
+
+func readVars() (map[string]interface{}, error) {
+ var vars map[string]interface{}
+ if _, err := toml.DecodeFile(filepath.Join(base, "vars.toml"), &vars); err != nil {
+ return nil, err
+ }
+ return vars, nil
+}