Documentation ¶
Overview ¶
Package cli contains utilities to create cli(s) faster in a declarative way that plays nicely with different configuration options.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type CLI ¶
type CLI struct {
// contains filtered or unexported fields
}
CLI implements cobra.Command that also plays nicely with viper.
func New ¶
New creates a new CLI.
Example ¶
package main import ( "context" "errors" "fmt" "os" "os/signal" "github.com/gojekfarm/xtools/x/cli" ) type Config struct { Debug bool `flag:"debug,global" env:"DEBUG" default:"false" flag-usage:"enable debug mode"` Log struct { Enabled bool `flag:"enabled" env:"ENABLED" default:"true" flag-usage:"enable logging"` Level string `flag:"level" env:"LEVEL" default:"info"` } `flag-prefix:"log,global" env-prefix:"LOG"` Host string `flag:"host" env:"HOST" default:"localhost" sub-commands:"server"` Port int `flag:"port" env:"PORT" default:"8080" sub-commands:"server"` Database struct { URL string `flag:"url" env:"URL" default:"mongodb://localhost:27017"` Name string `flag:"name" env:"NAME" default:"staging"` } `sub-commands:"migrate,server" flag-prefix:"db" env-prefix:"DB"` } func main() { cfg := new(Config) c := cli.New( "my-command", cli.ConfigObject(cfg), cli.Commands{ { Name: "migrate", Description: cli.Description{ Short: "migrate database", Long: "migrate database schema", }, Run: func(ctx context.Context, cfg interface{}) error { fmt.Println("run migrate") if v, ok := cfg.(*Config); !ok { return errors.New("not a Config object") } else { fmt.Printf("%+v\n", v) } return nil }, Commands: []cli.Command{ { Name: "up", Run: func(ctx context.Context, cfg interface{}) error { fmt.Println("run migrate up") if v, ok := cfg.(*Config); !ok { return errors.New("not a Config object") } else { fmt.Printf("%+v\n", v) } return nil }, }, }, }, { Name: "server", Run: func(ctx context.Context, cfg interface{}) error { fmt.Println("run server") if v, ok := cfg.(*Config); !ok { return errors.New("not a Config object") } else { fmt.Printf("%+v\n", v) } return nil }, }, }, ) ctx, _ := signal.NotifyContext(context.Background(), os.Interrupt, os.Kill) c.SetArgs([]string{"server", "--help"}) if err := c.Run(ctx); err != nil { os.Exit(1) } }
Output:
type Command ¶
type Command struct { Name string Description Description Run CommandRunner Commands []Command }
Command is an option that can be used to specify a command.
type CommandRunner ¶
CommandRunner is a function that can be used to run a command.
type Commands ¶
type Commands []Command
Commands is an option that can be used to specify multiple commands.
type Description ¶
type Description struct { // Short is a short description of the command. Short string // Long is a long description of the command. Long string }
Description is an option that can be used to specify a command description.
type Option ¶
type Option interface {
// contains filtered or unexported methods
}
Option can be used to configure a CLI.
func ConfigObject ¶
func ConfigObject(cfg interface{}) Option
ConfigObject is an option that can be used to specify a configuration object.
Click to show internal directories.
Click to hide internal directories.