-
Notifications
You must be signed in to change notification settings - Fork 24
/
decisions.go
65 lines (57 loc) · 1.4 KB
/
decisions.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
package utils
import (
"context"
"errors"
"strings"
languagev1 "github.com/fluxninja/aperture/api/v2/gen/proto/go/aperture/policy/language/v1"
"github.com/spf13/cobra"
)
var decisionTypes = []string{
"load_scheduler",
"rate_limiter",
"quota_scheduler",
"pod_scaler",
"sampler",
}
// DecisionsPreRun validates the decisions command.
func DecisionsPreRun(all bool, decisionType string) (string, error) {
if !all {
if decisionType == "" {
return decisionType, errors.New("decision type is required or use --all to get all decisions")
} else {
var found bool
for _, v := range decisionTypes {
if v == decisionType {
found = true
break
}
}
if !found {
return decisionType, errors.New("invalid decision type, use one of the valid types (" + strings.Join(decisionTypes, ", ") + ") or use --all to get all decisions")
}
}
} else {
decisionType = ""
}
return decisionType, nil
}
// ParseDecisions parses the decisions.
func ParseDecisions(cmd *cobra.Command, client SelfHostedPolicyClient, all bool, decisionType string) error {
getDecisionsReq := &languagev1.GetDecisionsRequest{
DecisionType: decisionType,
}
if all {
getDecisionsReq = nil
}
decisionsResp, err := client.GetDecisions(
context.Background(),
getDecisionsReq,
)
if err != nil {
return err
}
for k, v := range decisionsResp.Decisions {
cmd.Printf("%s:\n%s\n\n", k, v)
}
return nil
}