...

Source file src/github.com/peterbourgon/ff/v3/ffcli/examples/objectctl/pkg/deletecmd/delete.go

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

     1  package deletecmd
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"flag"
     7  	"fmt"
     8  	"io"
     9  
    10  	"github.com/peterbourgon/ff/v3/ffcli"
    11  	"github.com/peterbourgon/ff/v3/ffcli/examples/objectctl/pkg/rootcmd"
    12  )
    13  
    14  // Config for the delete subcommand, including a reference to the API client.
    15  type Config struct {
    16  	rootConfig *rootcmd.Config
    17  	out        io.Writer
    18  	force      bool
    19  }
    20  
    21  // New returns a usable ffcli.Command for the delete subcommand.
    22  func New(rootConfig *rootcmd.Config, out io.Writer) *ffcli.Command {
    23  	cfg := Config{
    24  		rootConfig: rootConfig,
    25  		out:        out,
    26  	}
    27  
    28  	fs := flag.NewFlagSet("objectctl delete", flag.ExitOnError)
    29  	rootConfig.RegisterFlags(fs)
    30  
    31  	return &ffcli.Command{
    32  		Name:       "delete",
    33  		ShortUsage: "objectctl delete <key>",
    34  		ShortHelp:  "Delete an object",
    35  		FlagSet:    fs,
    36  		Exec:       cfg.Exec,
    37  	}
    38  }
    39  
    40  // Exec function for this command.
    41  func (c *Config) Exec(ctx context.Context, args []string) error {
    42  	if len(args) < 1 {
    43  		return errors.New("delete requires at least 1 arg")
    44  	}
    45  
    46  	var (
    47  		key          = args[0]
    48  		existed, err = c.rootConfig.Client.Delete(ctx, key, c.force)
    49  	)
    50  	if err != nil {
    51  		return err
    52  	}
    53  
    54  	if c.rootConfig.Verbose {
    55  		fmt.Fprintf(c.out, "delete %q OK (existed %v)\n", key, existed)
    56  	}
    57  
    58  	return nil
    59  }
    60  

View as plain text