Home

tmpl @c01d965ad77ba352724d87b182bc68f68879a1c6 - 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
package registry

import (
	"fmt"
	"html/template"
	"io/ioutil"
	"os"
	"path/filepath"
	"sort"
	"strings"
	"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"`
	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) 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)
	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").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) (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
	}

	// Sort the map keys so they are consistent
	vars := tree.ToMap()
	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
}