Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwenma committed Oct 4, 2024
1 parent 06905fe commit bcd0e5c
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apis/bigqueryconnection/v1alpha1/connection_reference.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,5 +152,5 @@ func (r *BigQueryConnectionConnectionRef) NormalizedExternal(ctx context.Context
return "", fmt.Errorf("BigQueryConnectionConnection is not ready yet.")
}
r.External = actualExternalRef
return "", nil
return r.External, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -142,19 +142,19 @@ func RunGenerateCRD(ctx context.Context, o *GenerateCRDOptions) error {

kind := o.ResourceKindName
if !scaffolder.TypeFileNotExist(kind) {
fmt.Printf("file %s already exists, skipping\n", scaffolder.GetTypeFile(kind))
fmt.Printf("file %s already exists, skipping\n", scaffolder.PathToTypeFile(kind))
} else {
err := scaffolder.AddTypeFile(kind, o.ResourceProtoName)
if err != nil {
return fmt.Errorf("add type file %s: %w", scaffolder.GetTypeFile(kind), err)
return fmt.Errorf("add type file %s: %w", scaffolder.PathToTypeFile(kind), err)
}
}
if !scaffolder.RefsFileNotExist(kind, o.ResourceProtoName) {
fmt.Printf("file %s already exists, skipping\n", scaffolder.GetTypeFile(kind))
if scaffolder.RefsFileExist(kind, o.ResourceProtoName) {
fmt.Printf("file %s already exists, skipping\n", scaffolder.PathToRefsFile(kind, o.ResourceProtoName))
} else {
err := scaffolder.AddRefsFile(kind, o.ResourceProtoName)
if err != nil {
return fmt.Errorf("add refs file %s: %w", scaffolder.GetRefsFile(kind, o.ResourceProtoName), err)
return fmt.Errorf("add refs file %s: %w", scaffolder.PathToRefsFile(kind, o.ResourceProtoName), err)
}
}
return nil
Expand Down
32 changes: 9 additions & 23 deletions dev/tools/controllerbuilder/scaffold/apis.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,22 @@ type APIScaffolder struct {
PackageProtoTag string
}

func (a *APIScaffolder) RefsFileNotExist(kind, resourceProtoName string) bool {
refsFilePath := a.GetRefsFile(kind, resourceProtoName)
func (a *APIScaffolder) RefsFileExist(kind, resourceProtoName string) bool {
refsFilePath := a.PathToRefsFile(kind, resourceProtoName)
_, err := os.Stat(refsFilePath)
if err == nil {
return false
return true
}
return errors.Is(err, os.ErrNotExist)
return !errors.Is(err, os.ErrNotExist)
}

func (a *APIScaffolder) GetRefsFile(kind, resourceProtoName string) string {
func (a *APIScaffolder) PathToRefsFile(kind, resourceProtoName string) string {
fileName := strings.ToLower(resourceProtoName) + "_reference.go"
return filepath.Join(a.BaseDir, a.GoPackage, fileName)
}

func (a *APIScaffolder) AddRefsFile(kind, resourceProtoName string) error {
refsFilePath := a.GetRefsFile(kind, resourceProtoName)
refsFilePath := a.PathToRefsFile(kind, resourceProtoName)
cArgs := &apis.APIArgs{
Group: a.Group,
Version: a.Version,
Expand All @@ -62,20 +62,6 @@ func (a *APIScaffolder) AddRefsFile(kind, resourceProtoName string) error {
return scaffoldRefsFile(refsFilePath, cArgs)
}

func ReadFromFile(path string) ([]byte, error) {
f, err := os.OpenFile(path, os.O_CREATE|os.O_RDWR, 0777)
if err != nil {
return nil, err
}
defer f.Close()
out := make([]byte, 1024)
i, err := f.Read(out)
if err != nil {
return nil, fmt.Errorf("read file %s: %w", path, err)
}
return out[:i], nil
}

func scaffoldRefsFile(path string, cArgs *apis.APIArgs) error {
tmpl, err := template.New(cArgs.Kind).Funcs(funcMap).Parse(apis.RefsHeaderTemplate)
if err != nil {
Expand All @@ -95,21 +81,21 @@ func scaffoldRefsFile(path string, cArgs *apis.APIArgs) error {
}

func (a *APIScaffolder) TypeFileNotExist(kind string) bool {
typeFilePath := a.GetTypeFile(kind)
typeFilePath := a.PathToTypeFile(kind)
_, err := os.Stat(typeFilePath)
if err == nil {
return false
}
return errors.Is(err, os.ErrNotExist)
}

func (a *APIScaffolder) GetTypeFile(kind string) string {
func (a *APIScaffolder) PathToTypeFile(kind string) string {
fileName := strings.ToLower(kind) + "_types.go"
return filepath.Join(a.BaseDir, a.GoPackage, fileName)
}

func (a *APIScaffolder) AddTypeFile(kind, proto string) error {
typeFilePath := a.GetTypeFile(kind)
typeFilePath := a.PathToTypeFile(kind)
cArgs := &apis.APIArgs{
Group: a.Group,
Version: a.Version,
Expand Down
2 changes: 1 addition & 1 deletion dev/tools/controllerbuilder/scaffold/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ func WriteToFile(path string, out []byte) error {
return fmt.Errorf("failed to create directory %q: %w", filepath.Dir(path), err)
}
// Use O_TRUNC to truncate the file
f, err := os.OpenFile(path, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0777)
f, err := os.OpenFile(path, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return err
}
Expand Down

0 comments on commit bcd0e5c

Please sign in to comment.