Skip to content

Commit

Permalink
tools: always capitalize first letter in proto message name
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyih committed Sep 12, 2024
1 parent 3d7022e commit 38f3d0b
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,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 {
Expand Down Expand Up @@ -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)
}

0 comments on commit 38f3d0b

Please sign in to comment.