-
Notifications
You must be signed in to change notification settings - Fork 3
/
root.go
64 lines (53 loc) · 1.52 KB
/
root.go
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
package conf
import (
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/Depado/projectmpl/utils"
yaml "gopkg.in/yaml.v2"
)
// Root is a ConfigFile with extra information. It should be located at the root
// of the template
type Root struct {
ConfigFile `yaml:",inline"`
Name string `yaml:"name"`
Version string `yaml:"version"`
Description string `yaml:"description"`
After []Command `yaml:"after"`
}
// Parse will parse the yaml file and store its result in the root config
func (r *Root) Parse() error {
var err error
var out []byte
if out, err = ioutil.ReadFile(r.ConfigFile.File.Path); err != nil {
return err
}
return yaml.Unmarshal(out, r)
}
// ExecuteCommands will execute the commands in the newly rendered directory
func (r Root) ExecuteCommands(dir string) {
var err error
if err = os.Chdir(dir); err != nil {
utils.FatalPrintln("Couldn't change directory:", err)
}
for _, cmd := range r.After {
if cmd.Cmd != "" {
if cmd.If != "" && r.Variables != nil {
if v := r.Variables.FindNamed(cmd.If); v != nil && v.True() {
cmd.Run()
}
} else {
cmd.Run()
}
}
}
}
// NewPath adds the path where the file should be rendered according to the root
func (r Root) NewPath(f *File, new string) {
f.NewPath = strings.Replace(f.Path, r.File.Dir, new, 1)
}
// NewRootConfig will return the root configuration
func NewRootConfig(path string, file os.FileInfo) *Root {
return &Root{ConfigFile: ConfigFile{File: &File{Path: path, Info: file, Dir: filepath.Dir(path)}}}
}