Skip to content

Commit

Permalink
chore: run controller builder outside the repo dir
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed Feb 7, 2025
1 parent 0a48b49 commit 09da026
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (

type GenerateControllerOptions struct {
*options.GenerateOptions

Kind string
ProtoName string
}
Expand Down Expand Up @@ -97,10 +96,13 @@ func RunController(ctx context.Context, o *GenerateControllerOptions) error {
ProtoResource: o.ProtoName,
ProtoVersion: version,
}
err1 := scaffold.GenerateController(serviceName, o.ProtoName, cArgs)
err2 := scaffold.RegisterController(serviceName, o.ProtoName)
if err1 != nil || err2 != nil {
return errors.Join(err1, err2)
root, err := options.RepoRoot()
if err != nil {
return err
}
return nil

c := scaffold.NewControllerBuilder(root, serviceName, o.ProtoName)
err = errors.Join(err, c.GenerateController(cArgs))
err = errors.Join(err, c.RegisterController())
return err
}
69 changes: 30 additions & 39 deletions dev/tools/controllerbuilder/scaffold/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"go/token"
"os"
"path/filepath"
"runtime/debug"
"strings"
"text/template"

Expand All @@ -42,30 +41,33 @@ var funcMap = template.FuncMap{
"ToLower": strings.ToLower,
}

func RegisterController(service, kind string) error {
// Read register file
directControllerPkgPath, err := buildDirectControllerPath()
if err != nil {
return nil
func NewControllerBuilder(rootPath, service, proto string) *ControllerBuilder {
return &ControllerBuilder{
rootPath: rootPath,
service: service,
proto: proto,
}
registerFilePath := filepath.Join(directControllerPkgPath, "register", "register.go")
}

type ControllerBuilder struct {
rootPath string
service string
proto string
}

func (c *ControllerBuilder) RegisterController() error {
// Read register file
registerFilePath := filepath.Join(c.getDirectPath(), "register", "register.go")
fset := token.NewFileSet()
f, err := parser.ParseFile(fset, registerFilePath, nil, parser.ParseComments)
if err != nil {
return err
}

// Get main model name
bi, ok := debug.ReadBuildInfo()
if !ok {
return fmt.Errorf("could not read build info")
}
modelPath := strings.TrimSuffix(bi.Main.Path, currRelPath)

importPath := filepath.Join(modelPath, directControllerRelPath, service)
importPath := filepath.Join("github.com/GoogleCloudPlatform/k8s-config-connector", directControllerRelPath, c.service)
added := astutil.AddNamedImport(fset, f, "_", importPath)
if !added {
fmt.Printf("skip registering controller %s\n", service)
fmt.Printf("skip registering controller %s\n", c.service)
return nil
}

Expand All @@ -78,11 +80,11 @@ func RegisterController(service, kind string) error {
if err := FormatImports(registerFilePath, out.Bytes()); err != nil {
return err
}
color.HiGreen("New controller %s has been registered.\n", kind)
color.HiGreen("New controller %s has been registered.\n", c.proto)
return nil
}

func GenerateController(service, kind string, cArgs *ccTemplate.ControllerArgs) error {
func (c *ControllerBuilder) GenerateController(cArgs *ccTemplate.ControllerArgs) error {
tmpl, err := template.New(cArgs.Kind).Funcs(funcMap).Parse(ccTemplate.ControllerTemplate)
if err != nil {
return fmt.Errorf("parse controller template: %w", err)
Expand All @@ -93,7 +95,7 @@ func GenerateController(service, kind string, cArgs *ccTemplate.ControllerArgs)
return err
}

controllerFilePath, err := buildControllerPath(service, cArgs.ProtoResource)
controllerFilePath, err := c.getControllerPath()
if err != nil {
return err
}
Expand All @@ -112,31 +114,20 @@ func GenerateController(service, kind string, cArgs *ccTemplate.ControllerArgs)
if err := FormatImports(controllerFilePath, controllerOutput.Bytes()); err != nil {
return err
}
color.HiGreen("New controller %s has been generated.", kind)
color.HiGreen("New controller %s has been generated.", c.proto)
return nil
}

func buildDirectControllerPath() (string, error) {
pwd, err := os.Getwd()
if err != nil {
return "", fmt.Errorf("get current working directory: %w", err)
}
abs, err := filepath.Abs(pwd)
if err != nil {
return "", fmt.Errorf("get absolute path %s: %w", pwd, err)
}
seg := strings.Split(abs, currRelPath)
return filepath.Join(seg[0], directControllerRelPath), nil
func (c *ControllerBuilder) getDirectPath() string {
seg := strings.Split(c.rootPath, currRelPath)
return filepath.Join(seg[0], directControllerRelPath)
}

func buildControllerPath(service, protoResource string) (string, error) {
filename := strings.ToLower(protoResource) + "_controller.go"
directControllerPkgPath, err := buildDirectControllerPath()
if err != nil {
return "", nil
}
controllerDir := filepath.Join(directControllerPkgPath, service)
err = os.MkdirAll(controllerDir, os.ModePerm)
func (c *ControllerBuilder) getControllerPath() (string, error) {
filename := strings.ToLower(c.proto) + "_controller.go"
direct := c.getDirectPath()
controllerDir := filepath.Join(direct, c.service)
err := os.MkdirAll(controllerDir, os.ModePerm)
if err != nil {
return "", fmt.Errorf("create controller directory %s: %w", controllerDir, err)
}
Expand Down

0 comments on commit 09da026

Please sign in to comment.