/
usage.go
199 lines (178 loc) · 5.36 KB
/
usage.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
package jiracli
import (
"fmt"
"os"
"os/exec"
"regexp"
"runtime"
"syscall"
"github.com/coryb/figtree"
"github.com/coryb/kingpeon"
"github.com/coryb/oreo"
jira "github.com/go-jira/jira"
kingpin "gopkg.in/alecthomas/kingpin.v2"
)
var usage = `{{define "FormatCommand"}}\
{{if .FlagSummary}} {{.FlagSummary}}{{end}}\
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\
{{end}}\
{{define "FormatBriefCommands"}}\
{{range .FlattenedCommands}}\
{{if not .Hidden}}\
{{ print .FullCommand ":" | printf "%-20s"}} {{.Help}}
{{end}}\
{{end}}\
{{end}}\
{{define "FormatCommands"}}\
{{range .FlattenedCommands}}\
{{if not .Hidden}}\
{{.FullCommand}}{{if .Default}}*{{end}}{{template "FormatCommand" .}}
{{.Help|Wrap 4}}
{{with .Flags|FlagsToTwoColumns}}{{FormatTwoColumnsWithIndent . 4 2}}{{end}}
{{end}}\
{{end}}\
{{end}}\
{{define "FormatUsage"}}\
{{template "FormatCommand" .}}{{if .Commands}} <command> [<args> ...]{{end}}
{{if .Help}}
{{.Help|Wrap 0}}\
{{end}}\
{{end}}\
{{if .Context.SelectedCommand}}\
usage: {{.App.Name}} {{.Context.SelectedCommand}}{{template "FormatCommand" .Context.SelectedCommand}}
{{if .Context.SelectedCommand.Aliases }}\
{{range $top := .App.Commands}}\
{{if eq $top.FullCommand $.Context.SelectedCommand.FullCommand}}\
{{range $alias := $.Context.SelectedCommand.Aliases}}\
alias: {{$.App.Name}} {{$alias}}{{template "FormatCommand" $.Context.SelectedCommand}}
{{end}}\
{{else}}\
{{range $sub := $top.Commands}}\
{{if eq $sub.FullCommand $.Context.SelectedCommand.FullCommand}}\
{{range $alias := $.Context.SelectedCommand.Aliases}}\
alias: {{$.App.Name}} {{$top.Name}} {{$alias}}{{template "FormatCommand" $.Context.SelectedCommand}}
{{end}}\
{{end}}\
{{end}}\
{{end}}\
{{end}}\
{{end}}
{{if .Context.SelectedCommand.Help}}\
{{.Context.SelectedCommand.Help|Wrap 0}}
{{end}}\
{{else}}\
usage: {{.App.Name}}{{template "FormatUsage" .App}}
{{end}}\
{{if .App.Flags}}\
Global flags:
{{.App.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if and .Context.SelectedCommand.Flags|RequiredFlags}}\
Required flags:
{{.Context.SelectedCommand.Flags|RequiredFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand.Flags|OptionalFlags}}\
Optional flags:
{{.Context.SelectedCommand.Flags|OptionalFlags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{end}}\
{{if .Context.Args}}\
Args:
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if .Context.SelectedCommand.Commands}}\
Subcommands:
{{template "FormatCommands" .Context.SelectedCommand}}
{{end}}\
{{else if .App.Commands}}\
Commands:
{{template "FormatBriefCommands" .App}}
{{end}}\
`
func CommandLine(fig *figtree.FigTree, o *oreo.Client) *kingpin.Application {
app := kingpin.New("jira", "Jira Command Line Interface")
app.HelpFlag.Short('h')
app.UsageWriter(os.Stdout)
app.ErrorWriter(os.Stderr)
app.Command("version", "Prints version").PreAction(func(*kingpin.ParseContext) error {
fmt.Println(jira.VERSION)
panic(Exit{Code: 0})
})
app.UsageTemplate(usage)
var verbosity int
app.Flag("verbose", "Increase verbosity for debugging").Short('v').PreAction(func(_ *kingpin.ParseContext) error {
os.Setenv("JIRA_DEBUG", fmt.Sprintf("%d", verbosity))
IncreaseLogLevel(1)
return nil
}).CounterVar(&verbosity)
app.Terminate(func(status int) {
for _, arg := range os.Args {
if arg == "-h" || arg == "--help" || len(os.Args) == 1 {
panic(Exit{Code: 0})
}
}
panic(Exit{Code: 1})
})
register(app, o, fig)
// register custom commands
data := struct {
CustomCommands kingpeon.DynamicCommands `yaml:"custom-commands" json:"custom-commands"`
}{}
if err := fig.LoadAllConfigs("config.yml", &data); err != nil {
log.Errorf("%s", err)
panic(Exit{Code: 1})
}
if len(data.CustomCommands) > 0 {
runner := syscall.Exec
if runtime.GOOS == "windows" {
runner = func(binary string, cmd []string, env []string) error {
command := exec.Command(binary, cmd[1:]...)
command.Stdin = os.Stdin
command.Stdout = os.Stdout
command.Stderr = os.Stderr
command.Env = env
return command.Run()
}
}
tmp := map[string]interface{}{}
fig.LoadAllConfigs("config.yml", &tmp)
kingpeon.RegisterDynamicCommandsWithRunner(runner, app, data.CustomCommands, TemplateProcessor())
}
return app
}
func ParseCommandLine(app *kingpin.Application, args []string) {
// checking for default usage of `jira ISSUE-123` but need to allow
// for global options first like: `jira --user mothra ISSUE-123`
ctx, err := app.ParseContext(args)
if err != nil && ctx == nil {
// This is an internal kingpin usage error, duplicate options/commands
log.Fatalf("error: %s, ctx: %v", err, ctx)
}
if ctx != nil {
if ctx.SelectedCommand == nil {
next := ctx.Next()
if next != nil {
if ok, err := regexp.MatchString("^([A-Z]+-)?[0-9]+$", next.Value); err != nil {
log.Errorf("Invalid Regex: %s", err)
} else if ok {
// insert "view" at i=1 (2nd position)
os.Args = append(os.Args[:1], append([]string{"view"}, os.Args[1:]...)...)
}
}
}
}
if _, err := app.Parse(os.Args[1:]); err != nil {
if _, ok := err.(*Error); ok {
log.Errorf("%s", err)
panic(Exit{Code: 1})
}
ctx, _ := app.ParseContext(os.Args[1:])
if ctx != nil {
app.UsageForContext(ctx)
}
log.Errorf("Invalid Usage: %s", err)
panic(Exit{Code: 1})
}
}