-
Notifications
You must be signed in to change notification settings - Fork 7
/
cmd.go
414 lines (359 loc) · 12.2 KB
/
cmd.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
package cmd
import (
"bytes"
"errors"
"fmt"
"io"
"log"
"os"
"os/exec"
"strings"
"github.com/cli/safeexec"
"github.com/reubenmiller/go-c8y-cli/v2/internal/run"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/activitylogger"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/alias/expand"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/factory"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd/root"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmderrors"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/cmdutil"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/config"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/console"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/dataview"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/encrypt"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/iterator"
"github.com/reubenmiller/go-c8y-cli/v2/pkg/logger"
"github.com/reubenmiller/go-c8y/pkg/c8y"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"go.uber.org/zap/zapcore"
)
// Logger is used to record the log messages which should be visible to the user when using the verbose flag
var Logger *logger.Logger
// Build data
// These variables should be set using the -ldflags "-X github.com/reubenmiller/go-c8y-cli/v2/pkg/cmd.version=1.0.0" when running go build
var buildVersion string
var buildBranch string
const (
module = "c8yapi"
)
func init() {
Logger = logger.NewLogger(module, logger.Options{})
// Enable case insensitive matches
cobra.EnableCaseInsensitive = true
}
// Execute runs the root command
func MainRun() {
rootCmd, err := Initialize()
if err != nil {
os.Exit(int(cmderrors.ExitError))
}
// Expand any aliases
expandedArgs, err := setArgs(rootCmd.Command, rootCmd.Factory)
if err != nil {
Logger.Errorf("Could not expand aliases. %s", err)
os.Exit(int(cmderrors.ExitInvalidAlias))
}
// Completions for aliases
rootCmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
v := viper.GetViper()
aliases := v.GetStringMapString(config.SettingsCommonAliases)
for key, value := range v.GetStringMapString(config.SettingsAliases) {
aliases[key] = value
}
var results []string
for aliasName, aliasValue := range aliases {
if strings.HasPrefix(aliasName, toComplete) {
var s string
if strings.HasPrefix(aliasValue, "!") {
s = fmt.Sprintf("%s\tShell alias", aliasName)
} else {
if len(aliasValue) > 80 {
aliasValue = aliasValue[:80] + "..."
}
s = fmt.Sprintf("%s\tAlias for %s", aliasName, aliasValue)
}
results = append(results, s)
}
}
// Extension Aliases
for _, ext := range rootCmd.Factory.ExtensionManager().List() {
extAliases, aliasErr := ext.Aliases()
if aliasErr == nil {
for _, iAlias := range extAliases {
aliasName := iAlias.GetName()
if strings.HasPrefix(aliasName, toComplete) {
var s string
desc := iAlias.GetDescription()
if len(desc) > 80 {
desc = desc[:80] + "..."
}
if iAlias.IsShell() {
s = fmt.Sprintf("%s\tExtension shell alias %s", aliasName, desc)
} else {
s = fmt.Sprintf("%s\tExtension alias to %s", aliasName, desc)
}
s += fmt.Sprintf(" |%s", ext.Name())
results = append(results, s)
}
}
}
}
// Note: Extension commands are defined in root.go
return results, cobra.ShellCompDirectiveNoFileComp
}
Logger.Debugf("Expanded args: %v", expandedArgs)
rootCmd.SetArgs(expandedArgs)
if err := rootCmd.Execute(); err != nil {
err = CheckCommandError(rootCmd.Command, rootCmd.Factory, err)
// Help is not really error, just a way to exit early
// after displaying help to the user
if errors.Is(err, cmderrors.ErrHelp) {
os.Exit(int(cmderrors.ExitOK))
}
if cErr, ok := err.(cmderrors.CommandError); ok {
os.Exit(int(cErr.ExitCode))
}
if errors.Is(err, cmderrors.ErrNoMatchesFound) {
os.Exit(int(cmderrors.ExitNotFound404))
}
os.Exit(int(cmderrors.ExitError))
}
}
func CheckCommandError(cmd *cobra.Command, f *cmdutil.Factory, err error) error {
cfg, configErr := f.Config()
if configErr != nil {
log.Fatalf("Could not load configuration. %s", configErr)
}
logg, logErr := f.Logger()
if logErr != nil {
log.Fatalf("Could not configure logger. %s", logErr)
}
w := io.Discard
if errors.Is(err, cmderrors.ErrHelp) {
return err
}
if iterator.IsEmptyPipeInputError(err) && !cfg.AllowEmptyPipe() {
// Ignore empty pipe errors
logg.Debug("detected empty piped data")
return cmderrors.NewUserErrorWithExitCode(cmderrors.ExitOK)
}
// read directly from flags, as unknown flag errors are thrown before the config is read
if localWithError, fErr := cmd.Flags().GetBool("withError"); localWithError && fErr == nil {
w = cmd.OutOrStdout()
} else if cfg != nil && cfg.WithError() {
w = cmd.OutOrStdout()
}
if errors.Is(err, cmderrors.ErrNoMatchesFound) {
// Simulate a 404 error
customErr := cmderrors.NewUserErrorWithExitCode(cmderrors.ExitNotFound404, err)
customErr.StatusCode = 404
err = customErr
}
if errors.Is(err, encrypt.ErrDecryptFailed) {
// Decryption error
customErr := cmderrors.NewUserErrorWithExitCode(cmderrors.ExitDecryption, err)
err = customErr
}
if cErr, ok := err.(cmderrors.CommandError); ok {
if cErr.StatusCode == 403 || cErr.StatusCode == 401 {
logg.Error(fmt.Sprintf("Authentication failed (statusCode=%d). Try to run set-session again, or check the password", cErr.StatusCode))
}
// format errors as json messages
// only log users errors
silentStatusCodes := ""
if cfg != nil {
silentStatusCodes = cfg.GetSilentStatusCodes()
}
if !cErr.IsSilent() && !strings.Contains(silentStatusCodes, fmt.Sprintf("%d", cErr.StatusCode)) {
if !cErr.Processed {
logg.Errorf("%s", cErr)
fmt.Fprintf(w, "%s\n", cErr.JSONString())
} else {
logg.Debugf("Error has already been logged. %s", cErr)
}
}
} else {
// unexpected error
cErr := cmderrors.NewSystemErrorF("%s", err)
logg.Errorf("%s", cErr)
fmt.Fprintf(w, "%s\n", cErr.JSONString())
}
return err
}
func hasCommand(rootCmd *cobra.Command, args []string) bool {
c, _, err := rootCmd.Traverse(args)
return err == nil && c != rootCmd
}
func setArgs(cmd *cobra.Command, cmdFactory *cmdutil.Factory) ([]string, error) {
expandedArgs := []string{}
var err error
if len(os.Args) > 0 {
expandedArgs = os.Args[1:]
}
if !hasCommand(cmd.Root(), expandedArgs) {
originalArgs := expandedArgs
isShell := false
v := viper.GetViper()
aliases := v.GetStringMapString(config.SettingsCommonAliases)
for name, value := range v.GetStringMapString(config.SettingsAliases) {
aliases[name] = value
}
// add any aliases defined in the extensions
for _, ext := range cmdFactory.ExtensionManager().List() {
if extAliases, err := ext.Aliases(); err == nil {
for _, alias := range extAliases {
aliases[alias.GetName()] = alias.GetCommand()
}
}
}
expandedArgs, isShell, err = expand.ExpandAlias(aliases, os.Args, nil)
if err != nil {
return nil, err
}
Logger.Debugf("%v -> %v", originalArgs, expandedArgs)
if isShell {
exe, err := safeexec.LookPath(expandedArgs[0])
if err != nil {
return nil, err
}
externalCmd := exec.Command(exe, expandedArgs[1:]...)
externalCmd.Stderr = os.Stderr
externalCmd.Stdout = os.Stdout
externalCmd.Stdin = os.Stdin
preparedCmd := run.PrepareCmd(externalCmd)
err = preparedCmd.Run()
if err != nil {
if ee, ok := err.(*exec.ExitError); ok {
return nil, cmderrors.NewUserErrorWithExitCode(cmderrors.ExitCode(ee.ExitCode()), ee)
}
return nil, err
}
os.Exit(int(cmderrors.ExitOK))
} else if len(expandedArgs) > 0 && !hasCommand(cmd.Root(), expandedArgs) {
extensionManager := cmdFactory.ExtensionManager()
if found, err := extensionManager.Dispatch(expandedArgs, os.Stdin, os.Stdout, os.Stderr); err != nil {
var execError *exec.ExitError
if errors.As(err, &execError) {
return nil, cmderrors.NewUserErrorWithExitCode(cmderrors.ExitCode(execError.ExitCode()), execError)
}
fmt.Fprintf(cmdFactory.IOStreams.ErrOut, "failed to run extension: %s\n", err)
return nil, cmderrors.NewSystemError("failed to run extension")
} else if found {
return nil, cmderrors.NewErrorWithExitCode(0, nil)
}
}
}
return expandedArgs, nil
}
func getOutputHeaders(c *console.Console, cfg *config.Config, input []string) (headers []byte) {
if !c.IsCSV() || !c.WithCSVHeader() || len(input) == 0 {
Logger.Debugf("Ignoring csv headers: isCSV=%v, WithHeader=%v", c.IsCSV(), c.WithCSVHeader())
return
}
if len(input) > 0 {
return []byte(input[0] + "\n")
}
// TODO: improve detection by parsing more lines to find column names (if more lines are available)
columns := make([][]byte, 0)
for _, v := range cfg.GetJSONSelect() {
for _, column := range strings.Split(v, ",") {
if i := strings.Index(column, ":"); i > -1 {
columns = append(columns, []byte(column[0:i]))
} else {
columns = append(columns, []byte(column))
}
}
}
return append(bytes.Join(columns, []byte(",")), []byte("\n")...)
}
// GetInitLoggerOptions create a simple logger with a best-guess log level based on the given arguments
// It will not activate on any environment variables, however it will do a simplistic parsing of
// the common logging options before the configuration has been read which enables debugging around
// the configuration and extensions etc.
func GetInitLoggerOptions(args []string) logger.Options {
color := true
level := zapcore.WarnLevel
debug := false
for _, item := range args {
switch item {
case "--debug", "--debug=true":
level = zapcore.DebugLevel
debug = true
case "--verbose", "-v", "--verbose=true":
level = zapcore.InfoLevel
case "--noColor", "--noColor=true", "-M", "-M=true":
color = false
}
}
return logger.Options{
Level: level,
Debug: debug,
Color: color,
}
}
// Initialize initializes the configuration manager and c8y client
func Initialize() (*root.CmdRoot, error) {
var client *c8y.Client
var dataView *dataview.DataView
var consoleHandler *console.Console
var logHandler *logger.Logger
var activityLoggerHandler *activitylogger.ActivityLogger
var configHandler = config.NewConfig(viper.GetViper())
// init logger
logHandler = logger.NewLogger(module, GetInitLoggerOptions(os.Args))
if _, err := configHandler.ReadConfigFiles(nil); err != nil {
logHandler.Infof("Failed to read configuration. Trying to proceed anyway. %s", err)
}
// cmd factory
configFunc := func() (*config.Config, error) {
if configHandler == nil {
return nil, fmt.Errorf("config is missing")
}
return configHandler, nil
}
clientFunc := func() (*c8y.Client, error) {
if client == nil {
return nil, fmt.Errorf("client is missing")
}
return client, nil
}
loggerFunc := func() (*logger.Logger, error) {
if logHandler == nil {
return nil, fmt.Errorf("logger is missing")
}
return logHandler, nil
}
activityLoggerFunc := func() (*activitylogger.ActivityLogger, error) {
if activityLoggerHandler == nil {
return nil, fmt.Errorf("activityLogger is missing")
}
return activityLoggerHandler, nil
}
dataViewFunc := func() (*dataview.DataView, error) {
if dataView == nil {
return nil, fmt.Errorf("dataView is missing")
}
return dataView, nil
}
consoleFunc := func() (*console.Console, error) {
if consoleHandler == nil {
return nil, fmt.Errorf("console is missing")
}
return consoleHandler, nil
}
cmdFactory := factory.New(buildVersion, buildBranch, configFunc, clientFunc, loggerFunc, activityLoggerFunc, dataViewFunc, consoleFunc)
// Register the template resolver so the configuration can lookup values as needed
configHandler.RegisterTemplateResolver(cmdutil.NewTemplateResolver(cmdFactory))
rootCmd := root.NewCmdRoot(cmdFactory, buildVersion, "")
tableOptions := &console.TableOptions{
MinColumnWidth: configHandler.ViewColumnMinWidth(),
MaxColumnWidth: configHandler.ViewColumnMaxWidth(),
MinEmptyValueColumnWidth: configHandler.ViewColumnEmptyValueMinWidth(),
ColumnPadding: configHandler.ViewColumnPadding(),
RowMode: configHandler.ViewRowMode(),
}
consoleHandler = console.NewConsole(rootCmd.OutOrStdout(), tableOptions, func(s []string) []byte {
return getOutputHeaders(consoleHandler, configHandler, s)
})
return rootCmd, nil
}