-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.go
170 lines (142 loc) · 3.43 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
package app
import (
"fmt"
"os"
"github.com/alauda/registry-auth/pkg/app/version"
"github.com/alauda/registry-auth/pkg/server"
"github.com/fatih/color"
"github.com/spf13/cobra"
)
var (
progressMessage = color.BlueString("==>")
)
type App struct {
name string
basename string
description string
options Options
runFunc RunFunc
silence bool
rootCmd *cobra.Command
server *server.Server
}
// Option defines optional parameters for initializing the application
// structure.
type Option func(*App)
// WithOptions to open the application's function to read from the command line
// or read parameters from the configuration file.
func WithOptions(opt Options) Option {
return func(a *App) {
a.options = opt
}
}
// RunFunc defines the application's startup callback function.
type RunFunc func(basename string) error
func WithRunFunc(run RunFunc) Option {
return func(a *App) {
a.runFunc = run
}
}
// WithDescription is used to set the description of the application.
func WithDescription(desc string) Option {
return func(a *App) {
a.description = desc
}
}
// WithSilence sets the application to silent mode, in which the program startup
// information, configuration information, and version information are not
// printed in the console.
func WithSilence() Option {
return func(a *App) {
a.silence = true
}
}
// NewApp creates a new application instance based on the given application name,
// binary name, and other options.
func NewApp(name string, basename string, opts ...Option) *App {
a := &App{
name: name,
basename: basename,
server: server.New(),
}
for _, o := range opts {
o(a)
}
a.initRootCmd()
return a
}
func (a *App) GetRootCmd() *cobra.Command {
return a.rootCmd
}
// initRootCmd init the cobra root command
func (a *App) initRootCmd() {
initFlag()
cmd := cobra.Command{
Use: a.basename,
Long: a.description,
SilenceUsage: true,
SilenceErrors: true,
}
cmd.SetOut(os.Stdout)
cmd.Flags().SortFlags = false
cmd.Run = a.runCommand
if a.options != nil {
if _, ok := a.options.(ConfigurableOptions); ok {
addConfigFlag(a.basename, cmd.Flags())
}
a.options.AddFlags(cmd.Flags())
}
version.AddFlags(cmd.Flags())
a.rootCmd = &cmd
}
// Run is used to launch the application.
func (a *App) Run() {
if err := a.rootCmd.Execute(); err != nil {
printError(err)
os.Exit(1)
}
}
func (a *App) runCommand(cmd *cobra.Command, args []string) {
version.PrintAndExitIfRequested()
if !a.silence {
fmt.Printf("%v Starting %s...\n", progressMessage, a.name)
}
// merge configuration and print it
if a.options != nil {
if configurableOptions, ok := a.options.(ConfigurableOptions); ok {
if errs := configurableOptions.ApplyFlags(); len(errs) > 0 {
for _, err := range errs {
printError(err)
}
os.Exit(1)
}
if !a.silence {
printConfig()
}
}
if ConfigurableOptions, ok := a.options.(ServerOptions); ok {
if err := ConfigurableOptions.ApplyToServer(a.server); err != nil {
printError(err)
os.Exit(1)
}
}
}
// run application
if a.runFunc != nil {
if !a.silence {
fmt.Printf("%v Log data will now stream in as it occurs:\n", progressMessage)
}
if err := a.runFunc(a.basename); err != nil {
printError(err)
os.Exit(1)
}
}
err := a.server.Start()
if err != nil {
printError(err)
os.Exit(1)
}
}
func printError(err error) {
fmt.Printf("%v %v\n", color.RedString("Error:"), err)
}