...

Source file src/github.com/peterbourgon/ff/v3/ffcli/examples/objectctl/pkg/rootcmd/root.go

Documentation: github.com/peterbourgon/ff/v3/ffcli/examples/objectctl/pkg/rootcmd

     1  package rootcmd
     2  
     3  import (
     4  	"context"
     5  	"flag"
     6  
     7  	"github.com/peterbourgon/ff/v3/ffcli"
     8  	"github.com/peterbourgon/ff/v3/ffcli/examples/objectctl/pkg/objectapi"
     9  )
    10  
    11  // Config for the root command, including flags and types that should be
    12  // available to each subcommand.
    13  type Config struct {
    14  	Token   string
    15  	Verbose bool
    16  
    17  	Client *objectapi.Client
    18  }
    19  
    20  // New constructs a usable ffcli.Command and an empty Config. The config's token
    21  // and verbose fields will be set after a successful parse. The caller must
    22  // initialize the config's object API client field.
    23  func New() (*ffcli.Command, *Config) {
    24  	var cfg Config
    25  
    26  	fs := flag.NewFlagSet("objectctl", flag.ExitOnError)
    27  	cfg.RegisterFlags(fs)
    28  
    29  	return &ffcli.Command{
    30  		Name:       "objectctl",
    31  		ShortUsage: "objectctl [flags] <subcommand> [flags] [<arg>...]",
    32  		FlagSet:    fs,
    33  		Exec:       cfg.Exec,
    34  	}, &cfg
    35  }
    36  
    37  // RegisterFlags registers the flag fields into the provided flag.FlagSet. This
    38  // helper function allows subcommands to register the root flags into their
    39  // flagsets, creating "global" flags that can be passed after any subcommand at
    40  // the commandline.
    41  func (c *Config) RegisterFlags(fs *flag.FlagSet) {
    42  	fs.StringVar(&c.Token, "token", "", "secret token for object API")
    43  	fs.BoolVar(&c.Verbose, "v", false, "log verbose output")
    44  }
    45  
    46  // Exec function for this command.
    47  func (c *Config) Exec(context.Context, []string) error {
    48  	// The root command has no meaning, so if it gets executed,
    49  	// display the usage text to the user instead.
    50  	return flag.ErrHelp
    51  }
    52  

View as plain text