Skip to content

Commit

Permalink
BUILD/MINOR: kubebuilder: add comments for marker generator for kubeb…
Browse files Browse the repository at this point in the history
…uilder
  • Loading branch information
oktalz authored and mjuraga committed Dec 12, 2023
1 parent 4ad4353 commit 485b290
Show file tree
Hide file tree
Showing 61 changed files with 488 additions and 27 deletions.
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ models: spec swagger-check
rm -rf models/*_compare.go
rm -rf models/*_compare_test.go
go run cmd/struct_equal_generator/*.go -l ${PROJECT_PATH}/specification/copyright.txt ${PROJECT_PATH}/models
go run cmd/struct_tags_checker/*.go -l ${PROJECT_PATH}/specification/copyright.txt ${PROJECT_PATH}/models
go run cmd/struct_tags_checker/*.go ${PROJECT_PATH}/models
go run cmd/kubebuilder_marker_generator/*.go ${PROJECT_PATH}/models

.PHONY: swagger-check
swagger-check:
Expand Down
79 changes: 79 additions & 0 deletions cmd/kubebuilder_marker_generator/args.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package main

import (
_ "embed"
"fmt"
"io/fs"
"log"
"os"
"path"
"path/filepath"
"strings"
)

type Args struct {
Directory string
Selector string
Files []string
}

func (a *Args) Parse() error { //nolint:gocognit,unparam
selector, err := os.Getwd()
if err != nil {
log.Panic(err)
}
for i := 1; i < len(os.Args); i++ {
selector = os.Args[i]
}

isDirectory := false
file, err := os.Open(selector)
if err == nil {
var fileInfo fs.FileInfo
fileInfo, err = file.Stat()
if err == nil {
isDirectory = fileInfo.IsDir()
}
}

if selector == "*" || selector == "." || isDirectory {
err = filepath.Walk(selector, func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}
if info.IsDir() {
return nil
}
if strings.HasSuffix(path, "_compare.go") {
return nil
}
if strings.HasSuffix(path, "_test.go") {
return nil
}
if strings.HasSuffix(path, "_easyjson.go") {
return nil
}
if strings.HasSuffix(path, "_generated.go") {
return nil
}
fmt.Println(path) //nolint:forbidigo
if strings.HasSuffix(path, ".go") {
a.Files = append(a.Files, path)
}
return nil
})
if err != nil {
log.Panic(err)
}
} else {
a.Files = append(a.Files, selector)
}
a.Selector = selector
if isDirectory {
a.Directory = selector
} else {
a.Directory = path.Dir(selector)
}

return nil
}
81 changes: 81 additions & 0 deletions cmd/kubebuilder_marker_generator/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package main

import (
_ "embed"
"go/token"
"log"
"os"
"strings"

"github.com/sirkon/dst"
"github.com/sirkon/dst/decorator"
)

func main() {
// tool to add `json:",inline"` for embedded structs

args := Args{}
err := args.Parse()
if err != nil {
log.Panic(err)
}
for _, fileName := range args.Files {
err = generate(fileName)
if err != nil {
log.Panic(err)
}
}
}

func generate(fileName string) error { //nolint:gocognit,unparam
f, err := decorator.ParseFile(token.NewFileSet(), fileName, nil, 0)
if err != nil {
log.Fatal(err)
}

for _, decl := range f.Decls {
if genDecl, ok := decl.(*dst.GenDecl); ok && genDecl.Tok == token.TYPE {
for _, spec := range genDecl.Specs {
if typeSpec, ok := spec.(*dst.TypeSpec); ok {
if structType, ok := typeSpec.Type.(*dst.StructType); ok {
for _, field := range structType.Fields.List {
comments := field.Decorations().Start.All()
for _, comment := range comments {
if strings.HasPrefix(comment, "// Enum: [") {
field.Decorations().Before = dst.NewLine
newComment := `// +kubebuilder:validation:Enum=`
comment = strings.TrimPrefix(comment, "// Enum: [")
comment = strings.TrimSuffix(comment, "]")
for _, enum := range strings.Split(comment, " ") {
enum = strings.TrimSpace(enum)
newComment += enum
newComment += ";"
}
field.Decorations().Start.Append(newComment)
log.Printf("Adding comment for: %s: %+v %s\n", fileName, field.Type, newComment)
}
}
// if len(field.Names) > 0 {
// log.Printf("Comments before the field %s: %v\n", field.Names[0].Name, comments)
// }
}
}
}
}
}
}

outputFile, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
}
// decorator.Print(f)

err = decorator.Fprint(outputFile, f)
if err != nil {
log.Fatal(err)
}
defer outputFile.Close()

return nil
}
2 changes: 2 additions & 0 deletions models/aws_region.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 485b290

Please sign in to comment.