Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 2 additions & 33 deletions pkg/generator/gen_main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,11 @@ const (
type Main struct {
// imports
OperatorSDKImport string
APIImport string
StubImport string

// Service defintion in pkg/apis/..
ServicePlural string
Service string
}

// renderMainFile generates the cmd/<projectName>/main.go file given a repo path ("github.com/coreos/play"), apiVersion ("v1alpha1"),
// api dir name ("play"), service ("PlayService"), and servicePlural ("PlayServicePlural").
//
// for example:
// renderMain(w, "github.com/coreos/play", "v1alpha1", "play", "PlayService", "PlayServicePlural" )
// output:
//
// package main
//
// import (
// "context"
//
// sdk "github.com/coreos/operator-sdk/pkg/sdk"
// api "github.com/coreos/play/pkg/apis/play/v1alpha1"
// stub "github.com/coreos/play/pkg/stub"
// )
//
// func main() {
// namespace := "default"
// sdk.Watch(api.PlayServicePlural, namespace, api.PlayService)
// sdk.Handle(&stub.Handler{})
// sdk.Run(context.TODO())
// }
//
func renderMainFile(w io.Writer, repo, version, apiDirName, service, servicePlural string) error {
// renderMainFile generates the cmd/<projectName>/main.go file given a repo path ("github.com/coreos/play")
func renderMainFile(w io.Writer, repo string) error {
t := template.New("cmd/<projectName>/main.go")
t, err := t.Parse(mainTmpl)
if err != nil {
Expand All @@ -57,10 +29,7 @@ func renderMainFile(w io.Writer, repo, version, apiDirName, service, servicePlur

m := Main{
OperatorSDKImport: sdkImport,
APIImport: filepath.Join(repo, apisDir, apiDirName, version),
StubImport: filepath.Join(repo, stubDir),
ServicePlural: servicePlural,
Service: service,
}
return t.Execute(w, m)
}
10 changes: 3 additions & 7 deletions pkg/generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,21 +90,17 @@ func (g *Generator) renderCmd() error {
if err := os.MkdirAll(cpDir, defaultDirFileMode); err != nil {
return err
}
return renderCmdFiles(cpDir, g.repoPath, version(g.apiVersion), apiDirName(g.apiVersion), g.kind, toPlural(g.kind))
return renderCmdFiles(cpDir, g.repoPath)
}

func renderCmdFiles(cmdProjectDir, repoPath, version, apiDirName, kind, kindPlural string) error {
func renderCmdFiles(cmdProjectDir, repoPath string) error {
buf := &bytes.Buffer{}
if err := renderMainFile(buf, repoPath, version, apiDirName, kind, kindPlural); err != nil {
if err := renderMainFile(buf, repoPath); err != nil {
return err
}
return ioutil.WriteFile(filepath.Join(cmdProjectDir, main), buf.Bytes(), defaultFileMode)
}

func toPlural(kind string) string {
return kind + "Plural"
}

func (g *Generator) renderConfig() error {
if err := os.MkdirAll(filepath.Join(g.projectName, configDir), defaultDirFileMode); err != nil {
return err
Expand Down
3 changes: 1 addition & 2 deletions pkg/generator/main_tmpl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import (
"context"

sdk "{{.OperatorSDKImport}}"
api "{{.APIImport}}"
stub "{{.StubImport}}"
)

func main() {
namespace := "default"
sdk.Watch(api.{{.ServicePlural}}, namespace, api.{{.Service}})
sdk.Watch("apps/v1", "Deployment", namespace)
sdk.Handle(&stub.Handler{})
sdk.Run(context.TODO())
}
Expand Down