pink_fox/application/packages/fw/cmd.go

44 lines
737 B
Go

package fw
import "github.com/spf13/cobra"
type CLI struct {
Use string
Short string
Long string
Commands []Command
}
type Command struct {
Use string
Short string
Long string
Args cobra.PositionalArgs
Run func(cmd *cobra.Command, args []string)
Init func(cmd *cobra.Command)
}
func cmdInit(cli *CLI) *cobra.Command {
rootCmd := &cobra.Command{
Use: cli.Use,
Short: cli.Short,
Long: cli.Long,
}
for _, command := range cli.Commands {
subCmd := &cobra.Command{
Use: command.Use,
Short: command.Short,
Long: command.Long,
Args: command.Args,
Run: command.Run,
}
if command.Init != nil {
command.Init(subCmd)
}
rootCmd.AddCommand(subCmd)
}
return rootCmd
}