From 33731f4eefa64c9ca40004ce53f52c1f0806ad13 Mon Sep 17 00:00:00 2001 From: adohe Date: Tue, 12 Mar 2024 16:31:11 +0800 Subject: [PATCH] feat: add basic kusiom mod cmd --- pkg/cmd/cmd.go | 7 +++++++ pkg/cmd/mod/mod.go | 31 +++++++++++++++++++++++++++++++ pkg/cmd/mod/mod_init.go | 28 ++++++++++++++++++++++++++++ pkg/cmd/mod/mod_push.go | 28 ++++++++++++++++++++++++++++ pkg/cmd/util/helpers.go | 32 ++++++++++++++++++++++++++++++++ 5 files changed, 126 insertions(+) create mode 100644 pkg/cmd/mod/mod.go create mode 100644 pkg/cmd/mod/mod_init.go create mode 100644 pkg/cmd/mod/mod_push.go diff --git a/pkg/cmd/cmd.go b/pkg/cmd/cmd.go index a1d03d00..0549ab13 100644 --- a/pkg/cmd/cmd.go +++ b/pkg/cmd/cmd.go @@ -12,6 +12,7 @@ import ( "kusionstack.io/kusion/pkg/cmd/apply" "kusionstack.io/kusion/pkg/cmd/build" 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" @@ -103,6 +104,12 @@ func NewKusionctlCmd(o KusionctlOptions) *cobra.Command { destroy.NewCmdDestroy(), }, }, + { + Message: "Module Management Commands:", + Commands: []*cobra.Command{ + mod.NewCmdMod(o.IOStreams), + }, + }, } groups.Add(cmds) diff --git a/pkg/cmd/mod/mod.go b/pkg/cmd/mod/mod.go new file mode 100644 index 00000000..061c8673 --- /dev/null +++ b/pkg/cmd/mod/mod.go @@ -0,0 +1,31 @@ +package mod + +import ( + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" + + cmdutil "kusionstack.io/kusion/pkg/cmd/util" + "kusionstack.io/kusion/pkg/util/i18n" +) + +var modLong = i18n.T(` + Commands for managing Kusion modules. + + These commands help you manage the lifecycle of Kusion modules.`) + +// NewCmdMod returns an initialized Command instance for 'mod' sub command +func NewCmdMod(streams genericclioptions.IOStreams) *cobra.Command { + cmd := &cobra.Command{ + Use: "mod SUBCOMMAND", + DisableFlagsInUseLine: true, + Short: "Manage Kusion modules", + Long: modLong, + Run: cmdutil.DefaultSubCommandRun(streams.ErrOut), + } + + // add subcommands + cmd.AddCommand(NewCmdInit(streams)) + cmd.AddCommand(NewCmdPush(streams)) + + return cmd +} diff --git a/pkg/cmd/mod/mod_init.go b/pkg/cmd/mod/mod_init.go new file mode 100644 index 00000000..83b62939 --- /dev/null +++ b/pkg/cmd/mod/mod_init.go @@ -0,0 +1,28 @@ +package mod + +import ( + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" +) + +type InitModOptions struct{} + +var ( + initLong = `` + initExample = `` +) + +// NewCmdInit returns an initialized Command instance for the 'mod init' sub command +func NewCmdInit(streams genericclioptions.IOStreams) *cobra.Command { + cmd := &cobra.Command{ + Use: "", + DisableFlagsInUseLine: true, + Short: "", + Long: initLong, + Example: initExample, + Run: func(cmd *cobra.Command, args []string) { + }, + } + + return cmd +} diff --git a/pkg/cmd/mod/mod_push.go b/pkg/cmd/mod/mod_push.go new file mode 100644 index 00000000..9e5ec6fc --- /dev/null +++ b/pkg/cmd/mod/mod_push.go @@ -0,0 +1,28 @@ +package mod + +import ( + "github.com/spf13/cobra" + "k8s.io/cli-runtime/pkg/genericclioptions" +) + +type PushModOptions struct{} + +var ( + pushLong = `` + pushExample = `` +) + +// NewCmdPush returns an initialized Command instance for the 'mod push' sub command +func NewCmdPush(streams genericclioptions.IOStreams) *cobra.Command { + cmd := &cobra.Command{ + Use: "", + DisableFlagsInUseLine: true, + Short: "", + Long: pushLong, + Example: pushExample, + Run: func(cmd *cobra.Command, args []string) { + }, + } + + return cmd +} diff --git a/pkg/cmd/util/helpers.go b/pkg/cmd/util/helpers.go index f1c41695..9bcd1753 100644 --- a/pkg/cmd/util/helpers.go +++ b/pkg/cmd/util/helpers.go @@ -2,6 +2,11 @@ package util import ( "errors" + "fmt" + "io" + "strings" + + "github.com/spf13/cobra" ) func RecoverErr(err *error) { @@ -17,8 +22,35 @@ func RecoverErr(err *error) { } } +// ErrExit may be passed to CheckError to instruct it to output nothing but exit with +// status code 1. +var ErrExit = fmt.Errorf("exit") + func CheckErr(err error) { if err != nil { panic(err) } } + +func UsageErrorf(cmd *cobra.Command, format string, args ...interface{}) error { + msg := fmt.Sprintf(format, args...) + return fmt.Errorf("%s\nSee '%s -h' for help and examples", msg, cmd.CommandPath()) +} + +// RequireNoArguments exits with a usage error if extra arguments are provided. +func RequireNoArguments(c *cobra.Command, args []string) { + if len(args) > 0 { + CheckErr(UsageErrorf(c, "unknown command %q", strings.Join(args, " "))) + } +} + +// DefaultSubCommandRun prints a command's help string to the specified output if no +// arguments (sub-commands) are provided, or a usage error otherwise. +func DefaultSubCommandRun(out io.Writer) func(c *cobra.Command, args []string) { + return func(c *cobra.Command, args []string) { + c.SetOut(out) + RequireNoArguments(c, args) + c.Help() + CheckErr(ErrExit) + } +}