File tree Expand file tree Collapse file tree 5 files changed +41
-7
lines changed
plugins/golang/v4/scaffolds/internal/templates/webhooks Expand file tree Collapse file tree 5 files changed +41
-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,13 @@ 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 .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+ withOptionalBehavior . GetIfNotExistsAction () == IgnoreFile {
243+ log . Warnf ( "Skipping missing file: %s" , i .GetPath ())
244+ log .Warn ( "The code fragments will not be inserted." )
245+ return nil
246+ }
247247 }
248248 return fmt .Errorf ("failed to load previous model for %s: %w" , i .GetPath (), err )
249249 }
Original file line number Diff line number Diff line change @@ -34,6 +34,7 @@ type WebhookTest struct {
3434 machinery.MultiGroupMixin
3535 machinery.BoilerplateMixin
3636 machinery.ResourceMixin
37+ machinery.IfNotExistsActionMixin
3738
3839 Force bool
3940
@@ -78,7 +79,10 @@ func (f *WebhookTest) SetTemplateDefaults() error {
7879
7980 if f .Force {
8081 f .IfExistsAction = machinery .OverwriteFile
82+ } else {
83+ f .IfExistsAction = machinery .Error
8184 }
85+ f .IfNotExistsAction = machinery .IgnoreFile
8286
8387 return nil
8488}
You can’t perform that action at this time.
0 commit comments