Skip to content

Commit

Permalink
tidy up the command line arguments and flags for the crd and schema g…
Browse files Browse the repository at this point in the history
…enerate commands
  • Loading branch information
Skarlso committed Oct 13, 2024
1 parent 3e4ba2f commit a83651c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 30 deletions.
44 changes: 33 additions & 11 deletions cmd/crd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
Expand All @@ -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 {
Expand All @@ -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...)
Expand Down
12 changes: 0 additions & 12 deletions cmd/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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.")
}
22 changes: 15 additions & 7 deletions cmd/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,32 @@ 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 {
return err
}

// 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()
Expand All @@ -51,15 +63,11 @@ 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)
}
}
}

return nil
}

func init() {
generateCmd.AddCommand(schemaCmd)
}

0 comments on commit a83651c

Please sign in to comment.