/
gcmd_command_run.go
251 lines (234 loc) · 6.6 KB
/
gcmd_command_run.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://github.com/wowqhb/gf.
//
package gcmd
import (
"bytes"
"context"
"fmt"
"os"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/propagation"
"go.opentelemetry.io/otel/trace"
"github.com/wowqhb/gf/v2"
"github.com/wowqhb/gf/v2/errors/gcode"
"github.com/wowqhb/gf/v2/errors/gerror"
"github.com/wowqhb/gf/v2/net/gtrace"
"github.com/wowqhb/gf/v2/os/gcfg"
"github.com/wowqhb/gf/v2/os/genv"
"github.com/wowqhb/gf/v2/os/glog"
"github.com/wowqhb/gf/v2/text/gstr"
"github.com/wowqhb/gf/v2/util/gconv"
"github.com/wowqhb/gf/v2/util/gutil"
)
// Run calls custom function that bound to this command.
// It exits this process with exit code 1 if any error occurs.
func (c *Command) Run(ctx context.Context) {
_ = c.RunWithValue(ctx)
}
// RunWithValue calls custom function that bound to this command with value output.
// It exits this process with exit code 1 if any error occurs.
func (c *Command) RunWithValue(ctx context.Context) (value interface{}) {
value, err := c.RunWithValueError(ctx)
if err != nil {
var (
code = gerror.Code(err)
detail = code.Detail()
buffer = bytes.NewBuffer(nil)
)
if code.Code() == gcode.CodeNotFound.Code() {
buffer.WriteString(fmt.Sprintf("ERROR: %s\n", gstr.Trim(err.Error())))
if lastCmd, ok := detail.(*Command); ok {
lastCmd.PrintTo(buffer)
} else {
c.PrintTo(buffer)
}
} else {
buffer.WriteString(fmt.Sprintf("%+v\n", err))
}
if gtrace.GetTraceID(ctx) == "" {
fmt.Println(buffer.String())
os.Exit(1)
}
glog.Stack(false).Fatal(ctx, buffer.String())
}
return value
}
// RunWithError calls custom function that bound to this command with error output.
func (c *Command) RunWithError(ctx context.Context) (err error) {
_, err = c.RunWithValueError(ctx)
return
}
// RunWithValueError calls custom function that bound to this command with value and error output.
func (c *Command) RunWithValueError(ctx context.Context) (value interface{}, err error) {
// Parse command arguments and options using default algorithm.
parser, err := Parse(nil)
if err != nil {
return nil, err
}
args := parser.GetArgAll()
if len(args) == 1 {
return c.doRun(ctx, parser)
}
// Exclude the root binary name.
args = args[1:]
// Find the matched command and run it.
lastCmd, foundCmd, newCtx := c.searchCommand(ctx, args)
if foundCmd != nil {
return foundCmd.doRun(newCtx, parser)
}
// Print error and help command if no command found.
err = gerror.NewCodef(
gcode.WithCode(gcode.CodeNotFound, lastCmd),
`command "%s" not found for command "%s", command line: %s`,
gstr.Join(args, " "),
c.Name,
gstr.Join(os.Args, " "),
)
return
}
func (c *Command) doRun(ctx context.Context, parser *Parser) (value interface{}, err error) {
defer func() {
if exception := recover(); exception != nil {
if v, ok := exception.(error); ok && gerror.HasStack(v) {
err = v
} else {
err = gerror.Newf(`exception recovered: %+v`, exception)
}
}
}()
ctx = context.WithValue(ctx, CtxKeyCommand, c)
// Check built-in help command.
if parser.GetOpt(helpOptionName) != nil || parser.GetOpt(helpOptionNameShort) != nil {
if c.HelpFunc != nil {
return nil, c.HelpFunc(ctx, parser)
}
return nil, c.defaultHelpFunc(ctx, parser)
}
// OpenTelemetry for command.
var (
span trace.Span
tr = otel.GetTracerProvider().Tracer(
tracingInstrumentName,
trace.WithInstrumentationVersion(gf.VERSION),
)
)
ctx, span = tr.Start(
otel.GetTextMapPropagator().Extract(
ctx,
propagation.MapCarrier(genv.Map()),
),
gstr.Join(os.Args, " "),
trace.WithSpanKind(trace.SpanKindServer),
)
defer span.End()
span.SetAttributes(gtrace.CommonLabels()...)
// Reparse the arguments for current command configuration.
parser, err = c.reParse(ctx, parser)
if err != nil {
return nil, err
}
// Registered command function calling.
if c.Func != nil {
return nil, c.Func(ctx, parser)
}
if c.FuncWithValue != nil {
return c.FuncWithValue(ctx, parser)
}
// If no function defined in current command, it then prints help.
if c.HelpFunc != nil {
return nil, c.HelpFunc(ctx, parser)
}
return nil, c.defaultHelpFunc(ctx, parser)
}
// reParse parses the arguments using option configuration of current command.
func (c *Command) reParse(ctx context.Context, parser *Parser) (*Parser, error) {
if len(c.Arguments) == 0 {
return parser, nil
}
var (
optionKey string
supportedOptions = make(map[string]bool)
)
for _, arg := range c.Arguments {
if arg.IsArg {
continue
}
if arg.Short != "" {
optionKey = fmt.Sprintf(`%s,%s`, arg.Name, arg.Short)
} else {
optionKey = arg.Name
}
supportedOptions[optionKey] = !arg.Orphan
}
parser, err := Parse(supportedOptions, ParserOption{
CaseSensitive: c.CaseSensitive,
Strict: c.Strict,
})
if err != nil {
return nil, err
}
// Retrieve option values from config component if it has "config" tag.
if c.Config != "" && gcfg.Instance().Available(ctx) {
value, err := gcfg.Instance().Get(ctx, c.Config)
if err != nil {
return nil, err
}
configMap := value.Map()
for optionName := range parser.supportedOptions {
// The command line has the high priority.
if parser.GetOpt(optionName) != nil {
continue
}
// Merge the config value into parser.
foundKey, foundValue := gutil.MapPossibleItemByKey(configMap, optionName)
if foundKey != "" {
parser.parsedOptions[optionName] = gconv.String(foundValue)
}
}
}
return parser, nil
}
// searchCommand recursively searches the command according given arguments.
func (c *Command) searchCommand(ctx context.Context, args []string) (lastCmd, foundCmd *Command, newCtx context.Context) {
if len(args) == 0 {
return c, nil, ctx
}
for _, cmd := range c.commands {
// Recursively searching the command.
if cmd.Name == args[0] {
leftArgs := args[1:]
// If this command needs argument,
// it then gives all its left arguments to it.
if cmd.hasArgumentFromIndex() {
ctx = context.WithValue(ctx, CtxKeyArguments, leftArgs)
return c, cmd, ctx
}
// Recursively searching.
if len(leftArgs) == 0 {
return c, cmd, ctx
}
return cmd.searchCommand(ctx, leftArgs)
}
}
return c, nil, ctx
}
func (c *Command) hasArgumentFromIndex() bool {
for _, arg := range c.Arguments {
if arg.IsArg {
return true
}
}
return false
}
func (c *Command) hasArgumentFromOption() bool {
for _, arg := range c.Arguments {
if !arg.IsArg {
return true
}
}
return false
}