-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
87 lines (71 loc) · 2.25 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
package main
import (
"flag"
"fmt"
"log"
"os"
"runtime"
"strings"
)
// writeFile writes a file of given name to the output dir for project
func writeFile(project, projectPath *string, file string, content []byte) {
path := fmt.Sprintf("%s/%s/%s", *projectPath, *project, file)
err := os.WriteFile(path, content, 0644)
if err != nil {
log.Fatalln(err)
}
}
// makeProject creates the stubbed files for a project
func makeProject(project, projectPath, builderType *string) {
var b []byte
// make dir from project name
path := fmt.Sprintf("%s/%s", *projectPath, *project)
err := os.MkdirAll(path, 0755)
// continue if dir already exists
if err != nil {
log.Fatalln(err)
}
// create empty main.go
writeFile(project, projectPath, "main.go", nil)
// create go.mod
version := strings.SplitN(strings.Replace(runtime.Version(), "go", "", 1), ".", 3)
b = []byte(fmt.Sprintf("module %s\n\ngo %s", *project, version[0]+"."+version[1]))
writeFile(project, projectPath, "go.mod", b)
// create Dockerfile
writeDocker(project, projectPath)
// create Make/Task File
writeBuildFIle(builderType, project, projectPath)
// create .gitignore with project name added
b = []byte(fmt.Sprintf("%s\n", *project))
writeFile(project, projectPath, ".gitignore", b)
}
// flagCheck will print usage if no flags are passed in CLI
// or project name is missing
func flagCheck(project *string) {
if flag.NFlag() == 0 || *project == "" {
flag.Usage()
os.Exit(1)
}
}
func main() {
// default output dir
var defaultOutputDir = "output"
// project flag
builderType := flag.String("b", "task", "The `build file type of Make or Task to use `[make|task]. (OPTIONAL)")
projectName := flag.String("n", "newproject", "The `name of the project` to template out. (REQUIRED)")
projectPath := flag.String("p", "./output", "The `path to create the files in`. (OPTIONAL)")
// parse inputted flags from CLI
flag.Parse()
// check minimum flags
flagCheck(projectName)
// if no project path specified create default output dir if missing
if *projectPath == "" {
// set projectPath to default
*projectPath = defaultOutputDir
err := os.Mkdir(defaultOutputDir, 0755)
if err != nil && !os.IsExist(err) {
log.Fatalln(err)
}
}
makeProject(projectName, projectPath, builderType)
}