Home

tmpl @aee847f01826d46b3ac91362f8d8cefde20d1518 - refs - log -
-
https://git.jolheiser.com/tmpl.git
Template automation
tmpl / registry / registry.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package registry

import (
	"errors"
	"io/ioutil"
	"os"
	"path/filepath"
	"strings"
	"time"

	"github.com/go-git/go-git/v5"
	"github.com/go-git/go-git/v5/plumbing"
	"github.com/mholt/archiver/v3"
	"github.com/pelletier/go-toml"
)

// Registry is a collection of Template
type Registry struct {
	dir       string
	Sources   []*Source
	Templates []*Template
}

func (r *Registry) save() error {
	fi, err := os.Create(r.MetaFilePath())
	if err != nil {
		return err
	}
	if err := toml.NewEncoder(fi).Encode(r); err != nil {
		return err
	}
	return fi.Close()
}

// MetaFilePath is the path to the Registry meta-file
func (r *Registry) MetaFilePath() string {
	return filepath.Join(r.dir, "registry.toml")
}

// GetTemplate retrieves a Template from the Registry
func (r *Registry) GetTemplate(name string) (*Template, error) {
	for _, t := range r.Templates {
		if strings.EqualFold(name, t.Name) {
			t.reg = r
			return t, nil
		}
	}
	return nil, ErrTemplateNotFound{Name: name}
}

// AddTemplate downloads and adds a new Template to the Registry
func (r *Registry) AddTemplate(name, repo, branch string) (*Template, error) {
	t := &Template{
		reg:        r,
		Name:       name,
		Repository: repo,
		Branch:     branch,
		Created:    time.Now(),
	}
	r.Templates = append(r.Templates, t)

	if err := download(repo, branch, t.ArchivePath()); err != nil {
		return nil, err
	}

	return t, r.save()
}

// RemoveTemplate removes the Template from disk and meta
func (r *Registry) RemoveTemplate(name string) error {
	_, err := r.GetTemplate(name)
	if err != nil {
		return err
	}
	for idx, t := range r.Templates {
		if strings.EqualFold(name, t.Name) {
			r.Templates = append(r.Templates[:idx], r.Templates[idx+1:]...)
			if err := os.Remove(t.ArchivePath()); err != nil {
				return err
			}
		}
	}

	return r.save()
}

// GetSource retrieves a Source from the Registry
func (r *Registry) GetSource(name string) (*Source, error) {
	for _, s := range r.Sources {
		if strings.EqualFold(name, s.Name) {
			return s, nil
		}
	}
	return nil, ErrSourceNotFound{Name: name}
}

// AddSource adds a new Source to the Registry
func (r *Registry) AddSource(url, name string) (*Source, error) {
	url = strings.TrimSuffix(url, "/") + "/"
	s := &Source{
		Name: name,
		URL:  url,
	}
	r.Sources = append(r.Sources, s)

	return s, r.save()
}

// RemoveSource removes the Source from the registry meta
func (r *Registry) RemoveSource(name string) error {
	_, err := r.GetSource(name)
	if err != nil {
		return err
	}
	for idx, s := range r.Sources {
		if strings.EqualFold(name, s.Name) {
			r.Sources = append(r.Sources[:idx], r.Sources[idx+1:]...)
		}
	}

	return r.save()
}

// Open opens a Registry, creating one if none exists at dir
func Open(dir string) (*Registry, error) {
	reg := Registry{
		dir: dir,
	}

	_, err := os.Lstat(reg.MetaFilePath())
	if err != nil {
		if os.IsNotExist(err) {
			if err := create(reg.MetaFilePath()); err != nil {
				return nil, err
			}
		} else {
			return nil, err
		}
	}

	tree, err := toml.LoadFile(reg.MetaFilePath())
	if err != nil {
		return nil, err
	}
	return &reg, tree.Unmarshal(&reg)
}

func create(regFile string) error {
	if err := os.MkdirAll(filepath.Dir(regFile), os.ModePerm); err != nil {
		return err
	}
	fi, err := os.Create(regFile)
	if err != nil {
		return err
	}
	return fi.Close()
}

func download(cloneURL, branch, dest string) error {
	tmp, err := ioutil.TempDir(os.TempDir(), "tmpl")
	if err != nil {
		return err
	}
	defer os.RemoveAll(tmp)

	// Clone the repo
	if _, err := git.PlainClone(tmp, false, &git.CloneOptions{
		URL:           cloneURL,
		ReferenceName: plumbing.NewBranchReferenceName(branch),
		SingleBranch:  true,
		Depth:         1,
	}); err != nil {
		return err
	}

	// RemoveTemplate .git
	if err := os.RemoveAll(filepath.Join(tmp, ".git")); err != nil {
		return err
	}

	// Make sure it's a valid template
	if _, err := os.Lstat(filepath.Join(tmp, "template.toml")); err != nil {
		return err
	}
	fi, err := os.Lstat(filepath.Join(tmp, "template"))
	if err != nil {
		return err
	}
	if !fi.IsDir() {
		return errors.New("template found, expected directory")
	}

	// Create archive
	glob, err := filepath.Glob(filepath.Join(tmp, "*"))
	if err != nil {
		return err
	}

	if err := archiver.Archive(glob, dest); err != nil {
		return err
	}

	return nil
}