-
Notifications
You must be signed in to change notification settings - Fork 197
/
config.go
80 lines (73 loc) · 2.07 KB
/
config.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
77
78
79
80
package cmd
import (
"context"
"fmt"
"io"
"strings"
"github.com/aquasecurity/starboard/pkg/starboard"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/kubernetes"
)
type LocalFlags struct {
get string
nameOnly bool
}
func NewConfigCmd(cf *genericclioptions.ConfigFlags, outWriter io.Writer) *cobra.Command {
var localFlags LocalFlags
cmd := &cobra.Command{
Use: "config",
Short: "View the configuration parameters used by Starboard scanners",
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
kubernetesConfig, err := cf.ToRESTConfig()
if err != nil {
return err
}
clientset, err := kubernetes.NewForConfig(kubernetesConfig)
if err != nil {
return err
}
config, err := starboard.NewConfigManager(clientset, starboard.NamespaceName).Read(ctx)
if err != nil {
return err
}
filteredValues, err := getFilteredValues(config, &localFlags)
if err != nil {
return err
}
_, _ = fmt.Fprintf(outWriter, "%s\n", filteredValues)
return nil
},
}
setLocalFlags(cmd, &localFlags)
return cmd
}
func getFilteredValues(data starboard.ConfigData, localFlags *LocalFlags) (string, error) {
if localFlags.get != "" {
value := data[localFlags.get]
if value != "" {
return data[localFlags.get], nil
} else {
return "", fmt.Errorf("no such key exists: %s", localFlags.get)
}
}
if localFlags.nameOnly {
return convertMapToString(data, true), nil
}
return convertMapToString(data, false), nil
}
func convertMapToString(mapToConvert map[string]string, nameOnly bool) string {
asString := make([]string, 0, len(mapToConvert))
for key, val := range mapToConvert {
asString = append(asString, key)
if !nameOnly {
asString = append(asString, val)
}
}
return strings.Join(asString, "\n")
}
func setLocalFlags(cmd *cobra.Command, localFlags *LocalFlags) {
cmd.Flags().BoolVar(&localFlags.nameOnly, "name-only", false, "List parameters by name only")
cmd.Flags().StringVar(&localFlags.get, "get", "", "Get configuration parameters for a specified key")
}