-
Notifications
You must be signed in to change notification settings - Fork 103
/
print-rules.go
55 lines (46 loc) · 1.27 KB
/
print-rules.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
package cmd
import (
"fmt"
"io"
"os"
"sort"
"strings"
"github.com/controlplaneio/kubesec/v2/pkg/ruler"
"github.com/controlplaneio/kubesec/v2/pkg/util"
"github.com/spf13/cobra"
)
func init() {
var format string
var printRulesCmd = &cobra.Command{
Use: `print-rules`,
Short: "Print all the scanning rules with their associated scores",
Example: ` kubesec print-rules
kubesec print-rules -f yaml
kubesec print-rules -f table`,
}
printRulesCmd.Flags().StringVarP(&format, "format", "f", "json", "Set output format (json, yaml, table)")
printRulesCmd.RunE = func(cmd *cobra.Command, args []string) error {
rootCmd.SilenceErrors = true
rootCmd.SilenceUsage = true
ruleSet := ruler.NewRuleset(logger)
// Sort by rule ID
sort.Slice(ruleSet.Rules, func(i, j int) bool {
return ruleSet.Rules[i].ID < ruleSet.Rules[j].ID
})
printTableFn := func(w io.Writer) error {
tw := util.NewTabWriter(w)
fmt.Fprintf(tw, "ID\tReason\tPoints\tKinds\n")
for _, rule := range ruleSet.Rules {
fmt.Fprintf(tw, "%s\t%s\t%d\t%s\t\n",
rule.ID,
rule.Reason,
rule.Points,
strings.Join(rule.Kinds, ","),
)
}
return tw.Flush()
}
return util.Print(format, ruleSet.Rules, os.Stdout, printTableFn)
}
rootCmd.AddCommand(printRulesCmd)
}