Skip to content

Commit

Permalink
feat: add basic kusiom mod cmd (#905)
Browse files Browse the repository at this point in the history
  • Loading branch information
adohe committed Mar 12, 2024
1 parent 77d8c8f commit 44522e5
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pkg/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)

Expand Down
31 changes: 31 additions & 0 deletions pkg/cmd/mod/mod.go
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions pkg/cmd/mod/mod_init.go
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions pkg/cmd/mod/mod_push.go
Original file line number Diff line number Diff line change
@@ -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
}
32 changes: 32 additions & 0 deletions pkg/cmd/util/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ package util

import (
"errors"
"fmt"
"io"
"strings"

"github.com/spf13/cobra"
)

func RecoverErr(err *error) {
Expand All @@ -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)
}
}

0 comments on commit 44522e5

Please sign in to comment.