未验证 提交 585eb1c0 编写于 作者: JenkinsInChina's avatar JenkinsInChina 提交者: GitHub

Add a default function to cwp config file (#415)

* Add a default function to cwp config file

* Fix the template not working with default function

* Fix issues found by hound

* Remove unused files
上级 0c08f8e2
......@@ -4,15 +4,15 @@ import (
"encoding/xml"
"fmt"
"github.com/jenkins-zh/jenkins-cli/app/cmd/common"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/mitchellh/go-homedir"
"github.com/spf13/cobra"
"go.uber.org/zap"
"html/template"
"io/ioutil"
"os"
"path"
"github.com/jenkins-zh/jenkins-cli/app/i18n"
"github.com/jenkins-zh/jenkins-cli/util"
"github.com/spf13/cobra"
)
func init() {
......@@ -123,12 +123,10 @@ func (o *CWPOptions) Run(cmd *cobra.Command, args []string) (err error) {
if o.ConfigPath != "" {
configPath := o.ConfigPath
if o.ValueSet != nil {
if configPath, err = RenderTemplate(o.ConfigPath, o.ValueSet); err != nil {
return
}
defer os.RemoveAll(configPath)
if configPath, err = RenderTemplate(o.ConfigPath, o.ValueSet); err != nil {
return
}
defer os.RemoveAll(configPath)
cwpArgs = append(cwpArgs, "-configPath", configPath)
}
......@@ -146,13 +144,25 @@ func (o *CWPOptions) Run(cmd *cobra.Command, args []string) (err error) {
}
// RenderTemplate render a go template to a temporary file
func RenderTemplate(path string, values map[string]string) (result string, err error) {
func RenderTemplate(filepath string, values map[string]string) (result string, err error) {
var t *template.Template
if t, err = template.ParseFiles(path); err == nil {
tmp := template.New(path.Base(filepath)).Funcs(template.FuncMap{
"default": func(arg interface{}, value interface{}) interface{} {
if value == nil {
return arg
}
return value
},
})
if t, err = tmp.ParseFiles(filepath); err == nil {
logger.Debug("parse template done", zap.String("path", filepath))
f, _ := ioutil.TempFile("/tmp", ".yaml")
err = t.Execute(f, values)
result = f.Name()
} else {
logger.Error("error when parsing template file", zap.Error(err))
}
return
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册