Skip to content

pkg/generator: add cmd/main.go generator#29

Merged
fanminshi merged 1 commit intooperator-framework:masterfrom
fanminshi:gen_main
Feb 21, 2018
Merged

pkg/generator: add cmd/main.go generator#29
fanminshi merged 1 commit intooperator-framework:masterfrom
fanminshi:gen_main

Conversation

@fanminshi
Copy link
Copy Markdown
Contributor

uses "text/template" to generate cmd/main.go file.

@fanminshi
Copy link
Copy Markdown
Contributor Author

manual test:

func TestGenMain(t *testing.T) {
	buf := &bytes.Buffer{}
	if err := renderMain(buf, "github.com/coreos/play", "v1alpha1", "play", "PlayService", "PlayServicePlural"); err != nil {
		t.Error(err)
		return
	}
	if err := ioutil.WriteFile("./main.go", buf.Bytes(), 0644); err != nil {
		t.Error(err)
	}
}

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())
}

@fanminshi
Copy link
Copy Markdown
Contributor Author

cc/ @hasbro17 @hongchaodeng

Comment thread pkg/generator/gen_main.go Outdated
sdkImport = "github.com/coreos/operator-sdk/pkg/sdk"
main = "main"

tmpl = `package main
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Put this into a separate file, e.g. main_tmpl.go?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sounds good!

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed!

Comment thread pkg/generator/gen_main.go Outdated
}

func renderMain(w io.Writer, repo, version, projectName, service, servicePlural string) error {
t := template.New("cmd/main.go tmpl")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just "cmd/main.go"?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@fanminshi
Copy link
Copy Markdown
Contributor Author

all fixed cc/ @hongchaodeng @hasbro17

@fanminshi fanminshi mentioned this pull request Feb 21, 2018
21 tasks
@hongchaodeng
Copy link
Copy Markdown
Contributor

LGTM

@fanminshi fanminshi merged commit 256ea91 into operator-framework:master Feb 21, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants