/
gootstrap.go
262 lines (228 loc) · 6.88 KB
/
gootstrap.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
//Gootstrap is a simple package that bootstrap new Go packages. It creates a REAME.md, a doc.go and main (package name).go file as a placeholder. I did it because I was repeating myself a lot when starting new projects.
package main
import (
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"strings"
"time"
"github.com/hgsigner/stringfy"
)
var knownSubcommands = []string{"--minimal", "--light", "--no", "--template"}
// Runs the program.
func run(args []string, out io.Writer) {
switch len(args) {
case 1:
fmt.Fprintln(out, "===> Not enough arguments. Try goootstrap new project_name")
return
case 2:
fmt.Fprintln(out, "===> You should set the name of your package. Try goootstrap new project_name")
return
default:
runCommand(args, out)
}
}
// Runs to program based on the command passed.
func runCommand(args []string, out io.Writer) {
// Inits the command and the pack_name vars and
// tests if there is any subcommand passed as
// argument.
command := args[1]
pack_name := args[2]
subcommand, isSubcKnown := func(args []string) (string, bool) {
if len(args) > 3 {
for _, value := range knownSubcommands {
v := findMatch(value, args[3])
if v != "" {
return args[3], true
}
}
return args[3], false
}
return "", true
}(args)
switch command {
case "new":
// If the subcommand is known, it will
// pass it along, if not, it will print an error message.
if !isSubcKnown {
fmt.Fprintf(out, "===> Subcommand %s unknown. Try typing one of the following: %s\n", subcommand, strings.Join(knownSubcommands, ", "))
} else {
fmt.Fprintf(out, "===> Creating package %s\n", pack_name)
if subcommand != "--template" {
err := createDefaultPackage(pack_name, subcommand, out)
if err != nil {
fmt.Fprintf(out, "===> Error: %s\n", err)
return
}
} else {
// Checks if the template path was passed
if len(args) < 5 {
fmt.Fprintf(out, "===> You should pass the full path of the template file.\n")
return
}
// Everything is ok.
// Should create the package.
err := createTemplatePackage(pack_name, args[4], out)
if err != nil {
fmt.Fprintf(out, "===> Error: %s\n", err)
return
}
}
fmt.Fprintf(out, "===> Package created! cd %s to access.\n", pack_name)
}
default:
fmt.Fprintf(out, "===> Command %s unknown. Try typing the command 'new' instead.\n", command)
}
}
func main() {
run(os.Args, os.Stdout)
}
// Creates the package with files in it
func createDefaultPackage(pack_name, subcommand string, out io.Writer) error {
sep := string(filepath.Separator)
// Creates the project's folder
createFolder(pack_name, out)
// Init files
currentYear, currentMonth, currentDay := time.Now().Date()
currentDate := fmt.Sprintf("%d-%d-%d", currentYear, currentMonth, currentDay)
user, _ := user.Current()
files := filesList{
{
anchor: "gitignore",
fileName: fmt.Sprintf("%s%s.gitignore", pack_name, sep),
template: GitIgnoreFile{},
okMessage: "===> Creating .gitignore file",
output: out,
subcommand: subcommand,
},
{
anchor: "travis",
fileName: fmt.Sprintf("%s%s.travis.yml", pack_name, sep),
template: TravisFile{},
okMessage: "===> Creating .travis.yml file",
output: out,
subcommand: subcommand,
},
{
anchor: "license",
fileName: fmt.Sprintf("%s%sLICENSE.txt", pack_name, sep),
template: LicenseFile{currentYear, user.Name},
okMessage: "===> Creating LICENSE.txt file",
output: out,
subcommand: subcommand,
},
{
anchor: "readme",
fileName: fmt.Sprintf("%s%sREADME.md", pack_name, sep),
template: ReadmeFile{stringfy.CamelCase(pack_name), pack_name},
okMessage: "===> Creating README.md file",
output: out,
subcommand: subcommand,
},
{
anchor: "main",
fileName: fmt.Sprintf("%s%s%s.go", pack_name, sep, pack_name),
template: MainFile{pack_name},
okMessage: fmt.Sprintf("===> Creating %s.go file", pack_name),
output: out,
subcommand: subcommand,
},
{
anchor: "test",
fileName: fmt.Sprintf("%s%s%s_test.go", pack_name, sep, pack_name),
template: MainTestFile{pack_name},
okMessage: fmt.Sprintf("===> Creating %s_test.go file", pack_name),
output: out,
subcommand: subcommand,
},
{
anchor: "doc",
fileName: fmt.Sprintf("%s%sdoc.go", pack_name, sep),
template: DocFile{pack_name},
okMessage: "===> Creating doc.go file",
output: out,
subcommand: subcommand,
},
{
anchor: "changelog",
fileName: fmt.Sprintf("%s%sCHANGELOG.md", pack_name, sep),
template: ChangelogFile{currentDate},
okMessage: "===> Creating CHANGELOG.md file",
output: out,
subcommand: subcommand,
},
}
err := files.Process()
if err != nil {
return err
}
return nil
}
// Creates the package with files in it
func createTemplatePackage(packName, templPath string, out io.Writer) error {
sep := string(filepath.Separator)
// Inits a new instance of the toml parsed template
tomlTempl, err := NewTomlTemplate(templPath)
if err != nil {
return err
}
// Creates the project's folder
createFolder(packName, out)
// Loops through the template and creates the
// folders and files for the folders
files := make(filesList, 0)
for _, dir := range tomlTempl.Directories {
createFolder(packName+sep+dir.Name, out)
for _, fl := range dir.Files {
filepath := packName + sep + dir.Name + sep
gf := createCustomGootFile(packName, fl.Name, fl.Template, filepath, out)
files = append(files, gf)
}
}
// Creates files in the root directory
for _, fl := range tomlTempl.Files {
filepath := packName + sep
gf := createCustomGootFile(packName, fl.Name, fl.Template, filepath, out)
files = append(files, gf)
}
// Processes the files
err = files.Process()
if err != nil {
return err
}
return nil
}
// Creates the folders.
// Its a helper function.
func createFolder(folderName string, out io.Writer) {
if _, err := os.Stat(folderName); os.IsNotExist(err) {
os.Mkdir(folderName, 0777)
fmt.Fprintf(out, "===> Creating directory %s\n", folderName)
}
}
// Creates the custom gootFiles.
// Its a helper function.
func createCustomGootFile(packName, fileName, template, filepath string, out io.Writer) gootFile {
currentYear, currentMonth, currentDay := time.Now().Date()
currentDate := fmt.Sprintf("%d-%d-%d", currentYear, currentMonth, currentDay)
user, _ := user.Current()
fileNameTempl := CustomTemplate{PackageName: packName, Template: fileName}
filename := filepath + fileNameTempl.Parse()
gf := gootFile{
fileName: filename,
template: CustomTemplate{
PackageName: packName,
HumanizedPackageName: stringfy.CamelCase(packName),
CurrentYear: currentYear,
UserName: user.Name,
Date: currentDate,
Template: template,
},
okMessage: fmt.Sprintf("===> Creating %s file", filename),
output: out,
}
return gf
}