Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ handle missing boilerplate file gracefully during scaffolding #4518

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package scaffolds

import (
"errors"
"fmt"
"path/filepath"
"strings"
Expand Down Expand Up @@ -79,10 +80,21 @@ func (s *apiScaffolder) Scaffold() error {
return err
}

// Define the boilerplate file path
boilerplatePath := filepath.Join("hack", "boilerplate.go.txt")

// Load the boilerplate
boilerplate, err := afero.ReadFile(s.fs.FS, filepath.Join("hack", "boilerplate.go.txt"))
boilerplate, err := afero.ReadFile(s.fs.FS, boilerplatePath)
if err != nil {
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)
if errors.Is(err, afero.ErrFileNotFound) {
log.Warnf("Unable to find %s : %s .\n"+
"This file is used to generate the license header in the project.\n"+
"Note that controller-gen will also use this. Therefore, ensure that you add the license file or configure your project accordingly.",
boilerplatePath, err)
boilerplate = []byte("")
}else{
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)
}
sarthaksarthak9 marked this conversation as resolved.
Show resolved Hide resolved
}

// Initialize the machinery.Scaffold that will write the files to disk
Expand Down
4 changes: 4 additions & 0 deletions pkg/plugins/golang/v4/scaffolds/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (s *apiScaffolder) Scaffold() error {
boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath)
if err != nil {
if errors.Is(err, afero.ErrFileNotFound) {
log.Warnf("Unable to find %s: %s.\n"+
"This file is used to generate the license header in the project.\n"+
"Note that controller-gen will also use this. Therefore, ensure that you add the license file or configure your project accordingly.",
hack.DefaultBoilerplatePath, err)
boilerplate = []byte("")
} else {
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)
Expand Down
10 changes: 9 additions & 1 deletion pkg/plugins/golang/v4/scaffolds/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package scaffolds

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -114,7 +115,14 @@ func (s *initScaffolder) Scaffold() error {

boilerplate, err := afero.ReadFile(s.fs.FS, s.boilerplatePath)
if err != nil {
return err
if errors.Is(err, afero.ErrFileNotFound) {
log.Warnf("Unable to find %s: %s.\n"+ "This file is used to generate the license header in the project.\n"+
"Note that controller-gen will also use this. Therefore, ensure that you add the license file or configure your project accordingly.",
s.boilerplatePath, err)
boilerplate = []byte("")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why you added it ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#4518 (comment)

Ig, we have to handle it here also and yesterday you had also mentioned this one.

}else {
return fmt.Errorf("unable to load boilerplate: %w", err)
sarthaksarthak9 marked this conversation as resolved.
Show resolved Hide resolved
}
}
// Initialize the machinery.Scaffold that will write the files to disk
scaffold = machinery.NewScaffold(s.fs,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,13 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
{{ if .BoilerplatePath -}}
$(CONTROLLER_GEN) object:headerFile={{printf "%q" .BoilerplatePath}} paths="./..."
@if [ -f {{ .BoilerplatePath }} ]; then \
$(CONTROLLER_GEN) object:headerFile={{printf "%q" .BoilerplatePath}} paths="./..."; \
else \
echo "Warning: Boilerplate file '{{ .BoilerplatePath }}' not found. Proceeding without it."; \
echo "You can create it later and regenerate code with the boilerplate using 'make generate'."; \
$(CONTROLLER_GEN) object paths="./..."; \
fi
Copy link
Member

@camilamacedo86 camilamacedo86 Jan 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sarthaksarthak9

When we change something that change the scaffolds we need to run make generate so that all samples will be updated. Otherwise, we cannot move forward and get it merged. But you can wait I will review it properly better and provide a good guidance.

{{- else -}}
$(CONTROLLER_GEN) object paths="./..."
{{- end }}
Expand Down
11 changes: 10 additions & 1 deletion pkg/plugins/golang/v4/scaffolds/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package scaffolds

import (
"errors"
"fmt"
"strings"

Expand Down Expand Up @@ -76,7 +77,15 @@ func (s *webhookScaffolder) Scaffold() error {
// Load the boilerplate
boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath)
if err != nil {
return fmt.Errorf("error scaffolding webhook: unable to load boilerplate: %w", err)
if errors.Is(err, afero.ErrFileNotFound) {
log.Warnf("Unable to find %s : %s .\n"+
"This file is used to generate the license header in the project.\n"+
"Note that controller-gen will also use this. Therefore, ensure that you add the license file or configure your project accordingly.",
hack.DefaultBoilerplatePath, err)
boilerplate = []byte("")
}else{
return fmt.Errorf("error scaffolding webhook: unable to load boilerplate: %w", err)
}
}

// Initialize the machinery.Scaffold that will write the files to disk
Expand Down