File tree Expand file tree Collapse file tree 5 files changed +49
-7
lines changed
plugins/golang/v4/scaffolds/internal/templates/webhooks Expand file tree Collapse file tree 5 files changed +49
-7
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,17 @@ const (
30
30
OverwriteFile
31
31
)
32
32
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
+
33
44
// File describes a file that will be written
34
45
type File struct {
35
46
// Path is the file to write
@@ -40,4 +51,7 @@ type File struct {
40
51
41
52
// IfExistsAction determines what to do if the file exists
42
53
IfExistsAction IfExistsAction
54
+
55
+ // IfNotExistsAction determines what to do if the file is missing (optional updates only)
56
+ IfNotExistsAction IfNotExistsAction
43
57
}
Original file line number Diff line number Diff line change @@ -59,6 +59,11 @@ type Inserter interface {
59
59
GetCodeFragments () CodeFragmentsMap
60
60
}
61
61
62
+ // HasIfNotExistsAction allows a template to define an action if the file is missing
63
+ type HasIfNotExistsAction interface {
64
+ GetIfNotExistsAction () IfNotExistsAction
65
+ }
66
+
62
67
// HasDomain allows the domain to be used on a template
63
68
type HasDomain interface {
64
69
// 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) {
153
153
m .Resource = res
154
154
}
155
155
}
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) {
237
237
func (s Scaffold ) updateFileModel (i Inserter , models map [string ]* File ) error {
238
238
m , err := s .loadPreviousModel (i , models )
239
239
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 .Warnf ("Skipping missing test file: %s" , 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: %s" , 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 HasIfNotExistsAction is not implemented, return the original error
254
+ return err
247
255
}
248
256
return fmt .Errorf ("failed to load previous model for %s: %w" , i .GetPath (), err )
249
257
}
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ type WebhookTest struct {
34
34
machinery.MultiGroupMixin
35
35
machinery.BoilerplateMixin
36
36
machinery.ResourceMixin
37
+ machinery.IfNotExistsActionMixin
37
38
38
39
Force bool
39
40
@@ -78,7 +79,10 @@ func (f *WebhookTest) SetTemplateDefaults() error {
78
79
79
80
if f .Force {
80
81
f .IfExistsAction = machinery .OverwriteFile
82
+ } else {
83
+ f .IfExistsAction = machinery .Error
81
84
}
85
+ f .IfNotExistsAction = machinery .IgnoreFile
82
86
83
87
return nil
84
88
}
You can’t perform that action at this time.
0 commit comments