/cobra-cmder

Useful command builder for spf13/cobra

Primary LanguageGoBSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

cobra-cmder

go.dev

Introduction

This Go module contains cmder library package which is a useful command builder for spf13/cobra.

It helps you to easily build a cobra.Command hierarchy in the non-invasive and test-friendly way without any global variables or init().

Basic usage

Define each command's sturct that implements Cmder interface:

type App struct {
	Bool bool // storage for flag -b
}
type AppAlpha struct {
	*App          // storage for parent Cmder (embedded)
	String string // storage for flag -s
}
type AppAlphaOne struct {
	*AppAlpha     // storage for parent Cmder (embedded)
	Int       int // storage for flag -i
}

Define each Comder's Cmd() method that returns *cobra.Command:

func (app *App) Cmd() *cobra.Command {
	cmd := &cobra.Command{
		Use: "app",
	}
	cmd.PersistentFlags().BoolVarP(&app.Bool, "bool", "b", false, "Bool flag")
	return cmd
}

func (app *AppAlpha) Cmd() *cobra.Command {
	cmd := &cobra.Command{
		Use: "alpha",
	}
	cmd.PersistentFlags().StringVarP(&app.String, "string", "s", "", "String flag")
	return cmd
}

func (app *AppAlphaOne) Cmd() *cobra.Command {
	cmd := &cobra.Command{
		Use: "one",
		Run: app.Run,
	}
	cmd.Flags().IntVarP(&app.Int, "int", "i", 0, "Int flag")
	return cmd
}

func (app *AppAlphaOne) Run(cmd *cobra.Command, args []string) {
	fmt.Println(app.Bool, app.String, app.Int)
}

Associate Cmders each other by defining a method that returns a child Cmder:

func (app *App) AppAlphaCmder() cmder.Cmder         { return &AppAlpha{App: app} }
func (app *AppAlpha) AppAlphaOneCmder() cmder.Cmder { return &AppAlphaOne{AppAlpha: app} }

Call cmder.Cmd() to collect and associate all cobra.Command instances:

func main() {
	app := &App{}
	cmd := cmder.Cmd(app)
	err := cmd.Execute()
	if err != nil {
		os.Exit(1)
	}
}

Visit https://play.golang.org/p/zw4arxJfUkt to see and test the complete source code.

Unit test

You can easily perform the Go standard unit tests on CLI apps with cobra-cmder.

See https://play.golang.org/p/tijvjDzmwqW for another example.

Examples

See the sample CLI app in cmd/sample for more comprehensive example.

Usage in real applications: