Skip to content

Commit

Permalink
refactor: all files about templates and kubectl
Browse files Browse the repository at this point in the history
Signed-off-by: Bird <aflybird0@gmail.com>
  • Loading branch information
aFlyBird0 committed Aug 15, 2022
1 parent e325ef7 commit 3bd6e94
Show file tree
Hide file tree
Showing 34 changed files with 507 additions and 587 deletions.
5 changes: 1 addition & 4 deletions internal/pkg/configmanager/toolconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,11 +73,8 @@ func NewToolWithToolConfigBytesAndVarsConfigBytes(toolConfigBytes, varConfigByte
return nil, err
}

// handle variables format
toolConfigBytesWithDot := addDotForVariablesInConfig(string(toolConfigBytes))

// render config with variables
toolConfigBytesWithVars, err := renderConfigWithVariables(toolConfigBytesWithDot, variables)
toolConfigBytesWithVars, err := renderConfigWithVariables(string(toolConfigBytes), variables)
if err != nil {
log.Errorf("Failed to render tool config with vars: %s.", err)
return nil, err
Expand Down
28 changes: 7 additions & 21 deletions internal/pkg/configmanager/variables.go
Original file line number Diff line number Diff line change
@@ -1,33 +1,19 @@
package configmanager

import (
"bytes"
"html/template"
"regexp"
"github.com/devstream-io/devstream/pkg/util/template"
)

// this is because our variables' syntax is [[ varName ]]
// while Go's template is [[ .varName ]]
func addDotForVariablesInConfig(s string) string {
// regex := `\[\[\s*(.*)\s*\]\]`
// r := regexp.MustCompile(regex)
// return r.ReplaceAllString(s, "[[ .$1 ]]")
regex := `\[\[\s*`
r := regexp.MustCompile(regex)
return r.ReplaceAllString(s, "[[ .")
}

func renderConfigWithVariables(fileContent string, variables map[string]interface{}) ([]byte, error) {
tpl, err := template.New("configfile").Delims("[[", "]]").Parse(fileContent)
if err != nil {
return nil, err
}
str, err := template.New().
FromContent(fileContent).
AddProcessor(template.AddDotForVariablesInConfigProcessor()).
DefaultRender(fileContent, variables).
Render()

var results bytes.Buffer
err = tpl.Execute(&results, variables)
if err != nil {
return nil, err
}

return results.Bytes(), err
return []byte(str), nil
}
22 changes: 1 addition & 21 deletions internal/pkg/configmanager/variables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,12 @@ import (
"github.com/stretchr/testify/assert"
)

func TestAddDotForVariablesInConfigNormal(t *testing.T) {
res := addDotForVariablesInConfig("[[varNameA]]")
assert.Equal(t, "[[ .varNameA]]", res, "Adding dot for variable names passed.")
}

func TestAddDotForVariablesInConfigWithSpaces(t *testing.T) {
res := addDotForVariablesInConfig("[[ varNameA]]")
assert.Equal(t, "[[ .varNameA]]", res, "Adding dot for variable names passed.")
}

func TestAddDotForVariablesInConfigWithTrailingSpaces(t *testing.T) {
res := addDotForVariablesInConfig("[[ varNameA ]]")
assert.Equal(t, "[[ .varNameA ]]", res, "Adding dot for variable names passed.")
}

func TestAddDotForVariablesInConfigMultipleVars(t *testing.T) {
res := addDotForVariablesInConfig("[[ varNameA ]]/[[ varNameB ]]/[[ varNameC ]]")
assert.Equal(t, "[[ .varNameA ]]/[[ .varNameB ]]/[[ .varNameC ]]", res, "Adding dot for variable names passed.")
}

func TestRenderConfigWithVariables(t *testing.T) {
variables := map[string]interface{}{
"varNameA": "A",
"varNameB": "B",
}
result, err := renderConfigWithVariables("[[ .varNameA ]]/[[ .varNameB]]", variables)
result, err := renderConfigWithVariables("[[ varNameA ]]/[[ varNameB]]", variables)
assert.Equal(t, err, nil)
assert.Equal(t, string(result), "A/B")
}
14 changes: 3 additions & 11 deletions internal/pkg/develop/plugin/plugin.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package plugin

import (
"bytes"
"fmt"
"os"
"path"
Expand All @@ -10,6 +9,7 @@ import (

pluginTpl "github.com/devstream-io/devstream/internal/pkg/develop/plugin/template"
"github.com/devstream-io/devstream/pkg/util/log"
templateUtil "github.com/devstream-io/devstream/pkg/util/template"
)

type Plugin struct {
Expand Down Expand Up @@ -79,23 +79,15 @@ func (p *Plugin) renderTplString(tplStr string) (string, error) {
"dirFormat": pluginTpl.FormatPackageDirName,
}

t, err := template.New("default").Delims("[[", "]]").Funcs(funcMap).Parse(tplStr)
if err != nil {
log.Debugf("Template parse failed: %s.", err)
log.Debugf("Template content: %s.", tplStr)
return "", err
}

var buf bytes.Buffer
err = t.Execute(&buf, *p)
res, err := templateUtil.New().FromContent(tplStr).DefaultRender("default", *p, funcMap).Render()
if err != nil {
log.Debugf("Template execute failed: %s.", err)
log.Debugf("Template content: %s.", tplStr)
log.Debugf("Data object: %v.", *p)
return "", err
}

return buf.String(), nil
return res, nil
}

// PersistFiles gets the []pluginTpl.File, for each File:
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/plugin/argocdapp/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package argocdapp
import (
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl"
"github.com/devstream-io/devstream/pkg/util/file"
kubectlUtil "github.com/devstream-io/devstream/pkg/util/kubectl"
"github.com/devstream-io/devstream/pkg/util/log"
)
Expand All @@ -16,9 +15,7 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
validate,
},
ExecuteOperations: plugininstaller.ExecuteOperations{
kubectl.ProcessByContent(
kubectlUtil.Create, file.NewTemplate().FromContent(templateFileLoc),
),
kubectl.ProcessByContent(kubectlUtil.Create, templateFileLoc),
},
GetStateOperation: getStaticState,
}
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/plugin/argocdapp/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package argocdapp
import (
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl"
"github.com/devstream-io/devstream/pkg/util/file"
kubectlUtil "github.com/devstream-io/devstream/pkg/util/kubectl"
)

