package main import ( "fmt" "os" "github.com/spf13/cobra" "sigs.k8s.io/yaml" ) func main() { var err error util := NewUtil() err = util.Build() if err != nil { os.Exit(1) } err = util.Exec(os.Args[1:]) if err != nil { os.Exit(1) } } type Util struct { FileUtil ImageUtil rootCmd cobra.Command } func NewUtil() *Util { return &Util{} } func (util *Util) Build() error { var err error rootCmd := cobra.Command{ Use: "cmd", Short: "A brief description the command", } rootCmd.CompletionOptions.DisableDefaultCmd = true rootCmd.AddCommand(util.CreateFileCmds()) rootCmd.AddCommand(util.CreateImageCmds()) util.rootCmd = rootCmd return err } func (util *Util) Exec(args []string) error { var err error util.rootCmd.SetArgs(args) err = util.rootCmd.Execute() return err } func (util *Util) Hello(cmd *cobra.Command, args []string) { fmt.Println("hello, world!") } func printResponse(res any, err error) { type Response struct { Error bool `json:"error" yaml:"error"` Message string `json:"message,omitempty" yaml:"message,omitempty"` Result any `json:"result,omitempty" yaml:"result,omitempty"` } resp := Response{} if err != nil { resp.Error = true resp.Message = err.Error() } else { resp.Result = res } respBytes, _ := yaml.Marshal(resp) fmt.Printf("---\n%s\n", string(respBytes)) }