add execEnvs

Signed-off-by: yxxhero <aiopsclub@163.com>
This commit is contained in:
yxxhero
2022-05-14 09:59:08 +08:00
parent cc6edc18b6
commit 1258c9ed10
2 changed files with 29 additions and 1 deletions
+13 -1
View File
@@ -20,6 +20,7 @@ type Values = map[string]interface{}
func (c *Context) createFuncMap() template.FuncMap {
funcMap := template.FuncMap{
"execEnvs": c.ExecEnvs,
"exec": c.Exec,
"isFile": c.IsFile,
"readFile": c.ReadFile,
@@ -48,7 +49,8 @@ func (c *Context) createFuncMap() template.FuncMap {
return funcMap
}
func (c *Context) Exec(command string, args []interface{}, inputs ...string) (string, error) {
// TODO: in the next major version, remove this function.
func (c *Context) ExecEnvs(envs map[string]string, command string, args []interface{}, inputs ...string) (string, error) {
var input string
if len(inputs) > 0 {
input = inputs[0]
@@ -66,6 +68,12 @@ func (c *Context) Exec(command string, args []interface{}, inputs ...string) (st
cmd := exec.Command(command, strArgs...)
cmd.Dir = c.basePath
if envs != nil {
cmd.Env = os.Environ()
for k, v := range envs {
cmd.Env = append(cmd.Env, fmt.Sprintf("%s=%s", k, v))
}
}
g := errgroup.Group{}
@@ -119,6 +127,10 @@ func (c *Context) Exec(command string, args []interface{}, inputs ...string) (st
return string(bytes), nil
}
func (c *Context) Exec(command string, args []interface{}, inputs ...string) (string, error) {
return c.ExecEnvs(nil, command, args, inputs...)
}
func (c *Context) IsFile(filename string) (bool, error) {
var path string
if filepath.IsAbs(filename) {