forked from sensu/sensu-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create.go
76 lines (63 loc) · 1.82 KB
/
create.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package entity
import (
"errors"
"fmt"
"github.com/sensu/sensu-go/api/core/v2"
"github.com/sensu/sensu-go/cli"
"github.com/sensu/sensu-go/cli/commands/flags"
"github.com/sensu/sensu-go/cli/commands/helpers"
"github.com/spf13/cobra"
)
// CreateCommand allows a user to create a new entity
func CreateCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "create [NAME]",
Short: "create a new entity",
SilenceUsage: true,
PreRun: func(cmd *cobra.Command, args []string) {
isInteractive, _ := cmd.Flags().GetBool(flags.Interactive)
if !isInteractive {
// Mark flags are required for bash-completions
_ = cmd.MarkFlagRequired("name")
_ = cmd.MarkFlagRequired("entity-class")
}
},
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) > 1 {
_ = cmd.Help()
return errors.New("invalid argument(s) received")
}
isInteractive, _ := cmd.Flags().GetBool(flags.Interactive)
opts := newEntityOpts()
if len(args) > 0 {
opts.Name = args[0]
}
opts.Namespace = cli.Config.Namespace()
if isInteractive {
if err := opts.administerQuestionnaire(); err != nil {
return err
}
} else {
opts.withFlags(cmd.Flags())
}
// Apply given arguments to entity
entity := v2.NewEntity(v2.NewObjectMeta("", ""))
opts.copy(entity)
if err := entity.Validate(); err != nil {
if !isInteractive {
_ = cmd.Help()
}
return err
}
if err := cli.Client.CreateEntity(entity); err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), "Created")
return nil
},
}
_ = cmd.Flags().StringP("entity-class", "c", "", "entity class, either proxy or agent")
_ = cmd.Flags().StringP("subscriptions", "s", "", "comma separated list of subscriptions")
helpers.AddInteractiveFlag(cmd.Flags())
return cmd
}