/
app.go
93 lines (78 loc) · 2 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
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package extkingpin
import (
"fmt"
"os"
"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"
)
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')
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 string, 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 string, 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
}