Skip to content

✨ handle missing boilerplate file gracefully during scaffolding #4518

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

Merged
merged 1 commit into from
Feb 5, 2025
Merged
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
17 changes: 15 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,22 @@ 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)
}
}

// Initialize the machinery.Scaffold that will write the files to disk
Expand Down
5 changes: 5 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,11 @@ 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
11 changes: 10 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,15 @@ 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("")
} else {
return fmt.Errorf("unable to load boilerplate: %w", err)
}
}
// Initialize the machinery.Scaffold that will write the files to disk
scaffold = machinery.NewScaffold(s.fs,
Expand Down
12 changes: 11 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,16 @@ 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
Loading