forked from tcnksm/gcli
/
apply.go
192 lines (157 loc) · 5.11 KB
/
apply.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
package command
import (
"fmt"
"os"
"strings"
"github.com/BurntSushi/toml"
"github.com/tcnksm/gcli/skeleton"
)
// ApplyCommand is a Command that generates a new cli project
type ApplyCommand struct {
Meta
}
// Run generates a new cli project. It returns exit code
func (c *ApplyCommand) Run(args []string) int {
var (
frameworkStr string
skipTest bool
verbose bool
owner string
name string
)
uflag := c.Meta.NewFlagSet("apply", c.Help())
uflag.StringVar(&frameworkStr, "framework", "", "framework")
uflag.StringVar(&frameworkStr, "F", "", "framework (short)")
uflag.BoolVar(&skipTest, "skip-test", false, "skip-test")
uflag.BoolVar(&skipTest, "T", false, "skip-test (short)")
uflag.BoolVar(&verbose, "verbose", false, "verbose")
uflag.BoolVar(&verbose, "V", false, "verbose (short)")
// These flags are supposed only to use in test
uflag.StringVar(&owner, "owner", "", "owner (Should only for test)")
uflag.StringVar(&name, "name", "", "name (Should only for test)")
if err := uflag.Parse(args); err != nil {
return 1
}
parsedArgs := uflag.Args()
if len(parsedArgs) != 1 {
c.UI.Error("Invalid argument: Usage glic apply [options] FILE")
return 1
}
designFile := parsedArgs[0]
c.UI.Info(fmt.Sprintf(
"Use design template %q for generating new cli project", designFile))
// Check file is exist or not
if _, err := os.Stat(designFile); os.IsNotExist(err) {
c.UI.Error(fmt.Sprintf(
"Design file does not exsit"))
return 1
}
// Decode design file as skeleton.Executable
executable := skeleton.NewExecutable()
if _, err := toml.DecodeFile(designFile, executable); err != nil {
c.UI.Error(fmt.Sprintf(
"Failed to decode design file %q: %s", designFile, err))
return 1
}
if err := executable.Fix(); err != nil {
c.UI.Error(fmt.Sprintf(
"Failed to fix input value: %s", err))
return 1
}
// validate executable
if errs := executable.Validate(); len(errs) > 0 {
c.UI.Error(fmt.Sprintf(
"%q is not valid template file. It has %d errors:", designFile, len(errs)))
for _, err := range errs {
c.UI.Error(fmt.Sprintf(
" * %s", err.Error()))
}
return ExitCodeFailed
}
output := executable.Name
if _, err := os.Stat(output); !os.IsNotExist(err) {
msg := fmt.Sprintf("Cannot create directory %s: file exists", output)
c.UI.Error(msg)
return 1
}
// Check option input first and if it's specified use it
if len(frameworkStr) == 0 {
if len(executable.FrameworkStr) != 0 {
// If FrameworStr is specified from design file use it
frameworkStr = executable.FrameworkStr
} else {
frameworkStr = defaultFrameworkString
}
}
fmt.Println(frameworkStr)
framework, err := skeleton.FrameworkByName(frameworkStr)
if err != nil {
c.UI.Error(fmt.Sprintf("Failed to generate %q: %s", executable.Name, err.Error()))
return 1
}
if len(name) != 0 {
executable.Name = name
output = name
}
if len(owner) != 0 {
executable.Owner = owner
}
// Channels to receive artifact path (result) and error
artifactCh, errCh := make(chan string), make(chan error)
// Define Skeleton
skeleton := &skeleton.Skeleton{
Path: output,
Framework: framework,
SkipTest: skipTest,
Executable: executable,
ArtifactCh: artifactCh,
ErrCh: errCh,
Verbose: verbose,
LogWriter: os.Stdout,
}
// Create project directory
doneCh := skeleton.Generate()
for {
select {
case artifact := <-artifactCh:
c.UI.Output(fmt.Sprintf(" Created %s", artifact))
case err := <-errCh:
c.UI.Error(fmt.Sprintf("Failed to generate %s: %s", output, err.Error()))
// If some file are created before error happend
// Should be cleanuped
if _, err := os.Stat(output); !os.IsNotExist(err) {
c.UI.Output(fmt.Sprintf("Cleanup %s", output))
os.RemoveAll(output)
}
return ExitCodeFailed
case <-doneCh:
c.UI.Info(fmt.Sprintf("====> Successfully generated %s", executable.Name))
return ExitCodeOK
}
}
}
// Synopsis is a one-line, short synopsis of the command.
func (c *ApplyCommand) Synopsis() string {
return "Apply design template file for generating cli project"
}
// Help is a long-form help text that includes the command-line
// usage, a brief few sentences explaining the function of the command,
// and the complete list of flags the command accepts.
func (c *ApplyCommand) Help() string {
helpText := `
Apply design template file for generating cli project. You can generate
design template file via 'gcli design' command. If framework name is not
specified gcli use codegangsta/cli. You can set framework name via '-F'
option. To check cli framework you can use, run 'gcli list'.
Usage:
gcli apply [option] FILE
Options:
-framework=name, -F Cli framework name. By default, gcli use "codegangsta/cli"
To check cli framework you can use, run 'gcli list'.
If you set invalid framework, it will be failed.
-skip-test, -T Skip generating *_test.go file. By default, gcli generates
test file If you specify this flag, gcli will not generate
test files.
`
return strings.TrimSpace(helpText)
}