Skip to content

Commit c7c98f8

Browse files
committed
plugins/addon: perform replacements for type and controller
- controller runs in a second phase and needs the Plugins configured on the Scaffold - fix path to the api/<version>/<resource>_type.go file for file replacement - make the addon plugin work with the 2 phase run (api then controller) by not failing if the manifest/channel file has already been generated
1 parent 5f27a72 commit c7c98f8

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

pkg/scaffold/api.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,9 +228,13 @@ func (api *API) scaffoldV2() error {
228228
if api.DoController {
229229
fmt.Println(filepath.Join("controllers", fmt.Sprintf("%s_controller.go", strings.ToLower(r.Kind))))
230230

231+
scaffold := &Scaffold{
232+
Plugins: api.Plugins,
233+
}
234+
231235
ctrlScaffolder := &resourcev2.Controller{Resource: r}
232236
testsuiteScaffolder := &resourcev2.ControllerSuiteTest{Resource: r}
233-
err := (&Scaffold{}).Execute(
237+
err := scaffold.Execute(
234238
api.buildUniverse(),
235239
input.Options{},
236240
testsuiteScaffolder,

plugins/addon/channel.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ func ExampleChannel(u *model.Universe) error {
3232
m := &model.File{
3333
Path: filepath.Join("channels", "stable"),
3434
Contents: exampleChannel,
35-
IfExistsAction: input.Error,
35+
IfExistsAction: input.Skip,
3636
}
3737

38-
return AddFile(u, m)
38+
_, err := AddFile(u, m)
39+
return err
3940
}

plugins/addon/helpers.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,21 +16,24 @@ import (
1616

1717
type PluginFunc func(u *model.Universe) error
1818

19-
// AddFile adds the specified file to the model, returning a file if the file already exists
20-
func AddFile(u *model.Universe, add *model.File) error {
19+
// AddFile adds the specified file to the model.
20+
// If the file exists the function returns false and does not modify the Universe
21+
// If the file does not exist, the function returns true and adds the file to the Universe
22+
// If there is a problem with the file the function returns an error
23+
func AddFile(u *model.Universe, add *model.File) (bool, error) {
2124
p := add.Path
2225
if p == "" {
23-
return fmt.Errorf("path must be set")
26+
return false, fmt.Errorf("path must be set")
2427
}
2528

2629
for _, f := range u.Files {
2730
if f.Path == p {
28-
return fmt.Errorf("file already exists at path %q", p)
31+
return false, nil
2932
}
3033
}
3134

3235
u.Files = append(u.Files, add)
33-
return nil
36+
return true, nil
3437
}
3538

3639
// ReplaceFileIfExists replaces the specified file in the model by path

plugins/addon/manifest.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,12 @@ func ExampleManifest(u *model.Universe) error {
3535
m := &model.File{
3636
Path: filepath.Join("channels", "packages", packageName, exampleManifestVersion, "manifest.yaml"),
3737
Contents: exampleManifestContents,
38-
IfExistsAction: input.Error,
38+
IfExistsAction: input.Skip,
3939
}
4040

41-
return AddFile(u, m)
41+
_, err := AddFile(u, m)
42+
43+
return err
4244
}
4345

4446
// getPackageName returns the (default) name of the declarative package

plugins/addon/type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func ReplaceTypes(u *model.Universe) error {
1919
}
2020

2121
m := &model.File{
22-
Path: filepath.Join("controllers", strings.ToLower(u.Resource.Kind)+"_controller.go"),
22+
Path: filepath.Join("api", u.Resource.Version, strings.ToLower(u.Resource.Kind)+"_types.go"),
2323
Contents: contents,
2424
IfExistsAction: input.Error,
2525
}

0 commit comments

Comments
 (0)