-
Notifications
You must be signed in to change notification settings - Fork 78
/
cmd.go
129 lines (108 loc) · 3.52 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
package cmd
import (
"os"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericiooptions"
cliflag "k8s.io/component-base/cli/flag"
"k8s.io/kubectl/pkg/cmd/options"
"k8s.io/kubectl/pkg/util/templates"
"kusionstack.io/kusion/pkg/cmd/apply"
"kusionstack.io/kusion/pkg/cmd/config"
"kusionstack.io/kusion/pkg/cmd/generate"
cmdinit "kusionstack.io/kusion/pkg/cmd/init"
"kusionstack.io/kusion/pkg/cmd/mod"
"kusionstack.io/kusion/pkg/cmd/workspace"
"kusionstack.io/kusion/pkg/cmd/destroy"
"kusionstack.io/kusion/pkg/cmd/preview"
"kusionstack.io/kusion/pkg/cmd/version"
"kusionstack.io/kusion/pkg/util/i18n"
)
type KusionctlOptions struct {
Arguments []string
genericiooptions.IOStreams
}
// NewDefaultKusionctlCommand creates the `kusionctl` command with default arguments
func NewDefaultKusionctlCommand() *cobra.Command {
return NewDefaultKusionctlCommandWithArgs(KusionctlOptions{
Arguments: os.Args,
IOStreams: genericiooptions.IOStreams{In: os.Stdin, Out: os.Stdout, ErrOut: os.Stderr},
})
}
// NewDefaultKusionctlCommandWithArgs creates the `kusionctl` command with arguments
func NewDefaultKusionctlCommandWithArgs(o KusionctlOptions) *cobra.Command {
cmd := NewKusionctlCmd(o)
if len(o.Arguments) > 1 {
cmdPathPieces := o.Arguments[1:]
if _, _, err := cmd.Find(cmdPathPieces); err == nil {
// sub command exist
return cmd
}
}
return cmd
}
func NewKusionctlCmd(o KusionctlOptions) *cobra.Command {
// Sending in 'nil' for the getLanguageFn() results in using LANGUAGE, LC_ALL,
// LC_MESSAGES, or LANG environment variable in sequence.
_ = i18n.LoadTranslations(i18n.DomainKusion, nil)
// Parent command to which all subcommands are added.
cmds := &cobra.Command{
Use: "kusion",
Short: i18n.T(`Kusion is the Platform Orchestrator of Internal Developer Platform`),
Long: templates.LongDesc(`
As a Platform Orchestrator, Kusion delivers user intentions to Kubernetes, Clouds and On-Premise resources.
Also enables asynchronous cooperation between the development and the platform team and drives separation of concerns.
Find more information at:
https://www.kusionstack.io/docs/user_docs/reference/cli/kusion/`),
SilenceErrors: true,
Run: runHelp,
// Hook before and after Run initialize and write profiles to disk,
// respectively.
PersistentPreRunE: func(*cobra.Command, []string) error {
return initProfiling()
},
PersistentPostRunE: func(*cobra.Command, []string) error {
if err := flushProfiling(); err != nil {
return err
}
return nil
},
}
// From this point and forward we get warnings on flags that contain "_" separators
cmds.SetGlobalNormalizationFunc(cliflag.WarnWordSepNormalizeFunc)
flags := cmds.PersistentFlags()
addProfilingFlags(flags)
groups := templates.CommandGroups{
{
Message: "Configuration Commands:",
Commands: []*cobra.Command{
workspace.NewCmd(),
cmdinit.NewCmd(),
config.NewCmd(),
generate.NewCmdGenerate(o.IOStreams),
},
},
{
Message: "Runtime Commands:",
Commands: []*cobra.Command{
preview.NewCmdPreview(),
apply.NewCmdApply(),
destroy.NewCmdDestroy(),
},
},
{
Message: "Module Management Commands:",
Commands: []*cobra.Command{
mod.NewCmdMod(o.IOStreams),
},
},
}
groups.Add(cmds)
filters := []string{"options"}
templates.ActsAsRootCommand(cmds, filters, groups...)
cmds.AddCommand(version.NewCmdVersion())
cmds.AddCommand(options.NewCmdOptions(o.IOStreams.Out))
return cmds
}
func runHelp(cmd *cobra.Command, args []string) {
_ = cmd.Help()
}