-
Notifications
You must be signed in to change notification settings - Fork 52
/
init.go
76 lines (61 loc) · 1.73 KB
/
init.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
65
66
67
68
69
70
71
72
73
74
75
76
package projectconfig
import (
"bytes"
"fmt"
"io/ioutil"
"path"
"text/template"
"github.com/commitdev/zero/internal/constants"
"github.com/commitdev/zero/internal/util"
"gopkg.in/yaml.v2"
)
const zeroProjectConfigTemplate = `
# Templated zero-project.yml file
name: {{.Name}}
shouldPushRepositories: {{.ShouldPushRepositories | printf "%v"}}
modules:
{{.Modules}}
`
var RootDir = "./"
func SetRootDir(dir string) {
RootDir = dir
}
// CreateProjectConfigFile extracts the required content for zero project config file then write to disk.
func CreateProjectConfigFile(dir string, projectName string, projectContext *ZeroProjectConfig) error {
content, err := getProjectFileContent(*projectContext)
if err != nil {
return err
}
writeErr := ioutil.WriteFile(path.Join(dir, projectName, constants.ZeroProjectYml), []byte(content), 0644)
if writeErr != nil {
return err
}
return nil
}
func getProjectFileContent(projectConfig ZeroProjectConfig) (string, error) {
var tplBuffer bytes.Buffer
tmpl, err := template.New("projectConfig").Parse(zeroProjectConfigTemplate)
if err != nil {
return "", err
}
if len(projectConfig.Modules) == 0 {
return "", fmt.Errorf("Invalid project config, expected config modules to be non-empty")
}
pConfigModules, err := yaml.Marshal(projectConfig.Modules)
if err != nil {
return "", err
}
t := struct {
Name string
ShouldPushRepositories bool
Modules string
}{
Name: projectConfig.Name,
ShouldPushRepositories: projectConfig.ShouldPushRepositories,
Modules: util.IndentString(string(pConfigModules), 2),
}
if err := tmpl.Execute(&tplBuffer, t); err != nil {
return "", err
}
return tplBuffer.String(), nil
}