Newer
Older
"github.com/tmrts/boilr/pkg/boilr"
"github.com/tmrts/boilr/pkg/template"
"github.com/tmrts/boilr/pkg/util/exit"
"github.com/tmrts/boilr/pkg/util/validate"
// TemplateInRegistry checks whether the given name exists in the template registry.
func TemplateInRegistry(name string) (bool, error) {
names, err := ListTemplates()
if err != nil {
return false, err
}
_, ok := names[name]
return ok, nil
}
// TODO add --use-cache flag to execute a template from previous answers to prompts
// Use contains the cli-command for using templates located in the local template registry.
Short: "Execute a project template in the given directory",
Run: func(cmd *cli.Command, args []string) {
MustValidateTemplateDir()
tmplName := args[0]
targetDir, err := filepath.Abs(args[1])
if err != nil {
exit.Fatal(fmt.Errorf("use: %s", err))
}
templateFound, err := TemplateInRegistry(tmplName)
if err != nil {
exit.Fatal(fmt.Errorf("Template %q couldn't be found in the template registry", tmplName))
}
}
tmpl, err := template.Get(tmplPath)
if shouldUseDefaults := GetBoolFlag(cmd, "use-defaults"); shouldUseDefaults {
tmpl.UseDefaultValues()
}
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
tmpDir, err := ioutil.TempDir("", "boilr-use-template")
if err != nil {
exit.Fatal(fmt.Errorf("use: %s", err))
}
defer os.RemoveAll(tmpDir)
if err := os.Mkdir(targetDir, 0744); err != nil {
if os.IsNotExist(err) {
exit.Fatal(fmt.Errorf("use: directory %q doesn't exist", filepath.Dir(targetDir)))
}
if !os.IsExist(err) {
exit.Fatal(fmt.Errorf("use: %s", err))
}
}
if err := tmpl.Execute(tmpDir); err != nil {
exit.Fatal(fmt.Errorf("use: %s", err))
}
// Complete the template execution transaction by copying the temporary dir to
// the target directory.
if err := filepath.Walk(tmpDir, func(fname string, info os.FileInfo, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(tmpDir, fname)
if err != nil {
return err
}
mirrorPath := filepath.Join(targetDir, relPath)
if info.IsDir() {
if err := os.Mkdir(mirrorPath, 0744); err != nil {
if !os.IsExist(err) {
return err
}
}
} else {
fi, err := os.Lstat(fname)
if err != nil {
return err
}
tmpf, err := os.Open(fname)
if err != nil {
return err
}
defer tmpf.Close()
f, err := os.OpenFile(mirrorPath, os.O_CREATE|os.O_WRONLY, fi.Mode())
if err != nil {
return err
}
defer f.Close()
if _, err := io.Copy(f, tmpf); err != nil {
return err
}
}
exit.OK("Successfully executed the project template %v in %v", tmplName, targetDir)