forked from knight42/kopilot
/
audit.go
51 lines (45 loc) · 1.33 KB
/
audit.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
package main
import (
"bytes"
"fmt"
"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
)
func newAuditCommand(opt option) *cobra.Command {
cf := genericclioptions.NewConfigFlags(true)
cmd := &cobra.Command{
Use: "audit TYPE NAME",
Short: "Audit a resource",
PreRunE: func(cmd *cobra.Command, args []string) error {
if opt.token == "" {
return fmt.Errorf("please specify the token for %s with ENV %s", opt.typ, envKopilotToken)
}
return nil
},
SilenceUsage: true,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
ns, _, _ := cf.ToRawKubeConfigLoader().Namespace()
data, err := getResourceInYAML(cf, ns, args[0], args[1])
if err != nil {
return fmt.Errorf("marshal object: %w", err)
}
var buf bytes.Buffer
_ = promptAudit.Execute(&buf, templateData{
Data: data,
Lang: opt.lang,
})
cmd.Println("Auditing...")
resp, err := createCompletion(cmd.Context(), opt, buf.String())
if err != nil {
return err
}
cmd.Println(resp)
return nil
},
}
flags := cmd.Flags()
flags.StringVar(cf.KubeConfig, "kubeconfig", *cf.KubeConfig, "Path to the kubeconfig file to use for CLI requests.")
flags.StringVarP(cf.Namespace, "namespace", "n", *cf.Namespace, "If present, the namespace scope for this CLI request")
return cmd
}