Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: dtm develop create-plugin enhancement #456

Merged
merged 2 commits into from
May 9, 2022
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
6 changes: 5 additions & 1 deletion internal/pkg/develop/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 3 additions & 3 deletions internal/pkg/develop/plugin/template/NAME.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
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 }}
var NAME_go_contentTpl = `package {{ .Name | format }}

// TODO(dtm): Add your logic here.
`
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/develop/plugin/template/create.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/"
var create_go_contentTpl = `package {{ .Name | format }}

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/develop/plugin/template/delete.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/"
var delete_go_contentTpl = `package {{ .Name | format }}

import (
"fmt"
Expand Down
7 changes: 7 additions & 0 deletions internal/pkg/develop/plugin/template/funcmaps.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package template

import "strings"

func FormatPackageName(name string) string {
return strings.ReplaceAll(name, "-", "")
}
10 changes: 5 additions & 5 deletions internal/pkg/develop/plugin/template/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/develop/plugin/template/options.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/"
var options_go_contentTpl = `package {{ .Name | format }}

// Options is the struct for configurations of the {{ .Name }} plugin.
type Options struct {
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/develop/plugin/template/read.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/"
var read_go_contentTpl = `package {{ .Name | format }}

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/develop/plugin/template/update.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/"
var update_go_contentTpl = `package {{ .Name | format }}

import (
"fmt"
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/develop/plugin/template/validate.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
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_dirTpl = "internal/pkg/plugin/{{ .Name | format }}/"
var validate_go_contentTpl = `package {{ .Name | format }}

// validate validates the options provided by the core.
func validate(options *Options) []error {
Expand Down