-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.go
109 lines (87 loc) · 2.11 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
package app
import (
"os"
"sort"
"github.com/swisscom/korp/autocompletion"
"github.com/swisscom/korp/pull"
"github.com/swisscom/korp/push"
"github.com/swisscom/korp/scan"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
version = "1.0.2"
)
type CliApp struct {
app *cli.App
}
func Create() *CliApp {
app := cli.NewApp()
addGlobaConfig(app)
addGlobalFlags(app)
addBefore(app)
addCommands(app)
lastConfig(app)
return &CliApp{
app: app,
}
}
// globaConfig - Add global configurations to CLI application
func addGlobaConfig(app *cli.App) {
app.Name = "korp"
app.Usage = "push images to a corporate registry based on Kubernetes yaml files"
app.Version = version
// app.UseShortOptionHandling = true // flag not found in this version?!
app.EnableBashCompletion = true
}
// addGlobalFlags - Add global flag to CLI application
func addGlobalFlags(app *cli.App) {
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "debug, d",
Usage: "switch on debug log output",
EnvVar: "KORP_GLOBAL_DEBUG",
},
cli.StringFlag{
Name: "config, c",
Usage: "Load configuration from `FILE`",
EnvVar: "KORP_GLOBAL_CONFIG",
FilePath: "./config",
},
}
}
// TODO to be completed
// addBefore - Add before-action to CLI application
func addBefore(app *cli.App) {
app.Before = func(c *cli.Context) error {
if c.Bool("debug") {
log.SetLevel(log.DebugLevel)
}
// TODO add config-file action
// try using https://github.com/kelseyhightower/envconfig
return nil
}
}
// addCommands - Add commands to CLI application
func addCommands(app *cli.App) {
app.Commands = []cli.Command{
*scan.BuildCommand(),
*pull.BuildCommand(),
*push.BuildCommand(),
*autocompletion.BuildCommand(),
}
}
// lastConfig - Add last configuration right before start CLI application
func lastConfig(app *cli.App) {
// sorting flags in help section
sort.Sort(cli.FlagsByName(app.Flags))
// sorting commands in help section
sort.Sort(cli.CommandsByName(app.Commands))
}
// Start - Execute CLI application
func (a *CliApp) Start() {
err := a.app.Run(os.Args)
if err != nil {
log.Error(err)
}
}