File tree Expand file tree Collapse file tree 5 files changed +47
-7
lines changed
plugins/golang/v4/scaffolds/internal/templates/webhooks Expand file tree Collapse file tree 5 files changed +47
-7
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,17 @@ const (
3030 OverwriteFile
3131)
3232
33+ // IfNotExistsAction determines what to do if a file to be updated does not exist
34+ type IfNotExistsAction int
35+
36+ const (
37+ // ErrorIfNotExist returns an error and stops processing (default behavior)
38+ ErrorIfNotExist IfNotExistsAction = iota
39+
40+ // IgnoreFile skips the file and logs a message if it does not exist
41+ IgnoreFile
42+ )
43+
3344// File describes a file that will be written
3445type File struct {
3546 // Path is the file to write
@@ -40,4 +51,7 @@ type File struct {
4051
4152 // IfExistsAction determines what to do if the file exists
4253 IfExistsAction IfExistsAction
54+
55+ // IfNotExistsAction determines what to do if the file is missing (optional updates only)
56+ IfNotExistsAction IfNotExistsAction
4357}
Original file line number Diff line number Diff line change @@ -59,6 +59,11 @@ type Inserter interface {
5959 GetCodeFragments () CodeFragmentsMap
6060}
6161
62+ // HasIfNotExistsAction allows a template to define an action if the file is missing
63+ type HasIfNotExistsAction interface {
64+ GetIfNotExistsAction () IfNotExistsAction
65+ }
66+
6267// HasDomain allows the domain to be used on a template
6368type HasDomain interface {
6469 // InjectDomain sets the template domain
Original file line number Diff line number Diff line change @@ -153,3 +153,14 @@ func (m *ResourceMixin) InjectResource(res *resource.Resource) {
153153 m .Resource = res
154154 }
155155}
156+
157+ // IfNotExistsActionMixin provides file builders with an if-not-exists-action field
158+ type IfNotExistsActionMixin struct {
159+ // IfNotExistsAction determines what to do if the file does not exist
160+ IfNotExistsAction IfNotExistsAction
161+ }
162+
163+ // GetIfNotExistsAction implements Inserter
164+ func (m * IfNotExistsActionMixin ) GetIfNotExistsAction () IfNotExistsAction {
165+ return m .IfNotExistsAction
166+ }
Original file line number Diff line number Diff line change @@ -237,13 +237,21 @@ func doTemplate(t Template) ([]byte, error) {
237237func (s Scaffold ) updateFileModel (i Inserter , models map [string ]* File ) error {
238238 m , err := s .loadPreviousModel (i , models )
239239 if err != nil {
240- // TODO(kubebuilder/issues/4960): Create Machinery implementation to allow defining IfNotExistsAction
241- // If the file path starts with test/, warn and skip
242- // Workaround to allow projects be backwards compatible with previous versions
243- if strings .HasPrefix (i .GetPath (), "test/" ) {
244- log .Warn ("Skipping missing test file" , "file_path" , i .GetPath ())
245- log .Warn ("The code fragments will not be inserted." )
246- return nil
240+ if os .IsNotExist (err ) {
241+ if withOptionalBehavior , ok := i .(HasIfNotExistsAction ); ok {
242+ switch withOptionalBehavior .GetIfNotExistsAction () {
243+ case IgnoreFile :
244+ log .Warn ("Skipping missing file" , "file" , i .GetPath ())
245+ log .Warn ("The code fragments will not be inserted." )
246+ return nil
247+ case ErrorIfNotExist :
248+ return err
249+ default :
250+ return err
251+ }
252+ }
253+ // If inserter doesn't implement HasIfNotExistsAction, return the original error
254+ return err
247255 }
248256 return fmt .Errorf ("failed to load previous model for %s: %w" , i .GetPath (), err )
249257 }
Original file line number Diff line number Diff line change @@ -33,6 +33,7 @@ type WebhookTest struct {
3333 machinery.MultiGroupMixin
3434 machinery.BoilerplateMixin
3535 machinery.ResourceMixin
36+ machinery.IfNotExistsActionMixin
3637
3738 Force bool
3839
@@ -78,6 +79,7 @@ func (f *WebhookTest) SetTemplateDefaults() error {
7879 if f .Force {
7980 f .IfExistsAction = machinery .OverwriteFile
8081 }
82+ f .IfNotExistsAction = machinery .IgnoreFile
8183
8284 return nil
8385}
You can’t perform that action at this time.
0 commit comments