Expand All @@ -14,9 +13,7 @@ func Delete(options map[string]interface{}) (bool, error) {
validate,
},
ExecuteOperations: plugininstaller.ExecuteOperations{
kubectl.ProcessByContent(
kubectlUtil.Delete, file.NewTemplate().FromContent(templateFileLoc),
),
kubectl.ProcessByContent(kubectlUtil.Delete, templateFileLoc),
},
}

Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/plugin/devlake/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package devlake
import (
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl"
"github.com/devstream-io/devstream/pkg/util/file"
kubectlUtil "github.com/devstream-io/devstream/pkg/util/kubectl"
"github.com/devstream-io/devstream/pkg/util/log"
)
Expand All @@ -12,9 +11,7 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
// Initialize Operator with Operations
operator := &plugininstaller.Operator{
ExecuteOperations: plugininstaller.ExecuteOperations{
kubectl.ProcessByContent(
kubectlUtil.Create, file.NewTemplate().FromRemote(devLakeInstallYAMLDownloadURL),
),
kubectl.ProcessByURL(kubectlUtil.Create, devLakeInstallYAMLDownloadURL),
},
GetStateOperation: getStaticState,
}
Expand Down
5 changes: 1 addition & 4 deletions internal/pkg/plugin/devlake/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ package devlake
import (
"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/internal/pkg/plugininstaller/kubectl"
"github.com/devstream-io/devstream/pkg/util/file"
kubectlUtil "github.com/devstream-io/devstream/pkg/util/kubectl"
)

