Skip to content

Commit 20c4fe6

Browse files
authored
Merge pull request #4518 from sarthaksarthak9/boilerplate
✨ handle missing boilerplate file gracefully during scaffolding
2 parents be55cca + f88aa54 commit 20c4fe6

File tree

4 files changed

+41
-4
lines changed

4 files changed

+41
-4
lines changed

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

Lines changed: 15 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,22 @@ 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 "+
93+
"add the license file or configure your project accordingly.",
94+
boilerplatePath, err)
95+
boilerplate = []byte("")
96+
} else {
97+
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)
98+
}
8699
}
87100

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

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ 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 "+
76+
"add the license file or configure your project accordingly.",
77+
hack.DefaultBoilerplatePath, err)
7378
boilerplate = []byte("")
7479
} else {
7580
return fmt.Errorf("error scaffolding API/controller: unable to load boilerplate: %w", err)

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

Lines changed: 10 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,15 @@ 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 "+
121+
"add the license file or configure your project accordingly.",
122+
s.boilerplatePath, err)
123+
boilerplate = []byte("")
124+
} else {
125+
return fmt.Errorf("unable to load boilerplate: %w", err)
126+
}
118127
}
119128
// Initialize the machinery.Scaffold that will write the files to disk
120129
scaffold = machinery.NewScaffold(s.fs,

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

Lines changed: 11 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

@@ -76,7 +77,16 @@ func (s *webhookScaffolder) Scaffold() error {
7677
// Load the boilerplate
7778
boilerplate, err := afero.ReadFile(s.fs.FS, hack.DefaultBoilerplatePath)
7879
if err != nil {
79-
return fmt.Errorf("error scaffolding webhook: unable to load boilerplate: %w", err)
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 "+
84+
"add the license file or configure your project accordingly.",
85+
hack.DefaultBoilerplatePath, err)
86+
boilerplate = []byte("")
87+
} else {
88+
return fmt.Errorf("error scaffolding webhook: unable to load boilerplate: %w", err)
89+
}
8090
}
8191

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

0 commit comments

Comments
 (0)