-
Notifications
You must be signed in to change notification settings - Fork 2k
/
app.go
148 lines (128 loc) · 3.45 KB
/
app.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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package extkingpin
import (
"fmt"
"os"
"sort"
"text/template"
"github.com/go-kit/kit/log"
"github.com/oklog/run"
"github.com/opentracing/opentracing-go"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"gopkg.in/alecthomas/kingpin.v2"
)
const UsageTemplate = `{{define "FormatCommand"}}\
{{if .FlagSummary}} {{.FlagSummary}}{{end}}\
{{range .Args}} {{if not .Required}}[{{end}}<{{.Name}}>{{if .Value|IsCumulative}}...{{end}}{{if not .Required}}]{{end}}{{end}}\
{{end}}\
{{define "FormatCommands"}}\
{{range .FlattenedCommands}}\
{{if not .Hidden}}\
{{.FullCommand}}{{if .Default}}*{{end}}{{template "FormatCommand" .}}
{{.Help|Wrap 4}}
{{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 "FormatUsage" .Context.SelectedCommand}}
{{else}}\
usage: {{.App.Name}}{{template "FormatUsage" .App}}
{{end}}\
{{if .Context.Flags}}\
Flags:
{{alphabeticalSort .Context.Flags|FlagsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.Args}}\
Args:
{{.Context.Args|ArgsToTwoColumns|FormatTwoColumns}}
{{end}}\
{{if .Context.SelectedCommand}}\
{{if len .Context.SelectedCommand.Commands}}\
Subcommands:
{{template "FormatCommands" .Context.SelectedCommand}}
{{end}}\
{{else if .App.Commands}}\
Commands:
{{template "FormatCommands" .App}}
{{end}}\
`
type FlagClause interface {
Flag(name, help string) *kingpin.FlagClause
}
// TODO(bwplotka): Consider some extkingpin package that will not depend on those. Needed: Generics!
type SetupFunc func(*run.Group, log.Logger, *prometheus.Registry, opentracing.Tracer, <-chan struct{}, bool) error
type AppClause interface {
FlagClause
Command(cmd string, help string) AppClause
Flags() []*kingpin.FlagModel
Setup(s SetupFunc)
}
// App is a wrapper around kingping.Application for easier use.
type App struct {
FlagClause
app *kingpin.Application
setups map[string]SetupFunc
}
// NewApp returns new App.
func NewApp(app *kingpin.Application) *App {
app.HelpFlag.Short('h')
app.UsageTemplate(UsageTemplate)
app.UsageFuncs(template.FuncMap{
"alphabeticalSort": func(data []*kingpin.FlagModel) []*kingpin.FlagModel {
sort.Slice(data, func(i, j int) bool { return data[i].Name < data[j].Name })
return data
},
})
return &App{
app: app,
FlagClause: app,
setups: map[string]SetupFunc{},
}
}
func (a *App) Parse() (cmd string, setup SetupFunc) {
cmd, err := a.app.Parse(os.Args[1:])
if err != nil {
fmt.Fprintln(os.Stderr, errors.Wrapf(err, "error parsing commandline arguments: %v", os.Args))
a.app.Usage(os.Args[1:])
os.Exit(2)
}
return cmd, a.setups[cmd]
}
func (a *App) Command(cmd, help string) AppClause {
c := a.app.Command(cmd, help)
return &appClause{
c: c,
FlagClause: c,
setups: a.setups,
prefix: cmd,
}
}
type appClause struct {
c *kingpin.CmdClause
FlagClause
setups map[string]SetupFunc
prefix string
}
func (a *appClause) Command(cmd, help string) AppClause {
c := a.c.Command(cmd, help)
return &appClause{
c: c,
FlagClause: c,
setups: a.setups,
prefix: a.prefix + " " + cmd,
}
}
func (a *appClause) Setup(s SetupFunc) {
a.setups[a.prefix] = s
}
func (a *appClause) Flags() []*kingpin.FlagModel {
return a.c.Model().Flags
}