-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
180 lines (139 loc) · 4.41 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
package main
import (
"fmt"
"os"
)
func main() {
var registry *TemplateRegistry
var settings RunSettings
var err error
registry = NewTemplateRegistry()
settings, err = FindRunSettings()
if err != nil {
exitWith("Unable to parse run arguments: %s\n", err, 1)
return
}
if(settings.flagList) {
for _, flag := range FLAGS {
fmt.Println(flag)
}
return
}
registry = NewTemplateRegistry()
// is the user showing the registry?
if settings.showRegistry {
showRegistry(registry)
return
}
// is the user trying to add to the current registry?
if settings.addRegistry {
addRegistry(settings, registry)
return
}
createProject(settings, registry)
}
func showRegistry(registry *TemplateRegistry) {
for _, template := range registry.GetTemplateList() {
fmt.Println(template)
}
}
func addRegistry(settings RunSettings, registry *TemplateRegistry) {
var name string
var err error
name, err = registry.RegisterTemplate(settings.skeletonPath, settings.basicAuthUser, settings.basicAuthPass)
if err != nil {
// TODO: I'm deeply uncomfortable with using "exitWith" outside of the actual
// main method. This is too easy to let "exiting" become a separate code path.
exitWith("Unable to register template: %s\n", err, 1)
}
fmt.Printf("Registered template '%s'\n", name)
return
}
func createProject(settings RunSettings, registry *TemplateRegistry) {
var project *TemplatedProject
var parameters []string
var err error
// is this a registry skeleton?
if registry.IsPathRegistry(settings.skeletonPath) && registry.Contains(settings.skeletonPath) {
settings.skeletonPath, err = registry.GetTemplatePath(settings.skeletonPath)
if err != nil {
exitWith("Unable to expand registered template: %s\n", err, 1)
return
}
}
// Create templated project, in preparation for later use
project, err = NewTemplatedProject(settings.skeletonPath)
if err != nil {
exitWith("Unable to read templated project: %s\n", err, 1)
return
}
// inspect only?
if settings.inspectionRun {
parameters, err = project.FindParameters()
if err != nil {
exitWith("Unable to inspect skeleton: %s\n", err, 1)
return
}
for _, parameter := range parameters {
fmt.Println(parameter)
}
return
}
// check to see if the destination exists
_, err = os.Stat(settings.targetPath)
if(err == nil) {
// if we force-generate, see which type of force generation to do (delete or overwrite)
switch settings.forceGenerate {
case GENERATE_NONE:
fmt.Println("Destination path already exists, and no '-f' option was specified. Use '-f' to overwrite existing files.")
return
case GENERATE_DELETE:
err = os.RemoveAll(settings.targetPath)
if(err != nil) {
exitWith("Unable to delete existing files in the given output directory: %s\n", err, 1)
return
}
default:
break
}
}
// generate a project
err = project.GenerateAt(settings.targetPath, settings.parameters)
if err != nil {
exitWith("Unable to generate project: %s\n", err, 1)
return
}
// if everything succeeded, but we had missing parameters, make a note of it to the user.
if project.missingParameters.Length() > 0 {
if(settings.blankParameters) {
// if blank parameters are allowed, print a message about how blank parameters were used.
fmt.Printf("Project generated, but some parameters were not specified, and have been left blank:\n")
} else {
// if blank parameters are not allowed, delete the generated project.
fmt.Printf("Project cannot be generated, some parameters were not specified:\n")
os.RemoveAll(settings.targetPath)
}
for _, value := range project.missingParameters.GetSlice() {
fmt.Println(value)
}
return
}
if project.incorrectParameters.Length() > 0 {
fmt.Println("Project generated, but some placeholders in the template were specified with a join character ('[]'), but the given parameter was not a list")
fmt.Println("This may mean you need to specify the following parameters as a comma-separated list")
for _, value := range project.incorrectParameters.GetSlice() {
fmt.Println(value)
}
}
// if there's a post-generate script (and it's executable), call it.
err = executePostGenerate(project.rootDirectory, settings.targetPath)
if err != nil {
exitWith("Unable to complete post-generation script: %s\n", err, 1)
return
}
}
func exitWith(message string, err error, code int) {
errorMsg := fmt.Sprintf(message, err)
fmt.Fprintf(os.Stderr, errorMsg)
os.Exit(code)
}