From 38f3d0bd62fa62ff40bd36017232f73feec1a4d9 Mon Sep 17 00:00:00 2001 From: Jingyi Hu Date: Thu, 12 Sep 2024 20:16:42 +0000 Subject: [PATCH] tools: always capitalize first letter in proto message name --- .../generatetypes/generatetypescommand.go | 39 +++++++++++-------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/dev/tools/controllerbuilder/pkg/commands/generatetypes/generatetypescommand.go b/dev/tools/controllerbuilder/pkg/commands/generatetypes/generatetypescommand.go index 624b989b42..00a931911e 100644 --- a/dev/tools/controllerbuilder/pkg/commands/generatetypes/generatetypescommand.go +++ b/dev/tools/controllerbuilder/pkg/commands/generatetypes/generatetypescommand.go @@ -77,6 +77,10 @@ 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") + } + o.ResourceProtoName = capitalizeFirstRune(o.ResourceProtoName) gv, err := schema.ParseGroupVersion(o.APIVersion) if err != nil { @@ -122,23 +126,26 @@ 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 kind == "" { + kind = o.ResourceProtoName + } + 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) +}