-
Notifications
You must be signed in to change notification settings - Fork 0
/
root.go
119 lines (104 loc) · 2.74 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
package cmd
import (
_ "embed"
"fmt"
"os"
"runtime"
"strings"
"text/template"
"github.com/gookit/color"
"github.com/spf13/cobra"
"github.com/withfig/autocomplete-tools/integrations/cobra"
"github.com/jageros/goctl/api"
"github.com/jageros/goctl/bug"
"github.com/jageros/goctl/docker"
"github.com/jageros/goctl/env"
"github.com/jageros/goctl/gateway"
"github.com/jageros/goctl/internal/cobrax"
"github.com/jageros/goctl/internal/version"
"github.com/jageros/goctl/kube"
"github.com/jageros/goctl/migrate"
"github.com/jageros/goctl/model"
"github.com/jageros/goctl/quickstart"
"github.com/jageros/goctl/rpc"
"github.com/jageros/goctl/tpl"
"github.com/jageros/goctl/upgrade"
)
const (
codeFailure = 1
dash = "-"
doubleDash = "--"
assign = "="
)
var (
//go:embed usage.tpl
usageTpl string
rootCmd = cobrax.NewCommand("goctl")
)
// Execute executes the given command
func Execute() {
os.Args = supportGoStdFlag(os.Args)
if err := rootCmd.Execute(); err != nil {
fmt.Println(color.Red.Render(err.Error()))
os.Exit(codeFailure)
}
}
func supportGoStdFlag(args []string) []string {
copyArgs := append([]string(nil), args...)
parentCmd, _, err := rootCmd.Traverse(args[:1])
if err != nil { // ignore it to let cobra handle the error.
return copyArgs
}
for idx, arg := range copyArgs[0:] {
parentCmd, _, err = parentCmd.Traverse([]string{arg})
if err != nil { // ignore it to let cobra handle the error.
break
}
if !strings.HasPrefix(arg, dash) {
continue
}
flagExpr := strings.TrimPrefix(arg, doubleDash)
flagExpr = strings.TrimPrefix(flagExpr, dash)
flagName, flagValue := flagExpr, ""
assignIndex := strings.Index(flagExpr, assign)
if assignIndex > 0 {
flagName = flagExpr[:assignIndex]
flagValue = flagExpr[assignIndex:]
}
if !isBuiltin(flagName) {
// The method Flag can only match the user custom flags.
f := parentCmd.Flag(flagName)
if f == nil {
continue
}
if f.Shorthand == flagName {
continue
}
}
goStyleFlag := doubleDash + flagName
if assignIndex > 0 {
goStyleFlag += flagValue
}
copyArgs[idx] = goStyleFlag
}
return copyArgs
}
func isBuiltin(name string) bool {
return name == "version" || name == "help"
}
func init() {
cobra.AddTemplateFuncs(template.FuncMap{
"blue": blue,
"green": green,
"rpadx": rpadx,
"rainbow": rainbow,
})
rootCmd.Version = fmt.Sprintf(
"%s %s/%s", version.BuildVersion,
runtime.GOOS, runtime.GOARCH)
rootCmd.SetUsageTemplate(usageTpl)
rootCmd.AddCommand(api.Cmd, bug.Cmd, docker.Cmd, kube.Cmd, env.Cmd, gateway.Cmd, model.Cmd)
rootCmd.AddCommand(migrate.Cmd, quickstart.Cmd, rpc.Cmd, tpl.Cmd, upgrade.Cmd)
rootCmd.Command.AddCommand(cobracompletefig.CreateCompletionSpecCommand())
rootCmd.MustInit()
}