This repository has been archived by the owner on Jan 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
init.go
137 lines (121 loc) · 2.67 KB
/
init.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
package main
import (
"fmt"
"html/template"
"os"
"path/filepath"
"github.com/urfave/cli"
)
type fileDetails struct {
templatefile string
}
var fileTemplates = map[string]*fileDetails{
".editorconfig": {
templatefile: "templates/editorconfig.tpl",
},
".gitignore": {
templatefile: "templates/gitignore.tpl",
},
".vscode/settings.json": {
templatefile: "templates/vscode-settings.tpl",
},
"Makefile": {
templatefile: "templates/makefile.tpl",
},
}
type templateContext struct {
GoPath string
GoPathRel string
Docker bool
Dep bool
}
func getTemplateContext(c *cli.Context) templateContext {
ctx := templateContext{}
ctx.GoPath = os.Getenv("GOPATH")
pwd, err := os.Getwd()
if err != nil {
die(err)
}
rel, err := filepath.Rel(pwd, ctx.GoPath)
if err != nil {
die(err)
}
ctx.GoPathRel = rel
ctx.Docker = c.Bool("docker")
ctx.Dep = c.Bool("dep")
return ctx
}
func createFile(c *cli.Context, outfile string, f *fileDetails) {
if _, err := os.Stat(outfile); err == nil {
if !c.Bool("force") {
// file exists, don't overwrite
fmt.Fprintln(os.Stderr, outfile, " - already exists, not overwriting")
return
}
}
b, err := Asset(f.templatefile)
if err != nil {
die(err)
}
d := filepath.Dir(outfile)
if d != "." {
err = os.MkdirAll(d, 0755)
if err != nil {
die(err)
}
}
tpl, err := template.New("t").Parse(string(b))
if err != nil {
die(err)
}
fmt.Fprintln(os.Stdout, outfile, " - creating")
w, err := os.Create(outfile)
if err != nil {
die(err)
}
err = tpl.Execute(w, getTemplateContext(c))
if err != nil {
die(err)
}
}
var commandInit = cli.Command{
Name: "init",
Aliases: []string{"i"},
Usage: "initialise scaffolding for this project",
UsageText: "go-make init [command options] [files...]",
Description: `[files...] is a list of one/more files to generate. If not specified,
all known files will be generated. By default, no existing files will
be overwritten - but you can force generation of files with -f`,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "force, f",
Usage: "overwrites existing files - use with care!",
},
cli.BoolFlag{
Name: "docker, d",
Usage: "include support for docker containers",
},
cli.BoolFlag{
Name: "dep",
Usage: "use 'dep' tool for vendoring",
},
},
Action: func(c *cli.Context) error {
args := c.Args()
if len(args) > 0 {
for _, outfile := range args {
f := fileTemplates[outfile]
if f == nil {
fmt.Fprintln(os.Stderr, "unknown output file:", outfile)
os.Exit(1)
}
createFile(c, outfile, f)
}
} else {
for outfile, f := range fileTemplates {
createFile(c, outfile, f)
}
}
return nil
},
}