...
1 package createcmd
2
3 import (
4 "context"
5 "errors"
6 "flag"
7 "fmt"
8 "io"
9 "strings"
10
11 "github.com/peterbourgon/ff/v3/ffcli"
12 "github.com/peterbourgon/ff/v3/ffcli/examples/objectctl/pkg/rootcmd"
13 )
14
15
16 type Config struct {
17 rootConfig *rootcmd.Config
18 out io.Writer
19 overwrite bool
20 }
21
22
23 func New(rootConfig *rootcmd.Config, out io.Writer) *ffcli.Command {
24 cfg := Config{
25 rootConfig: rootConfig,
26 out: out,
27 }
28
29 fs := flag.NewFlagSet("objectctl create", flag.ExitOnError)
30 fs.BoolVar(&cfg.overwrite, "overwrite", false, "overwrite existing object, if it exists")
31 rootConfig.RegisterFlags(fs)
32
33 return &ffcli.Command{
34 Name: "create",
35 ShortUsage: "objectctl create [flags] <key> <value data...>",
36 ShortHelp: "Create or overwrite an object",
37 FlagSet: fs,
38 Exec: cfg.Exec,
39 }
40 }
41
42
43 func (c *Config) Exec(ctx context.Context, args []string) error {
44 if len(args) < 2 {
45 return errors.New("create requires at least 2 args")
46 }
47
48 var (
49 key = args[0]
50 value = strings.Join(args[1:], " ")
51 err = c.rootConfig.Client.Create(ctx, key, value, c.overwrite)
52 )
53 if err != nil {
54 return err
55 }
56
57 if c.rootConfig.Verbose {
58 fmt.Fprintf(c.out, "create %q OK\n", key)
59 }
60
61 return nil
62 }
63
View as plain text