Skip to content

Commit

Permalink
plugins/addon: perform replacements for type and controller
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
johnsonj committed Oct 1, 2019
1 parent 5f27a72 commit c7c98f8
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 11 deletions.
6 changes: 5 additions & 1 deletion pkg/scaffold/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,9 +228,13 @@ func (api *API) scaffoldV2() error {
if api.DoController {
fmt.Println(filepath.Join("controllers", fmt.Sprintf("%s_controller.go", strings.ToLower(r.Kind))))

scaffold := &Scaffold{
Plugins: api.Plugins,
}

ctrlScaffolder := &resourcev2.Controller{Resource: r}
testsuiteScaffolder := &resourcev2.ControllerSuiteTest{Resource: r}
err := (&Scaffold{}).Execute(
err := scaffold.Execute(
api.buildUniverse(),
input.Options{},
testsuiteScaffolder,
Expand Down
5 changes: 3 additions & 2 deletions plugins/addon/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ func ExampleChannel(u *model.Universe) error {
m := &model.File{
Path: filepath.Join("channels", "stable"),
Contents: exampleChannel,
IfExistsAction: input.Error,
IfExistsAction: input.Skip,
}

return AddFile(u, m)
_, err := AddFile(u, m)
return err
}
13 changes: 8 additions & 5 deletions plugins/addon/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,24 @@ import (

type PluginFunc func(u *model.Universe) error

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

for _, f := range u.Files {
if f.Path == p {
return fmt.Errorf("file already exists at path %q", p)
return false, nil
}
}

u.Files = append(u.Files, add)
return nil
return true, nil
}

// ReplaceFileIfExists replaces the specified file in the model by path
Expand Down
6 changes: 4 additions & 2 deletions plugins/addon/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,12 @@ func ExampleManifest(u *model.Universe) error {
m := &model.File{
Path: filepath.Join("channels", "packages", packageName, exampleManifestVersion, "manifest.yaml"),
Contents: exampleManifestContents,
IfExistsAction: input.Error,
IfExistsAction: input.Skip,
}

return AddFile(u, m)
_, err := AddFile(u, m)

return err
}

// getPackageName returns the (default) name of the declarative package
Expand Down
2 changes: 1 addition & 1 deletion plugins/addon/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func ReplaceTypes(u *model.Universe) error {
}

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

0 comments on commit c7c98f8

Please sign in to comment.