Skip to content

Commit

Permalink
Merge pull request #2686 from jingyih/fix_type_generator
Browse files Browse the repository at this point in the history
tools: require "--kind" and "--proto-resource" when generating types
  • Loading branch information
google-oss-prow[bot] authored Sep 19, 2024
2 parents ab984fb + fa5ee34 commit abe8356
Showing 1 changed file with 23 additions and 16 deletions.
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)

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)
}

0 comments on commit abe8356

Please sign in to comment.