Home

tmpl @cae16cdbdc6ec5ee3def0755aeb1959f1d20f7de - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tmpl / registry / template.go
- raw
  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
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package registry

import (
	"fmt"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"text/template"
	"time"

	"github.com/AlecAivazis/survey/v2"
	"github.com/mholt/archiver/v3"
	"github.com/pelletier/go-toml"
)

// Template is a tmpl project
type Template struct {
	reg        *Registry `toml:"-"`
	Name       string    `toml:"name"`
	Path       string    `toml:"path"`
	Repository string    `toml:"repository"`
	Branch     string    `toml:"branch"`
	Created    time.Time `toml:"created"`
}

// ArchiveName is the name given to the archive for this Template
func (t *Template) ArchiveName() string {
	return fmt.Sprintf("%s.tar.gz", t.Name)
}

// ArchivePath is the full path to the archive for this Template within the Registry
func (t *Template) ArchivePath() string {
	return filepath.Join(t.reg.dir, t.ArchiveName())
}

// Execute runs the Template and copies to dest
func (t *Template) Execute(dest string, defaults bool) error {
	tmp, err := ioutil.TempDir(os.TempDir(), "tmpl")
	if err != nil {
		return err
	}
	defer os.RemoveAll(tmp)

	if err := archiver.Unarchive(t.ArchivePath(), tmp); err != nil {
		return err
	}

	vars, err := prompt(tmp, defaults)
	if err != nil {
		return err
	}

	base := filepath.Join(tmp, "template")
	return filepath.Walk(base, func(walkPath string, walkInfo os.FileInfo, walkErr error) error {
		if walkErr != nil {
			return walkErr
		}

		if walkInfo.IsDir() {
			return nil
		}

		contents, err := ioutil.ReadFile(walkPath)
		if err != nil {
			return err
		}

		tmpl, err := template.New("tmpl").Funcs(mergeMaps(funcMap, convertMap(vars))).Parse(string(contents))
		if err != nil {
			return err
		}

		newDest := strings.TrimPrefix(walkPath, base+"/")
		newDest = filepath.Join(dest, newDest)

		if err := os.MkdirAll(filepath.Dir(newDest), os.ModePerm); err != nil {
			return err
		}

		oldFi, err := os.Lstat(walkPath)
		if err != nil {
			return err
		}
		newFi, err := os.OpenFile(newDest, os.O_RDWR|os.O_CREATE|os.O_TRUNC, oldFi.Mode())
		if err != nil {
			return err
		}

		if err := tmpl.Execute(newFi, vars); err != nil {
			return err
		}

		return newFi.Close()
	})
}

func prompt(dir string, defaults bool) (map[string]interface{}, error) {
	templatePath := filepath.Join(dir, "template.toml")
	if _, err := os.Lstat(templatePath); err != nil {
		return nil, err
	}

	tree, err := toml.LoadFile(templatePath)
	if err != nil {
		return nil, err
	}
	vars := tree.ToMap()

	// Return early if we only want defaults
	if defaults {
		return vars, nil
	}

	// Sort the map keys so they are consistent
	sorted := make([]string, 0, len(vars))
	for k := range vars {
		sorted = append(sorted, k)
	}
	sort.Strings(sorted)

	for _, k := range sorted {
		v := vars[k]
		var p survey.Prompt
		switch t := v.(type) {
		case []string:
			p = &survey.Select{
				Message: k,
				Options: t,
			}
		default:
			p = &survey.Input{
				Message: k,
				Default: fmt.Sprintf("%v", t),
			}
		}
		q := []*survey.Question{
			{
				Name:     "response",
				Prompt:   p,
				Validate: survey.Required,
			},
		}
		a := struct {
			Response string
		}{}
		if err := survey.Ask(q, &a); err != nil {
			return nil, err
		}
		vars[k] = a.Response
	}

	return vars, nil
}

func convertMap(m map[string]interface{}) template.FuncMap {
	mm := make(template.FuncMap)
	for k, v := range m {
		vv := v // Enclosures in a loop
		mm[k] = func() interface{} {
			return fmt.Sprintf("%v", vv)
		}
	}
	return mm
}

func mergeMaps(maps ...map[string]interface{}) map[string]interface{} {
	m := make(map[string]interface{})
	for _, mm := range maps {
		for k, v := range mm {
			m[k] = v
		}
	}
	return m
}