Skip to content

Commit 7ddda45

Browse files
migueleliaswebsarthaksarthak9
authored andcommitted
handle missing boilerplate file gracefully during scaffolding
1 parent 7ee23df commit 7ddda45

File tree

5 files changed

+42
-4
lines changed

5 files changed

+42
-4
lines changed

pkg/plugins/golang/deploy-image/v1alpha1/scaffolds/api.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package scaffolds
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"path/filepath"
2223
"strings"
@@ -79,10 +80,21 @@ func (s *apiScaffolder) Scaffold() error {
7980
return err
8081
}
8182

83+
// Define the boilerplate file path
84+
boilerplatePath := filepath.Join("hack", "boilerplate.go.txt")
85+
8286
// Load the boilerplate
83-
boilerplate, err := afero.ReadFile(s.fs.FS, filepath.Join("hack", "boilerplate.go.txt"))
87+
boilerplate, err := afero.ReadFile(s.fs.FS, boilerplatePath)
8488
if err != nil {
85-
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)
89+
if errors.Is(err, afero.ErrFileNotFound) {
90+
log.Warnf("Unable to find %s : %s .\n"+
91+
"This file is used to generate the license header in the project.\n"+
92+
"Note that controller-gen will also use this. Therefore, ensure that you add the license file or configure your project accordingly.",
93+
boilerplatePath, err)
94+
boilerplate = []byte("")
95+
}else{
96+
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)
97+
}
8698
}
8799

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

pkg/plugins/golang/v4/scaffolds/api.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func (s *apiScaffolder) Scaffold() error {
7070
boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath)
7171
if err != nil {
7272
if errors.Is(err, afero.ErrFileNotFound) {
73+
log.Warnf("Unable to find %s: %s.\n"+
74+
"This file is used to generate the license header in the project.\n"+
75+
"Note that controller-gen will also use this. Therefore, ensure that you add the license file or configure your project accordingly.",
76+
hack.DefaultBoilerplatePath, err)
7377
boilerplate = []byte("")
7478
} else {
7579
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)

pkg/plugins/golang/v4/scaffolds/init.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package scaffolds
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"strings"
2223

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

115116
boilerplate, err := afero.ReadFile(s.fs.FS, s.boilerplatePath)
116117
if err != nil {
117-
return err
118+
if errors.Is(err, afero.ErrFileNotFound) {
119+
log.Warnf("Unable to find %s: %s.\n"+ "This file is used to generate the license header in the project.\n"+
120+
"Note that controller-gen will also use this. Therefore, ensure that you add the license file or configure your project accordingly.",
121+
s.boilerplatePath, err)
122+
boilerplate = []byte("")
123+
}else {
124+
return fmt.Errorf("unable to load boilerplate: %w", err)
125+
}
118126
}
119127
// Initialize the machinery.Scaffold that will write the files to disk
120128
scaffold = machinery.NewScaffold(s.fs,

pkg/plugins/golang/v4/scaffolds/internal/templates/makefile.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,13 @@ manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and Cust
123123
.PHONY: generate
124124
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
125125
{{ if .BoilerplatePath -}}
126-
$(CONTROLLER_GEN) object:headerFile={{printf "%q" .BoilerplatePath}} paths="./..."
126+
@if [ -f {{ .BoilerplatePath }} ]; then \
127+
$(CONTROLLER_GEN) object:headerFile={{printf "%q" .BoilerplatePath}} paths="./..."; \
128+
else \
129+
echo "Warning: Boilerplate file '{{ .BoilerplatePath }}' not found. Proceeding without it."; \
130+
echo "You can create it later and regenerate code with the boilerplate using 'make generate'."; \
131+
$(CONTROLLER_GEN) object paths="./..."; \
132+
fi
127133
{{- else -}}
128134
$(CONTROLLER_GEN) object paths="./..."
129135
{{- end }}

pkg/plugins/golang/v4/scaffolds/webhook.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package scaffolds
1818

1919
import (
20+
"errors"
2021
"fmt"
2122
"strings"
2223

@@ -76,6 +77,13 @@ func (s *webhookScaffolder) Scaffold() error {
7677
// Load the boilerplate
7778
boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath)
7879
if err != nil {
80+
if errors.Is(err, afero.ErrFileNotFound) {
81+
log.Warnf("Unable to find %s : %s .\n"+
82+
"This file is used to generate the license header in the project.\n"+
83+
"Note that controller-gen will also use this. Therefore, ensure that you add the license file or configure your project accordingly.",
84+
hack.DefaultBoilerplatePath, err)
85+
boilerplate = []byte("")
86+
}
7987
return fmt.Errorf("error scaffolding webhook: unable to load boilerplate: %w", err)
8088
}
8189

0 commit comments

Comments
 (0)