-
Notifications
You must be signed in to change notification settings - Fork 2
/
cli.go
205 lines (174 loc) · 4.35 KB
/
cli.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
package dewy
import (
"bytes"
"fmt"
"io"
"log"
"reflect"
"strings"
"github.com/hashicorp/logutils"
flags "github.com/jessevdk/go-flags"
)
const (
// ExitOK for exit code.
ExitOK int = 0
// ExitErr for exit code.
ExitErr int = 1
)
type cli struct {
env Env
command string
args []string
LogLevel string `long:"log-level" short:"l" arg:"(debug|info|warn|error)" description:"Level displayed as log"`
Interval int `long:"interval" arg:"seconds" short:"i" description:"The polling interval to the repository (default: 10)"`
Port string `long:"port" short:"p" description:"TCP port to listen"`
Registry string `long:"registry" description:"Registry for application"`
Notify string `long:"notify" description:"Notify for application"`
BeforeDeployHook string `long:"before-deploy-hook" description:"Command to execute before deploy"`
AfterDeployHook string `long:"after-deploy-hook" description:"Command to execute after deploy"`
Help bool `long:"help" short:"h" description:"show this help message and exit"`
Version bool `long:"version" short:"v" description:"prints the version number"`
}
// Env struct.
type Env struct {
Out, Err io.Writer
Args []string
Version string
Commit string
Date string
}
// RunCLI runs as cli.
func RunCLI(env Env) int {
cli := &cli{env: env, Interval: -1}
return cli.run()
}
func (c *cli) buildHelp(names []string) []string {
var help []string
t := reflect.TypeOf(cli{})
for _, name := range names {
f, ok := t.FieldByName(name)
if !ok {
continue
}
tag := f.Tag
if tag == "" {
continue
}
var o, a string
if a = tag.Get("arg"); a != "" {
a = fmt.Sprintf("=%s", a)
}
if s := tag.Get("short"); s != "" {
o = fmt.Sprintf("-%s, --%s%s", tag.Get("short"), tag.Get("long"), a)
} else {
o = fmt.Sprintf("--%s%s", tag.Get("long"), a)
}
desc := tag.Get("description")
if i := strings.Index(desc, "\n"); i >= 0 {
var buf bytes.Buffer
buf.WriteString(desc[:i+1])
desc = desc[i+1:]
const indent = " "
for {
if i = strings.Index(desc, "\n"); i >= 0 {
buf.WriteString(indent)
buf.WriteString(desc[:i+1])
desc = desc[i+1:]
continue
}
break
}
if len(desc) > 0 {
buf.WriteString(indent)
buf.WriteString(desc)
}
desc = buf.String()
}
help = append(help, fmt.Sprintf(" %-40s %s", o, desc))
}
return help
}
func (c *cli) showHelp() {
opts := strings.Join(c.buildHelp([]string{
"Config",
"Interval",
"Registry",
"Notify",
"Port",
"LogLevel",
"BeforeDeployHook",
"AfterDeployHook",
}), "\n")
help := `
Usage: dewy [--version] [--help] command <options>
Commands:
server Keep the app server up to date
assets Keep assets up to date
Options:
%s
`
fmt.Fprintf(c.env.Out, help, opts)
}
func (c *cli) run() int {
p := flags.NewParser(c, flags.PassDoubleDash)
args, err := p.ParseArgs(c.env.Args)
if err != nil || c.Help {
c.showHelp()
return ExitErr
}
if c.Version {
fmt.Fprintf(c.env.Err, "dewy version %s [%v, %v]\n", c.env.Version, c.env.Commit, c.env.Date)
return ExitOK
}
if len(args) == 0 || (args[0] != "server" && args[0] != "assets") {
fmt.Fprintf(c.env.Err, "Error: command is not available\n")
c.showHelp()
return ExitErr
}
if c.Interval < 0 {
c.Interval = 10
}
c.command = args[0]
if len(args) > 1 {
c.args = args[1:]
}
if c.LogLevel != "" {
c.LogLevel = strings.ToUpper(c.LogLevel)
} else {
c.LogLevel = "ERROR"
}
filter := &logutils.LevelFilter{
Levels: []logutils.LogLevel{"DEBUG", "INFO", "WARN", "ERROR"},
MinLevel: logutils.LogLevel(c.LogLevel),
Writer: c.env.Err,
}
log.SetOutput(filter)
conf := DefaultConfig()
if c.Registry == "" {
fmt.Fprintf(c.env.Err, "Error: --registry is not set\n")
c.showHelp()
return ExitErr
}
conf.Registry = c.Registry
conf.Notify = c.Notify
conf.BeforeDeployHook = c.BeforeDeployHook
conf.AfterDeployHook = c.AfterDeployHook
if c.command == "server" {
conf.Command = SERVER
conf.Starter = &StarterConfig{
ports: []string{c.Port},
command: c.args[0],
args: c.args[1:],
}
} else {
conf.Command = ASSETS
}
conf.OverrideWithEnv()
d, err := New(conf)
if err != nil {
fmt.Fprintf(c.env.Err, "Error: %s\n", err)
return ExitErr
}
d.Start(c.Interval)
return ExitOK
}