-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
176 lines (141 loc) · 3.79 KB
/
root.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
package boot
import (
"context"
"fmt"
bootflag "github.com/al8n/micro-boot/flag"
"google.golang.org/grpc"
"os"
"path/filepath"
"strings"
"sync"
)
var (
onceBoot sync.Once
root = boot{}
)
var reservedCmds = map[string]bool{
"start": true,
"stop": true,
"build": true,
}
func New(name string, server Booter, config Root) (bt Boot, err error) {
onceBoot.Do(func() {
err = constructRootCmd(name, server, config)
if err != nil {
return
}
constructDaemonCmd(name, config)
constructStopCmd(name, config)
})
if err != nil {
return nil, err
}
return root, nil
}
func constructStopCmd(name string, config Root) {
var stopCmd *Command
{
stopFlagSet := bootflag.NewFlagSet("stop", config.ErrorHandling)
stopFlagSet.BoolVarP(&ls.force, "force", "f", false, fmt.Sprintf("force to stop %s daemon", name))
stopCmd = newStopCmd(name, stopFlagSet, config.Stop, config.Options...)
root.cmd.Subcommands = append(root.cmd.Subcommands, stopCmd)
}
}
func constructDaemonCmd(name string, config Root) {
var (
startCmd, runCmd *Command
logFilePath, configFilePath string
)
{
logFilePath = logPath(defaultLogFileDir, name)
configFilePath = filepath.Join(
defaultConfigFileDirectory,
defaultConfigFileName + defaultConfigFileType)
daemonFlagSet := bootflag.NewFlagSet("daemon", config.ErrorHandling)
daemonFlagSet.StringVarP(
&root.logFile,
defaultLogFileFlag,
defaultLogFileFlagShortHand,
logFilePath,
fmt.Sprintf("%s (default is %s)", defaultLogFileFlagUsage, logFilePath))
daemonFlagSet.StringVarP(
&root.configFile,
defaultConfigFileFlag,
defaultConfigFileFlagShortHand,
configFilePath,
fmt.Sprintf("%s (default is %s)", defaultConfigFileFlagUsage, configFilePath))
daemonFlagSet.BoolVarP(
&ls.daemon,
"daemon",
"d",
false,
"run service in daemon mode")
startCmd = newStartCmd(name, daemonFlagSet, config.Start)
runCmd = newRunCmd(name, daemonFlagSet, config.Options...)
root.cmd.Subcommands = append(root.cmd.Subcommands, startCmd, runCmd)
}
}
func constructRootCmd(name string, server Booter, config Root) (err error) {
var (
dialOpts = []grpc.DialOption{grpc.WithInsecure()}
subcommands []*Command
execFn = defaultRootExec
fs = bootflag.NewFlagSet(name, config.ErrorHandling)
shortUsage = fmt.Sprintf("%s [flags] <subcommand>", name)
shortHelp, longHelp string
options = []Option{WithEnvVarNoPrefix(), WithAllowMissingConfigFile(true), WithConfigFile(root.configFile), WithConfigFileParser(configParser)}
)
{
if strings.TrimSpace(config.LongHelp) != "" {
longHelp = config.LongHelp
}
if strings.TrimSpace(config.ShortHelp) != "" {
shortHelp = config.ShortHelp
}
if strings.TrimSpace(config.ShortUsage) != "" {
shortUsage = config.ShortUsage
}
if len(config.Options) != 0 {
options = config.Options
}
if config.Exec != nil {
execFn = config.Exec
}
if len(config.LocalRPCDialOptions) > 0 {
dialOpts = config.LocalRPCDialOptions
}
if len(config.CustomCommands) > 0 {
for _, command := range config.CustomCommands {
if _, ok := reservedCmds[strings.TrimSpace(command.Name)]; ok {
err = fmt.Errorf("%s is a reserved command name by micro boot", command.Name)
return err
}
subcommands = append(subcommands, command)
}
}
}
ls = &localServer{
name: name,
external: server,
serverOpts: config.LocalRPCServerOptions,
dialOpts: dialOpts,
gracefulSignal: make(chan os.Signal, 1),
forceSignal: make(chan os.Signal, 1),
daemon: false,
exiting: false,
}
root.cmd = &Command{
Name: name,
ShortUsage: shortUsage,
ShortHelp: shortHelp,
LongHelp: longHelp,
Options: options,
FlagSet: fs,
Subcommands: subcommands,
Exec: execFn,
}
return err
}
func defaultRootExec(ctx context.Context, args []string) (err error) {
return nil
}