-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.go
176 lines (132 loc) · 4.56 KB
/
build.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
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
package main
import (
"fmt"
"github.com/codeskyblue/go-sh"
"github.com/fatih/color"
"io/ioutil"
"os"
"path/filepath"
)
// Save required diretory locations for methods
type builder struct {
destination string
templateSource string
envSource string
replaceMarker string
ignoreDirs []string
}
/**
* Saves the required configuration options
* @param {[type]} build *builder) Configure(destination, templateSource, envSource string [description]
* @return {[type]} [description]
*/
func (build *builder) Configure(destination, templateSource, envSource, replaceMarker string) {
build.ignoreDirs = append(build.ignoreDirs, "partials")
build.replaceMarker = replaceMarker
// Ensure all required directory entries exist
directories := []string{destination, templateSource, envSource}
errCount := 0
for _, directory := range directories {
color.White(fmt.Sprintf("Checking if '%s' exists \r\n", directory))
file, err := exists(directory)
if file == false || err != nil {
color.Yellow(fmt.Sprintf("The file '%s' could not be found \r\n", directory))
errCount++
} else {
color.Green(fmt.Sprintf("Found '%s'", directory))
}
// Handle the destination directory for dynamic creation
if file == false && directory == destination {
color.White(fmt.Sprintf("Lets create '%s'", directory))
err = os.MkdirAll(destination, 0755)
if err != nil {
color.Red(fmt.Sprintf("Could not create directory '%s'", destination))
os.Exit(1)
}
color.Green(fmt.Sprintf("Successfully created '%s'", destination))
errCount--
}
}
// Cancel processing
if errCount > 0 {
color.Red(fmt.Sprint("We cannot continue without these directories"))
os.Exit(1)
}
build.destination = destination
build.templateSource = templateSource
build.envSource = envSource
}
/**
* Build the template files for the environment
* @param {[type]} build *builder) Build( [description]
* @return {[type]} [description]
*/
func (build *builder) Build() {
color.White("Checking for template files")
// Set variables to point to folders
templateFiles, _ := ioutil.ReadDir(build.templateSource)
envFiles, _ := ioutil.ReadDir(build.envSource)
build.BuildTemplatesFromFolder(templateFiles, build.templateSource, "")
build.BuildTemplatesFromFolder(envFiles, build.envSource, "")
}
/**
* [func description]
* @param {[type]} build *builder) func BuildTemplatesFromFolder(templateFiles []os.FileInfo [description]
* @return {[type]} [description]
*/
func (build *builder) BuildTemplatesFromFolder(templateFiles []os.FileInfo, baseDir, subDir string) {
fileLoop:
// Loop through template files for processing
for _, file := range templateFiles {
// If file is dir, recurse function
if file.IsDir() {
for _, ignore := range build.ignoreDirs {
if file.Name() == ignore {
continue fileLoop
}
}
// Create the directory
err = os.MkdirAll(build.destination+subDir+file.Name(), 0755)
if err != nil {
color.Red(fmt.Sprintf("Could not create directory '%s'", build.destination+subDir+file.Name()))
os.Exit(1)
}
templateFiles, _ := ioutil.ReadDir(baseDir + subDir + file.Name())
build.BuildTemplatesFromFolder(templateFiles, baseDir, "/"+file.Name())
continue
}
// Skip if file is not a .yaml
if filepath.Ext(file.Name()) != ".yaml" {
color.Yellow(fmt.Sprintf("File %s is not a template", file.Name()))
continue
}
// Copy template file to dist
filename := baseDir + subDir + file.Name()
distFilename := build.destination + file.Name()
// err := CopyFile(filename, build.destination)
msg, err := sh.Command("cp", "-rfv", filename, build.destination).Output()
if err != nil {
color.Yellow(fmt.Sprintf("Could not copy %s to dist folder, %s", filename, err))
continue
} else {
color.Green(fmt.Sprintf("%s", msg))
}
// Translate the template file
parseResult, err := build.ParseTemplate(distFilename)
if err != nil {
color.Yellow(fmt.Sprintf("%s", err.Error()))
}
color.White(fmt.Sprintf("%s", parseResult))
}
}
/**
* Get a template file and translate any content using environment files
* @param {[type]} template os.FileInfo [description]
* @param {[type]} environmentDir string) (contents string, err error [description]
*/
func (build *builder) ParseTemplate(filename string) (output string, err error) {
msg, err := sh.Command("python2.7", "yamlthingy.py", "--templatefile", filename, "--marker", build.replaceMarker, "--envdir", build.envSource+"partials/").Output()
// Convert byte array to string
output = string(msg[:])
return output, err
}