-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbash-init.go
179 lines (138 loc) · 3.68 KB
/
bash-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
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
package main
import (
"fmt"
flag "github.com/dotcloud/docker/pkg/mflag"
"log"
"os"
"strings"
"text/template"
)
var mainTemplate = template.Must(parseAsset("main", "templates/main.tmpl"))
var readmeTemplate = template.Must(parseAsset("readme", "templates/README.tmpl"))
var readmeMd = Source{
Name: "README.md",
Template: *readmeTemplate,
}
type application struct {
Name, Author, Email string
HasSubCommand bool
SubCommands []subCommand
}
type subCommand struct {
Name string
}
func parseAsset(name string, path string) (*template.Template, error) {
src, err := Asset(path)
if err != nil {
return nil, err
}
return template.New(name).Parse(string(src))
}
func defineApplication(appName string, inputSubCommands []string) application {
hasSubCommand := false
if inputSubCommands[0] != "" {
hasSubCommand = true
}
return application{
Name: appName,
Author: GitConfig("user.name"),
Email: GitConfig("user.email"),
HasSubCommand: hasSubCommand,
SubCommands: defineSubCommands(inputSubCommands),
}
}
func defineSubCommands(inputSubCommands []string) []subCommand {
var subCommands []subCommand
if inputSubCommands[0] == "" {
return subCommands
}
for _, name := range inputSubCommands {
subCommand := subCommand{
Name: name,
}
subCommands = append(subCommands, subCommand)
}
return subCommands
}
func debug(v ...interface{}) {
if os.Getenv("DEBUG") != "" {
log.Println(v...)
}
}
func assert(err error) {
if err != nil {
log.Fatal(err)
}
}
func showVersion() {
fmt.Fprintf(os.Stderr, "cli-init v%s\n", Version)
}
func showHelp() {
fmt.Fprintf(os.Stderr, helpText)
}
func main() {
var (
flVersion = flag.Bool([]string{"v", "-version"}, false, "Print version information and quit")
flHelp = flag.Bool([]string{"h", "-help"}, false, "Print this message and quit")
flDebug = flag.Bool([]string{"-debug"}, false, "Run as DEBUG mode")
flSubCommands = flag.String([]string{"s", "-subcommands"}, "", "Conma-seplated list of sub-commands to build")
flReadme = flag.Bool([]string{"r", "-readme"}, false, "Include README.md file")
flForce = flag.Bool([]string{"f", "-force"}, false, "Overwrite application without prompting")
)
flag.Parse()
if *flHelp {
showHelp()
os.Exit(0)
}
if *flVersion {
showVersion()
os.Exit(0)
}
if *flDebug {
os.Setenv("DEBUG", "1")
debug("Run as DEBUG mode")
}
inputSubCommands := strings.Split(*flSubCommands, ",")
debug("inputSubCommands:", inputSubCommands)
appName := flag.Arg(0)
debug("appName:", appName)
if appName == "" {
fmt.Fprintf(os.Stderr, "Application name must not be blank\n")
os.Exit(1)
}
// Define Application
application := defineApplication(appName, inputSubCommands)
// Create <appName>.sh
mainSh := Source{
Name: appName + ".sh",
Template: *mainTemplate,
}
removed, err := mainSh.safeRemove(*flForce)
assert(err)
if removed {
err = mainSh.generate(application)
assert(err)
}
if *flReadme {
removed, err := readmeMd.safeRemove(*flForce)
assert(err)
if removed {
err = readmeMd.generate(application)
assert(err)
}
}
os.Exit(0)
}
const helpText = `Usage: bash-init [options] [application]
bash-init is the easy way to start building command-line app.
Options:
-s="", --subcommands="" Comma-separated list of sub-commands to build
-f, --force Overwrite application without prompting
-r, --readme Include README.md
-h, --help Print this message and quit
-v, --version Print version information and quit
--debug=false Run as DEBUG mode
Example:
$ bash-init todo
$ bash-init -s add,list,delete todo
`