-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathmain.go
More file actions
78 lines (71 loc) · 1.85 KB
/
main.go
File metadata and controls
78 lines (71 loc) · 1.85 KB
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
package main
import (
"fmt"
"os"
"strings"
log "github.com/kosli-dev/cli/internal/logger"
"github.com/kosli-dev/cli/internal/requests"
"github.com/spf13/cobra"
_ "k8s.io/client-go/plugin/pkg/client/auth"
)
var (
logger *log.Logger
kosliClient *requests.Client
)
func init() {
logger = log.NewStandardLogger()
// needed for some tests, actual CLI client is initialized in root.go
kosliClient, _ = requests.NewKosliClient("", 3, false, logger)
}
func main() {
var err error
if isDoubledHost() {
var output string
output, err = runDoubledHost(os.Args)
fmt.Print(output)
} else {
var cmd *cobra.Command
cmd, err = newRootCmd(logger.Out, os.Args[1:])
if err == nil {
err = innerMain(cmd, os.Args)
}
}
if err != nil {
logger.Error(err.Error())
}
}
func innerMain(cmd *cobra.Command, args []string) error {
err := cmd.Execute()
if err == nil {
return nil
}
// cobra does not capture unknown/missing commands, see https://github.com/spf13/cobra/issues/706
// so we handle this here until it is fixed in cobra
if strings.Contains(err.Error(), "unknown flag:") {
c, flags, err := cmd.Traverse(args[1:])
if err != nil {
return err
}
if c.HasSubCommands() {
errMessage := ""
if strings.HasPrefix(flags[0], "-") {
errMessage = "missing subcommand"
} else {
errMessage = fmt.Sprintf("unknown command: %s", flags[0])
}
availableSubcommands := []string{}
for _, sc := range c.Commands() {
if !sc.Hidden {
availableSubcommands = append(availableSubcommands, strings.Split(sc.Use, " ")[0])
}
}
logger.Error("%s\navailable subcommands are: %s", errMessage, strings.Join(availableSubcommands, " | "))
}
}
if global.DryRun == "true" {
logger.Info("Error: %s", err.Error())
logger.Warning("Encountered an error but --dry-run is enabled. Exiting with 0 exit code.")
return nil
}
return err
}