forked from sensu/sensu-go
/
update.go
61 lines (51 loc) · 1.34 KB
/
update.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
package check
import (
"errors"
"fmt"
"github.com/sensu/sensu-go/cli"
"github.com/spf13/cobra"
)
// UpdateCommand adds command that allows user to create new checks
func UpdateCommand(cli *cli.SensuCli) *cobra.Command {
cmd := &cobra.Command{
Use: "update [NAME]",
Short: "update checks",
SilenceUsage: false,
RunE: func(cmd *cobra.Command, args []string) error {
// Print usage if we do not receive one argument
if len(args) != 1 {
_ = cmd.Help()
return errors.New("invalid argument(s) received")
}
// Fetch checks from API
checkID := args[0]
check, err := cli.Client.FetchCheck(checkID)
if err != nil {
return err
}
// Administer questionnaire
opts := newCheckOpts()
opts.withCheck(check)
if err := opts.administerQuestionnaire(true); err != nil {
return err
}
// Apply given arguments to check
opts.Copy(check)
if err := check.Validate(); err != nil {
return err
}
//
// TODO:
//
// Current validation is a bit too laissez faire. For usability we should
// determine whether there are assets / handlers / mutators associated w/
// the check and warn the user if they do not exist yet.
if err := cli.Client.UpdateCheck(check); err != nil {
return err
}
fmt.Fprintln(cmd.OutOrStdout(), "Updated")
return nil
},
}
return cmd
}