-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
203 lines (170 loc) · 4.78 KB
/
main.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
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
package main
import (
"errors"
"flag"
"fmt"
"io"
"log"
"os"
"os/user"
"path/filepath"
"regexp"
"strconv"
"text/template"
)
type Context struct {
Env map[string]string
}
func main() {
configFilePath := getConfigFilePath()
config, err := readConfig(configFilePath)
checkExitError(err)
ctx, err := newContext(config)
checkExitError(err)
err = generateTemplates(config.Templates, ctx)
checkExitError(err)
}
func checkExitError(err error) {
if err != nil {
log.Fatal(err.Error())
os.Exit(1)
}
}
// creates a new context and initializes it with the Env vars defined in the configuration
func newContext(config *Config) (*Context, error) {
ctx := &Context{Env: make(map[string]string)}
for _, envVarName := range config.Envvars {
value := os.Getenv(envVarName)
if value != "" {
//log.Printf("Env var %s found with value '%s'", envVarName, value)
ctx.Env[envVarName] = value
} else {
return nil, errors.New(fmt.Sprintf("Env var %s not defined!", envVarName))
}
}
return ctx, nil
}
// reads the flag and returns config file path
func getConfigFilePath() string {
var configFile string
flag.StringVar(&configFile, "config", "./config.json", "JSON config file path")
flag.Parse()
log.Printf("Using config file %s", configFile)
return configFile
}
func tplFuncTest() string {
return "testoutput"
}
func generateTemplates(templates []Template, ctx *Context) error {
for _, template := range templates {
err := handleTemplate(template, ctx)
if err != nil {
return err
}
}
return nil
}
func handleTemplate(template Template, ctx *Context) error {
var err error
dest := os.Stdout //TODO: add proper way to print templates to stdout
if template.DestPath != "" {
createFolderStructure(template.DestPath)
dest, err = os.Create(template.DestPath)
if err != nil {
return fmt.Errorf("could not create destination file %s, error: %v", template.DestPath, err)
}
defer dest.Close()
}
if err = writeTemplate(template.TemplatePath, dest, ctx); err != nil {
return err
}
if err = handlePermission(dest, template); err != nil {
return err
}
if err = handleOwnerAndGroup(dest, template); err != nil {
return err
}
return nil
}
// writes the given template to the target destination
func writeTemplate(templatePath string, writer io.Writer, ctx *Context) error {
tmpl, err := template.New(filepath.Base(templatePath)).Funcs(template.FuncMap{
"test": tplFuncTest,
}).ParseFiles(templatePath)
if err != nil {
return fmt.Errorf("Failed to prepare template %s, error: %v", templatePath, err)
}
err = tmpl.Execute(writer, &ctx)
if err != nil {
return fmt.Errorf("Failed to generate template %s, error: %v", templatePath, err)
}
return nil
}
func handlePermission(file *os.File, template Template) error {
if template.FileMode != "" {
if !isValidFileMode(template.FileMode) {
return fmt.Errorf("Invalid file mode %s for template %s", template.FileMode, template.DestPath)
}
if value, err := strconv.ParseUint(template.FileMode, 0, 32); err == nil {
fileMode := os.FileMode(value)
err := os.Chmod(file.Name(), fileMode)
if err != nil {
return fmt.Errorf("Chmod with mode %v failed for template destPath %s, error: %v", fileMode,
file.Name(), err)
}
}
}
return nil
}
func handleOwnerAndGroup(file *os.File, template Template) error {
uid := os.Geteuid()
gid := os.Getegid()
if template.Owner != "" {
if isNumber(template.Owner) {
uid, _ = strconv.Atoi(template.Owner)
} else {
owner, err := user.Lookup(template.Owner)
if err == nil {
uid, _ = strconv.Atoi(owner.Uid)
} else {
return fmt.Errorf("User %s not found chown to curent user, error: %v", template.Owner, err)
}
}
}
if template.Group != "" {
if isNumber(template.Group) {
gid, _ = strconv.Atoi(template.Group)
} else {
group, err := LookupGroupByName(template.Group)
if err == nil {
gid, _ = strconv.Atoi(group.Gid)
} else {
return fmt.Errorf("Group %s not found chown to curent users primary group, error: %v", template.Group, err)
}
}
}
err := file.Chown(uid, gid)
if err != nil {
return fmt.Errorf("Chown with uid %d and gid %d failed for template destPath %s, error: %v", uid, gid, template.DestPath, err)
}
return nil
}
// creates the parent folder it it does not exist
func createFolderStructure(templatePath string) error {
path := filepath.Dir(templatePath)
if _, err := os.Stat(path); os.IsNotExist(err) {
err := os.MkdirAll(path, 0755) //TODO: use dir mode if available
if err != nil {
fmt.Errorf("Could not create dir structure for template destPath %s, error: %v", templatePath, err)
}
}
return nil
}
func isNumber(id string) bool {
_, err := strconv.Atoi(id)
return err == nil
}
func isValidFileMode(fileMode string) bool {
match, _ := regexp.MatchString("^[0-7]{4}$", fileMode)
return match
}