1
0
Fork 0
mirror of https://github.com/kyverno/kyverno.git synced 2025-03-09 17:37:12 +00:00
kyverno/cmd/cli/kubectl-kyverno/commands/create/metrics-config/command.go
Charles-Edouard Brétéché dc71610df7
refactor: cli commands tests and error handling (#8367)
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
2023-09-13 09:53:19 +00:00

57 lines
1.8 KiB
Go

package metricsconfig
import (
"os"
"text/template"
"github.com/Masterminds/sprig/v3"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/command"
"github.com/kyverno/kyverno/cmd/cli/kubectl-kyverno/commands/create/templates"
"github.com/spf13/cobra"
)
type namespaces struct {
Include []string `json:"include,omitempty"`
Exclude []string `json:"exclude,omitempty"`
}
type options struct {
Name string
Namespace string
Namespaces namespaces
}
func Command() *cobra.Command {
var path string
var options options
cmd := &cobra.Command{
Use: "metrics-config",
Short: command.FormatDescription(true, websiteUrl, false, description...),
Long: command.FormatDescription(false, websiteUrl, false, description...),
Example: command.FormatExamples(examples...),
Args: cobra.NoArgs,
SilenceUsage: true,
RunE: func(cmd *cobra.Command, _ []string) error {
tmpl, err := template.New("metricsconfig").Funcs(sprig.HermeticTxtFuncMap()).Parse(templates.MetricsConfigTemplate)
if err != nil {
return err
}
output := cmd.OutOrStdout()
if path != "" {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
output = file
}
return tmpl.Execute(output, options)
},
}
cmd.Flags().StringVarP(&path, "output", "o", "", "Output path (uses standard console output if not set)")
cmd.Flags().StringVarP(&options.Name, "name", "n", "kyverno-metrics", "Name")
cmd.Flags().StringVar(&options.Namespace, "namespace", "kyverno", "Namespace")
cmd.Flags().StringSliceVarP(&options.Namespaces.Include, "include", "i", []string{}, "Included namespaces")
cmd.Flags().StringSliceVarP(&options.Namespaces.Exclude, "exclude", "e", []string{}, "Excluded namespaces")
return cmd
}