package main import ( "bytes" "os" "os/exec" "path/filepath" "runtime" "strings" "text/template" "github.com/BurntSushi/toml" ) type TemplateData struct { OS string Arch string Hostname string Home 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 strings.TrimRight(string(line), "\n"), nil } func getTemplateData(conf *Conf) (*TemplateData, error) { vars, err := readVars(conf) if err != nil { return nil, err } data := &TemplateData{ OS: runtime.GOOS, Arch: runtime.GOARCH, Vars: vars, } if host, err := os.Hostname(); err != nil { return nil, err } else { data.Hostname = host } if home, err := os.UserHomeDir(); err != nil { return nil, err } else { data.Home = home } return data, nil } func readVars(conf *Conf) (map[string]interface{}, error) { var vars map[string]interface{} if _, err := toml.DecodeFile(filepath.Join(conf.Source, "vars.toml"), &vars); err != nil { return nil, err } return vars, nil }