Skip to content

Commit 9da9bac

Browse files
handle missing boilerplate file gracefully during scaffolding
1 parent 7ee23df commit 9da9bac

File tree

5 files changed

+25
-2
lines changed

5 files changed

+25
-2
lines changed

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

Lines changed: 5 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
"path/filepath"
2223
"strings"
@@ -82,6 +83,10 @@ func (s *apiScaffolder) Scaffold() error {
8283
// Load the boilerplate
8384
boilerplate, err := afero.ReadFile(s.fs.FS, filepath.Join("hack", "boilerplate.go.txt"))
8485
if err != nil {
86+
if errors.Is(err, afero.ErrFileNotFound) {
87+
log.Printf("Warning: Boilerplate file 'hack/boilerplate.go.txt' not found. Please ensure it exists or update project settings if needed.")
88+
boilerplate = []byte("")
89+
}
8590
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)
8691
}
8792

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ 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.Println("Warning: Boilerplate file 'hack/boilerplate.go.txt' not found. Please ensure it exists or update project settings if needed.")
7374
boilerplate = []byte("")
7475
} else {
7576
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)

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

Lines changed: 7 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,12 @@ 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.Printf("Warning: Boilerplate file 'hack/boilerplate.go.txt' not found. Please ensure it exists or update project settings if needed.")
120+
boilerplate = []byte("")
121+
}else {
122+
return fmt.Errorf("unable to load boilerplate: %w", err)
123+
}
118124
}
119125
// Initialize the machinery.Scaffold that will write the files to disk
120126
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: 5 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,10 @@ 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.Printf("Warning: Boilerplate file 'hack/boilerplate.go.txt' not found. Please ensure it exists or update project settings if needed.")
82+
boilerplate = []byte("")
83+
}
7984
return fmt.Errorf("error scaffolding webhook: unable to load boilerplate: %w", err)
8085
}
8186

0 commit comments

Comments
 (0)