-
Notifications
You must be signed in to change notification settings - Fork 15
/
sabakan_set_variables.go
61 lines (52 loc) · 1.26 KB
/
sabakan_set_variables.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 cmd
import (
"context"
"encoding/json"
"os"
"github.com/cybozu-go/cke/sabakan"
"github.com/cybozu-go/well"
"github.com/spf13/cobra"
)
// sabakanSetVariablesCmd represents the "sabakan set-variables" command
var sabakanSetVariablesCmd = &cobra.Command{
Use: "set-variables FILE",
Short: "set the query variables to search machines in sabakan",
Long: `Set the query variables to search machines in sabakan.
FILE should contain a JSON object like this:
{
"having": {
"labels": [{"name": "foo", "value": "bar"}],
"racks": [0, 1, 2],
"roles": ["worker"],
"states": ["HEALTHY"],
"minDaysBeforeRetire": 90
},
"notHaving": {
}
}
`,
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
data, err := os.ReadFile(args[0])
if err != nil {
return err
}
vars := new(sabakan.QueryVariables)
err = json.Unmarshal(data, vars)
if err != nil {
return err
}
err = vars.IsValid()
if err != nil {
return err
}
well.Go(func(ctx context.Context) error {
return storage.SetSabakanQueryVariables(ctx, string(data))
})
well.Stop()
return well.Wait()
},
}
func init() {
sabakanCmd.AddCommand(sabakanSetVariablesCmd)
}