-
Notifications
You must be signed in to change notification settings - Fork 197
/
commands.go
77 lines (67 loc) · 1.93 KB
/
commands.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
package cmd
import (
"errors"
"strings"
"time"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/runtime/schema"
"github.com/aquasecurity/starboard/pkg/kube"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
func SetGlobalFlags(cf *genericclioptions.ConfigFlags, cmd *cobra.Command) {
cf.AddFlags(cmd.Flags())
for _, c := range cmd.Commands() {
SetGlobalFlags(cf, c)
}
}
func WorkloadFromArgs(mapper meta.RESTMapper, namespace string, args []string) (workload kube.ObjectRef, gvk schema.GroupVersionKind, err error) {
if len(args) < 1 {
err = errors.New("required workload kind and name not specified")
return
}
var resource, resourceName string
parts := strings.SplitN(args[0], "/", 2)
if len(parts) == 1 {
resource = "pods"
resourceName = parts[0]
} else {
resource = parts[0]
resourceName = parts[1]
}
_, gvk, err = kube.GVRForResource(mapper, resource)
if err != nil {
return
}
if "" == resourceName {
err = errors.New("required workload name is blank")
return
}
workload = kube.ObjectRef{
Namespace: namespace,
Kind: kube.Kind(gvk.Kind),
Name: resourceName,
}
return
}
const (
scanJobTimeoutFlagName = "scan-job-timeout"
deleteScanJobFlagName = "delete-scan-job"
)
func registerScannerOpts(cmd *cobra.Command) {
cmd.Flags().Duration(scanJobTimeoutFlagName, time.Duration(0),
"The length of time to wait before giving up on a scan job. Non-zero values should contain a"+
" corresponding time unit (e.g. 1s, 2m, 3h). A value of zero means don't timeout the scan job.")
cmd.Flags().Bool(deleteScanJobFlagName, true, "If true, delete a scan job either complete or failed")
}
func getScannerOpts(cmd *cobra.Command) (opts kube.ScannerOpts, err error) {
opts.ScanJobTimeout, err = cmd.Flags().GetDuration(scanJobTimeoutFlagName)
if err != nil {
return
}
opts.DeleteScanJob, err = cmd.Flags().GetBool(deleteScanJobFlagName)
if err != nil {
return
}
return
}