From bb97781cc35378b9449acfe8542ad41cfcf7ac37 Mon Sep 17 00:00:00 2001 From: weiran Date: Mon, 9 May 2022 18:58:25 +0800 Subject: [PATCH 1/2] feat: new dtm develop create-plugin --- internal/pkg/develop/plugin/plugin.go | 6 +++++- internal/pkg/develop/plugin/template/NAME.go | 2 +- internal/pkg/develop/plugin/template/create.go | 2 +- internal/pkg/develop/plugin/template/delete.go | 2 +- internal/pkg/develop/plugin/template/funcmaps.go | 7 +++++++ internal/pkg/develop/plugin/template/options.go | 2 +- internal/pkg/develop/plugin/template/read.go | 2 +- internal/pkg/develop/plugin/template/update.go | 2 +- internal/pkg/develop/plugin/template/validate.go | 2 +- 9 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 internal/pkg/develop/plugin/template/funcmaps.go diff --git a/internal/pkg/develop/plugin/plugin.go b/internal/pkg/develop/plugin/plugin.go index 1bb7c0d44..3479eb9b1 100644 --- a/internal/pkg/develop/plugin/plugin.go +++ b/internal/pkg/develop/plugin/plugin.go @@ -74,7 +74,11 @@ func (p *Plugin) renderTplString(tplStr string) (string, error) { return "", nil } - t, err := template.New("default").Parse(tplStr) + var funcMap = template.FuncMap{ + "format": pluginTpl.FormatPackageName, + } + + t, err := template.New("default").Funcs(funcMap).Parse(tplStr) if err != nil { log.Debugf("Template parse failed: %s.", err) log.Debugf("Template content: %s.", tplStr) diff --git a/internal/pkg/develop/plugin/template/NAME.go b/internal/pkg/develop/plugin/template/NAME.go index b42d7a9c2..dc98d8237 100644 --- a/internal/pkg/develop/plugin/template/NAME.go +++ b/internal/pkg/develop/plugin/template/NAME.go @@ -3,7 +3,7 @@ package template var NAME_go_nameTpl = "{{ .Name }}.go" var NAME_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" var NAME_go_mustExistFlag = true -var NAME_go_contentTpl = `package {{ .Name }} +var NAME_go_contentTpl = `package {{ .Name | format }} // TODO(dtm): Add your logic here. ` diff --git a/internal/pkg/develop/plugin/template/create.go b/internal/pkg/develop/plugin/template/create.go index 382bb958a..7ec0571fa 100644 --- a/internal/pkg/develop/plugin/template/create.go +++ b/internal/pkg/develop/plugin/template/create.go @@ -2,7 +2,7 @@ package template var create_go_nameTpl = "create.go" var create_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" -var create_go_contentTpl = `package {{ .Name }} +var create_go_contentTpl = `package {{ .Name | format }} import ( "fmt" diff --git a/internal/pkg/develop/plugin/template/delete.go b/internal/pkg/develop/plugin/template/delete.go index c3e0973b0..97a1637cc 100644 --- a/internal/pkg/develop/plugin/template/delete.go +++ b/internal/pkg/develop/plugin/template/delete.go @@ -2,7 +2,7 @@ package template var delete_go_nameTpl = "delete.go" var delete_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" -var delete_go_contentTpl = `package {{ .Name }} +var delete_go_contentTpl = `package {{ .Name | format }} import ( "fmt" diff --git a/internal/pkg/develop/plugin/template/funcmaps.go b/internal/pkg/develop/plugin/template/funcmaps.go new file mode 100644 index 000000000..317da7c5c --- /dev/null +++ b/internal/pkg/develop/plugin/template/funcmaps.go @@ -0,0 +1,7 @@ +package template + +import "strings" + +func FormatPackageName(name string) string { + return strings.ReplaceAll(name, "-", "") +} diff --git a/internal/pkg/develop/plugin/template/options.go b/internal/pkg/develop/plugin/template/options.go index 808ec567c..b88d591da 100644 --- a/internal/pkg/develop/plugin/template/options.go +++ b/internal/pkg/develop/plugin/template/options.go @@ -2,7 +2,7 @@ package template var options_go_nameTpl = "options.go" var options_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" -var options_go_contentTpl = `package {{ .Name }} +var options_go_contentTpl = `package {{ .Name | format }} // Options is the struct for configurations of the {{ .Name }} plugin. type Options struct { diff --git a/internal/pkg/develop/plugin/template/read.go b/internal/pkg/develop/plugin/template/read.go index 347a2faa7..2db7b6ee1 100644 --- a/internal/pkg/develop/plugin/template/read.go +++ b/internal/pkg/develop/plugin/template/read.go @@ -2,7 +2,7 @@ package template var read_go_nameTpl = "read.go" var read_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" -var read_go_contentTpl = `package {{ .Name }} +var read_go_contentTpl = `package {{ .Name | format }} import ( "fmt" diff --git a/internal/pkg/develop/plugin/template/update.go b/internal/pkg/develop/plugin/template/update.go index b7652d7c3..8dfa2503d 100644 --- a/internal/pkg/develop/plugin/template/update.go +++ b/internal/pkg/develop/plugin/template/update.go @@ -2,7 +2,7 @@ package template var update_go_nameTpl = "update.go" var update_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" -var update_go_contentTpl = `package {{ .Name }} +var update_go_contentTpl = `package {{ .Name | format }} import ( "fmt" diff --git a/internal/pkg/develop/plugin/template/validate.go b/internal/pkg/develop/plugin/template/validate.go index 3db8e8277..c1f5aaf1f 100644 --- a/internal/pkg/develop/plugin/template/validate.go +++ b/internal/pkg/develop/plugin/template/validate.go @@ -2,7 +2,7 @@ package template var validate_go_nameTpl = "validate.go" var validate_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" -var validate_go_contentTpl = `package {{ .Name }} +var validate_go_contentTpl = `package {{ .Name | format }} // validate validates the options provided by the core. func validate(options *Options) []error { From 2068abae769487452dce345ecc208520409809ee Mon Sep 17 00:00:00 2001 From: weiran Date: Mon, 9 May 2022 22:36:05 +0800 Subject: [PATCH 2/2] fix: dtm develop create-plugin enhancement --- internal/pkg/develop/plugin/template/NAME.go | 4 ++-- internal/pkg/develop/plugin/template/create.go | 2 +- internal/pkg/develop/plugin/template/delete.go | 2 +- internal/pkg/develop/plugin/template/main.go | 10 +++++----- internal/pkg/develop/plugin/template/options.go | 2 +- internal/pkg/develop/plugin/template/read.go | 2 +- internal/pkg/develop/plugin/template/update.go | 2 +- internal/pkg/develop/plugin/template/validate.go | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/internal/pkg/develop/plugin/template/NAME.go b/internal/pkg/develop/plugin/template/NAME.go index dc98d8237..f36cda3e0 100644 --- a/internal/pkg/develop/plugin/template/NAME.go +++ b/internal/pkg/develop/plugin/template/NAME.go @@ -1,7 +1,7 @@ package template -var NAME_go_nameTpl = "{{ .Name }}.go" -var NAME_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" +var NAME_go_nameTpl = "{{ .Name | format }}.go" +var NAME_go_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/" var NAME_go_mustExistFlag = true var NAME_go_contentTpl = `package {{ .Name | format }} diff --git a/internal/pkg/develop/plugin/template/create.go b/internal/pkg/develop/plugin/template/create.go index 7ec0571fa..74ea38843 100644 --- a/internal/pkg/develop/plugin/template/create.go +++ b/internal/pkg/develop/plugin/template/create.go @@ -1,7 +1,7 @@ package template var create_go_nameTpl = "create.go" -var create_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" +var create_go_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/" var create_go_contentTpl = `package {{ .Name | format }} import ( diff --git a/internal/pkg/develop/plugin/template/delete.go b/internal/pkg/develop/plugin/template/delete.go index 97a1637cc..e0ac9bbff 100644 --- a/internal/pkg/develop/plugin/template/delete.go +++ b/internal/pkg/develop/plugin/template/delete.go @@ -1,7 +1,7 @@ package template var delete_go_nameTpl = "delete.go" -var delete_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" +var delete_go_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/" var delete_go_contentTpl = `package {{ .Name | format }} import ( diff --git a/internal/pkg/develop/plugin/template/main.go b/internal/pkg/develop/plugin/template/main.go index f423d07c1..e4b0f8f49 100644 --- a/internal/pkg/develop/plugin/template/main.go +++ b/internal/pkg/develop/plugin/template/main.go @@ -5,7 +5,7 @@ var main_go_dirTpl = "cmd/plugin/{{ .Name }}/" var main_go_contentTpl = `package main import ( - "github.com/devstream-io/devstream/internal/pkg/plugin/{{ .Name }}" + "github.com/devstream-io/devstream/internal/pkg/plugin/{{ .Name | format }}" "github.com/devstream-io/devstream/pkg/util/log" ) @@ -17,22 +17,22 @@ type Plugin string // Create implements the create of {{ .Name }}. func (p Plugin) Create(options map[string]interface{}) (map[string]interface{}, error) { - return {{ .Name }}.Create(options) + return {{ .Name | format }}.Create(options) } // Update implements the update of {{ .Name }}. func (p Plugin) Update(options map[string]interface{}) (map[string]interface{}, error) { - return {{ .Name }}.Update(options) + return {{ .Name | format }}.Update(options) } // Delete implements the delete of {{ .Name }}. func (p Plugin) Delete(options map[string]interface{}) (bool, error) { - return {{ .Name }}.Delete(options) + return {{ .Name | format }}.Delete(options) } // Read implements the read of {{ .Name }}. func (p Plugin) Read(options map[string]interface{}) (map[string]interface{}, error) { - return {{ .Name }}.Read(options) + return {{ .Name | format }}.Read(options) } // DevStreamPlugin is the exported variable used by the DevStream core. diff --git a/internal/pkg/develop/plugin/template/options.go b/internal/pkg/develop/plugin/template/options.go index b88d591da..90d9a131f 100644 --- a/internal/pkg/develop/plugin/template/options.go +++ b/internal/pkg/develop/plugin/template/options.go @@ -1,7 +1,7 @@ package template var options_go_nameTpl = "options.go" -var options_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" +var options_go_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/" var options_go_contentTpl = `package {{ .Name | format }} // Options is the struct for configurations of the {{ .Name }} plugin. diff --git a/internal/pkg/develop/plugin/template/read.go b/internal/pkg/develop/plugin/template/read.go index 2db7b6ee1..8b36db5e0 100644 --- a/internal/pkg/develop/plugin/template/read.go +++ b/internal/pkg/develop/plugin/template/read.go @@ -1,7 +1,7 @@ package template var read_go_nameTpl = "read.go" -var read_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" +var read_go_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/" var read_go_contentTpl = `package {{ .Name | format }} import ( diff --git a/internal/pkg/develop/plugin/template/update.go b/internal/pkg/develop/plugin/template/update.go index 8dfa2503d..cf2012917 100644 --- a/internal/pkg/develop/plugin/template/update.go +++ b/internal/pkg/develop/plugin/template/update.go @@ -1,7 +1,7 @@ package template var update_go_nameTpl = "update.go" -var update_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" +var update_go_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/" var update_go_contentTpl = `package {{ .Name | format }} import ( diff --git a/internal/pkg/develop/plugin/template/validate.go b/internal/pkg/develop/plugin/template/validate.go index c1f5aaf1f..b363eb15e 100644 --- a/internal/pkg/develop/plugin/template/validate.go +++ b/internal/pkg/develop/plugin/template/validate.go @@ -1,7 +1,7 @@ package template var validate_go_nameTpl = "validate.go" -var validate_go_dirTpl = "internal/pkg/plugin/{{ .Name }}/" +var validate_go_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/" var validate_go_contentTpl = `package {{ .Name | format }} // validate validates the options provided by the core.