func Delete(options map[string]interface{}) (bool, error) {
// Initialize Operator with Operations
operator := &plugininstaller.Operator{
ExecuteOperations: plugininstaller.ExecuteOperations{
kubectl.ProcessByContent(
kubectlUtil.Delete, file.NewTemplate().FromRemote(devLakeInstallYAMLDownloadURL),
),
kubectl.ProcessByURL(kubectlUtil.Delete, devLakeInstallYAMLDownloadURL),
},
}

Expand Down
15 changes: 6 additions & 9 deletions internal/pkg/plugin/gitlabci/generic/create.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package generic

import (
"bytes"
"fmt"
"text/template"

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/template"
)

func Create(options map[string]interface{}) (map[string]interface{}, error) {
Expand All @@ -31,12 +30,10 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
}

// render template
var ciFileContentBytes bytes.Buffer
tpl, err := template.New("ci").Option("missingkey=error").Parse(ciTemplateString)
if err != nil {
return nil, fmt.Errorf("parse template error: %s", err)
}
err = tpl.Execute(&ciFileContentBytes, opts.TemplateVariables)
ciFileContent, err := template.New().
FromContent(ciTemplateString).
DefaultRender("ci", opts.TemplateVariables).Render()

if err != nil {
return nil, fmt.Errorf("execute template error: %s", err)
}
Expand All @@ -50,7 +47,7 @@ func Create(options map[string]interface{}) (map[string]interface{}, error) {
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
ciFileName: ciFileContentBytes.Bytes(),
ciFileName: []byte(ciFileContent),
},
})
if err != nil {
Expand Down
15 changes: 6 additions & 9 deletions internal/pkg/plugin/gitlabci/generic/update.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package generic

import (
"bytes"
"fmt"
"text/template"

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/pkg/util/git"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/template"
)

func Update(options map[string]interface{}) (map[string]interface{}, error) {
Expand All @@ -31,12 +30,10 @@ func Update(options map[string]interface{}) (map[string]interface{}, error) {
}

// render template
var ciFileContentBytes bytes.Buffer
tpl, err := template.New("ci").Option("missingkey=error").Parse(ciTemplateString)
if err != nil {
return nil, fmt.Errorf("parse template error: %s", err)
}
err = tpl.Execute(&ciFileContentBytes, opts.TemplateVariables)
ciFileContent, err := template.New().
FromContent(ciTemplateString).
DefaultRender("ci", opts.TemplateVariables).Render()

if err != nil {
return nil, fmt.Errorf("execute template error: %s", err)
}
Expand All @@ -50,7 +47,7 @@ func Update(options map[string]interface{}) (map[string]interface{}, error) {
CommitMsg: commitMessage,
CommitBranch: opts.Branch,
GitFileMap: git.GitFileContentMap{
ciFileName: ciFileContentBytes.Bytes(),
ciFileName: []byte(ciFileContent),
},
}
// the only difference between create and update
Expand Down
14 changes: 3 additions & 11 deletions internal/pkg/plugin/gitlabci/java/template.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
package java

import (
"bytes"
_ "embed"
"html/template"

"github.com/devstream-io/devstream/pkg/util/template"
)

//go:embed gitlabci-template.yml
var gitlabCITemplate string

// Render gitlab-ci.yml template with Options
func renderTmpl(Opts *Options) (string, error) {
t := template.Must(template.New("gitlabci-java").Delims("[[", "]]").Option("missingkey=error").Parse(gitlabCITemplate))

var buf bytes.Buffer
err := t.Execute(&buf, Opts)
if err != nil {
return "", err
}

return buf.String(), nil
return template.New().FromContent(gitlabCITemplate).DefaultRender("gitlabci-java", Opts).Render()
}
7 changes: 3 additions & 4 deletions internal/pkg/plugin/harbordocker/harbordocker.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@ package harbordocker
import (
_ "embed"
"os"
"text/template"

"github.com/mitchellh/mapstructure"

"github.com/devstream-io/devstream/internal/pkg/plugininstaller"
"github.com/devstream-io/devstream/pkg/util/docker/dockersh"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/template"
)

const (
Expand Down Expand Up @@ -57,8 +57,7 @@ func renderConfig(options plugininstaller.RawOptions) (plugininstaller.RawOption
return nil, err
}

// TODO(daniel-hutao): use template wrapper here
tmpl, err := template.New("compose").Delims("[[", "]]").Parse(HarborConfigTemplate)
content, err := template.New().FromContent(HarborConfigTemplate).DefaultRender("compose", opts).Render()
if err != nil {
return nil, err
}
Expand All @@ -74,7 +73,7 @@ func renderConfig(options plugininstaller.RawOptions) (plugininstaller.RawOption
}
}()

if err := tmpl.Execute(configFile, opts); err != nil {
if _, err := configFile.Write([]byte(content)); err != nil {
return nil, err
}

Expand Down
17 changes: 2 additions & 15 deletions internal/pkg/plugin/jenkinsgithub/jcasc.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package jenkinsgithub

import (
"bytes"
"context"
_ "embed"
"fmt"
"text/template"
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
corev1 "k8s.io/client-go/applyconfigurations/core/v1"

"github.com/devstream-io/devstream/pkg/util/k8s"
"github.com/devstream-io/devstream/pkg/util/log"
"github.com/devstream-io/devstream/pkg/util/template"
)

const githubIntegName = "github-integ"
Expand Down Expand Up @@ -84,17 +83,5 @@ func applyJCasC(namespace, chartReleaseName, configName, fileContent string) err
}

func renderGitHubInteg(opts *GitHubIntegOptions) (string, error) {
tpl := template.New(githubIntegName).Delims("[[", "]]")
tpl, err := tpl.Parse(githubIntegTemplate)
if err != nil {
return "", err
}

var buf bytes.Buffer
err = tpl.Execute(&buf, opts)
if err != nil {
return "", err
}

return buf.String(), nil
return template.New().FromContent(githubIntegTemplate).DefaultRender(githubIntegName, opts).Render()
}
2 changes: 1 addition & 1 deletion internal/pkg/plugin/jenkinsgithub/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ func createJob(client *jenkins.Jenkins, jobName, jobTemplate string, opts *Optio
// renderJobXml renders the job xml content from the job template and the job options.
// pr job && main job
func renderJobXml(jobTemplate string, opts *JobOptions) (string, error) {
return template.Render(githubIntegName, jobTemplate, opts)
return template.New().FromContent(jobTemplate).DefaultRender(githubIntegTemplate, opts).Render()
}
Loading

0 comments on commit 3bd6e94

Please sign in to comment.