Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools: require "--kind" and "--proto-resource" when generating types #2686

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ func RunGenerateCRD(ctx context.Context, o *GenerateCRDOptions) error {
if o.GenerateOptions.ProtoSourcePath == "" {
return fmt.Errorf("`proto-source-path` is required")
}
if o.ResourceProtoName == "" {
return fmt.Errorf("`--proto-resource` is required")
}
if o.ResourceKindName == "" {
return fmt.Errorf("`--kind` is required")
}
o.ResourceProtoName = capitalizeFirstRune(o.ResourceProtoName)
Comment on lines +80 to +86
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if neither is provided previously? IIRC, if --kind and --proto-resource is not given, this tool generates everything except the "_types.go" file. Maybe we'd better still keep this functionality and only require both if either one is given.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually I never tried running the command without --kind and --proto-resource. What is the intended behavior?

  • Prior to commit f5960e4, It looks like the tool will re-generate (a.k.a update) all generated types for the given GCP API. This function is already lost after the commit, the question is do we want to bring it back?
  • After commit f5960e4, we need to explicitly provide --kind, and the tool will re-generate (a.k.a. update) all generated types for the given kind.


gv, err := schema.ParseGroupVersion(o.APIVersion)
if err != nil {
Expand Down Expand Up @@ -122,23 +129,23 @@ func RunGenerateCRD(ctx context.Context, o *GenerateCRDOptions) error {
return err
}

if o.ResourceProtoName != "" {
kind := o.ResourceKindName
if kind == "" {
output := []rune{unicode.ToUpper(rune(o.ResourceProtoName[0]))}
for _, val := range o.ResourceProtoName[1:] {
output = append(output, val)
}
kind = string(output)
}
if !scaffolder.TypeFileNotExist(kind) {
fmt.Printf("file %s already exists, skipping\n", scaffolder.GetTypeFile(kind))
} else {
err := scaffolder.AddTypeFile(kind, o.ResourceProtoName)
if err != nil {
return fmt.Errorf("add type file %s: %w", scaffolder.GetTypeFile(kind), err)
}
kind := o.ResourceKindName
if !scaffolder.TypeFileNotExist(kind) {
fmt.Printf("file %s already exists, skipping\n", scaffolder.GetTypeFile(kind))
} else {
err := scaffolder.AddTypeFile(kind, o.ResourceProtoName)
if err != nil {
return fmt.Errorf("add type file %s: %w", scaffolder.GetTypeFile(kind), err)
}
}
return nil
}

func capitalizeFirstRune(s string) string {
if s == "" {
return s
}
runes := []rune(s)
runes[0] = unicode.ToUpper(runes[0])
return string(runes)
}
Loading