From a83651c372299cda5769f02fed76e80e9452e37b Mon Sep 17 00:00:00 2001 From: Gergely Brautigam <182850+Skarlso@users.noreply.github.com> Date: Sun, 13 Oct 2024 20:09:56 +0200 Subject: [PATCH] tidy up the command line arguments and flags for the crd and schema generate commands --- cmd/crd.go | 44 +++++++++++++++++++++++++++++++++----------- cmd/generate.go | 12 ------------ cmd/schema.go | 22 +++++++++++++++------- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/cmd/crd.go b/cmd/crd.go index 06294f8..0c3cecc 100644 --- a/cmd/crd.go +++ b/cmd/crd.go @@ -25,12 +25,30 @@ var crdCmd = &cobra.Command{ RunE: runGenerate, } +type crdGenArgs struct { + comments bool + minimal bool + skipRandom bool + output string + format string + stdOut bool +} + +var crdArgs = &crdGenArgs{} + type Handler interface { CRDs() ([]*v1beta1.CustomResourceDefinition, error) } func init() { generateCmd.AddCommand(crdCmd) + f := crdCmd.PersistentFlags() + f.BoolVarP(&crdArgs.comments, "comments", "m", false, "If set, it will add descriptions as comments to each line where available.") + f.BoolVarP(&crdArgs.minimal, "minimal", "l", false, "If set, only the minimal required example yaml is generated.") + f.BoolVar(&crdArgs.skipRandom, "no-random", false, "Skip generating random values that satisfy the property patterns.") + f.StringVarP(&crdArgs.output, "output", "o", "", "The location of the output file. Default is next to the CRD.") + f.StringVarP(&crdArgs.format, "format", "f", FormatYAML, "The format in which to output. Default is YAML. Options are: yaml, html.") + f.BoolVarP(&crdArgs.stdOut, "stdout", "s", false, "If set, it will output the generated content to stdout.") } func runGenerate(_ *cobra.Command, _ []string) error { @@ -39,20 +57,20 @@ func runGenerate(_ *cobra.Command, _ []string) error { return err } - if args.format == FormatHTML { + if crdArgs.format == FormatHTML { if err := pkg.LoadTemplates(); err != nil { return fmt.Errorf("failed to load templates: %w", err) } } // determine location of output - if args.output == "" { + if crdArgs.output == "" { loc, err := os.Executable() if err != nil { return fmt.Errorf("failed to determine executable location: %w", err) } - args.output = filepath.Dir(loc) + crdArgs.output = filepath.Dir(loc) } crds, err := crdHandler.CRDs() @@ -62,27 +80,31 @@ func runGenerate(_ *cobra.Command, _ []string) error { var w io.WriteCloser - if args.format == FormatHTML { - if args.stdOut { + if crdArgs.format == FormatHTML { + if crdArgs.stdOut { w = os.Stdout } else { - w, err = os.Create(args.output) + w, err = os.Create(crdArgs.output) if err != nil { return fmt.Errorf("failed to create output file: %w", err) } - defer w.Close() + defer func() { + if err := w.Close(); err != nil { + _, _ = fmt.Fprintf(os.Stderr, "failed to close output file: %s", err.Error()) + } + }() } - return pkg.RenderContent(w, crds, args.comments, args.minimal) + return pkg.RenderContent(w, crds, crdArgs.comments, crdArgs.minimal) } var errs []error //nolint:prealloc // nope for _, crd := range crds { - if args.stdOut { + if crdArgs.stdOut { w = os.Stdout } else { - outputLocation := filepath.Join(args.output, crd.Name+"_sample."+args.format) + outputLocation := filepath.Join(crdArgs.output, crd.Name+"_sample."+crdArgs.format) // closed later during render outputFile, err := os.Create(outputLocation) if err != nil { @@ -94,7 +116,7 @@ func runGenerate(_ *cobra.Command, _ []string) error { w = outputFile } - errs = append(errs, pkg.Generate(crd, w, args.comments, args.minimal, args.skipRandom)) + errs = append(errs, pkg.Generate(crd, w, crdArgs.comments, crdArgs.minimal, crdArgs.skipRandom)) } return errors.Join(errs...) diff --git a/cmd/generate.go b/cmd/generate.go index e1a658e..d9d0a1f 100644 --- a/cmd/generate.go +++ b/cmd/generate.go @@ -11,12 +11,6 @@ type rootArgs struct { username string password string token string - output string - format string - stdOut bool - comments bool - minimal bool - skipRandom bool } var ( @@ -39,10 +33,4 @@ func init() { f.StringVar(&args.username, "username", "", "Optional username to authenticate a URL.") f.StringVar(&args.password, "password", "", "Optional password to authenticate a URL.") f.StringVar(&args.token, "token", "", "A bearer token to authenticate a URL.") - f.StringVarP(&args.output, "output", "o", "", "The location of the output file. Default is next to the CRD.") - f.StringVarP(&args.format, "format", "f", FormatYAML, "The format in which to output. Default is YAML. Options are: yaml, html.") - f.BoolVarP(&args.stdOut, "stdout", "s", false, "If set, it will output the generated content to stdout.") - f.BoolVarP(&args.comments, "comments", "m", false, "If set, it will add descriptions as comments to each line where available.") - f.BoolVarP(&args.minimal, "minimal", "l", false, "If set, only the minimal required example yaml is generated.") - f.BoolVar(&args.skipRandom, "no-random", false, "Skip generating random values that satisfy the property patterns.") } diff --git a/cmd/schema.go b/cmd/schema.go index 96242c0..c833743 100644 --- a/cmd/schema.go +++ b/cmd/schema.go @@ -16,6 +16,18 @@ var schemaCmd = &cobra.Command{ RunE: runGenerateSchema, } +type schemaCmdArgs struct { + outputFolder string +} + +var schemaArgs = &schemaCmdArgs{} + +func init() { + generateCmd.AddCommand(schemaCmd) + f := schemaCmd.PersistentFlags() + f.StringVarP(&schemaArgs.outputFolder, "output", "o", ".", "output location of the generated schema files") +} + func runGenerateSchema(_ *cobra.Command, _ []string) error { crdHandler, err := constructHandler(args) if err != nil { @@ -23,13 +35,13 @@ func runGenerateSchema(_ *cobra.Command, _ []string) error { } // determine location of output - if args.output == "" { + if schemaArgs.outputFolder == "" { loc, err := os.Executable() if err != nil { return fmt.Errorf("failed to determine executable location: %w", err) } - args.output = filepath.Dir(loc) + schemaArgs.outputFolder = filepath.Dir(loc) } crds, err := crdHandler.CRDs() @@ -51,7 +63,7 @@ func runGenerateSchema(_ *cobra.Command, _ []string) error { } const perm = 0o600 - if err := os.WriteFile(filepath.Join(args.output, crd.Spec.Names.Kind+"."+crd.Spec.Group+"."+v.Name+".json"), content, perm); err != nil { + if err := os.WriteFile(filepath.Join(schemaArgs.outputFolder, crd.Spec.Names.Kind+"."+crd.Spec.Group+"."+v.Name+".schema.json"), content, perm); err != nil { return fmt.Errorf("failed to write schema: %w", err) } } @@ -59,7 +71,3 @@ func runGenerateSchema(_ *cobra.Command, _ []string) error { return nil } - -func init() { - generateCmd.AddCommand(schemaCmd) -}