Merge pull request #97 from yxxhero/rename_execEnvs_to_-envExec

rename execEnvs to envExec
This commit is contained in:
Quan TRAN 2022-05-20 09:52:26 +02:00 committed by GitHub
commit eddb9eec45
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 32 additions and 11 deletions

View File

@ -1124,13 +1124,13 @@ mysetting: |
The possibility is endless. Try importing values from your golang app, bash script, jsonnet, or anything! The possibility is endless. Try importing values from your golang app, bash script, jsonnet, or anything!
Then `execEnvs` same as `exec`, but it can receive a dict as the envs. Then `envExec` same as `exec`, but it can receive a dict as the envs.
A usual usage of `execEnvs` would look like this: A usual usage of `envExec` would look like this:
```yaml ```yaml
mysetting: | mysetting: |
{{ execEnvs (dict "envkey" "envValue") "./mycmd" (list "arg1" "arg2" "--flag1") | indent 2 }} {{ envExec (dict "envkey" "envValue") "./mycmd" (list "arg1" "arg2" "--flag1") | indent 2 }}
``` ```

View File

@ -20,7 +20,7 @@ type Values = map[string]interface{}
func (c *Context) createFuncMap() template.FuncMap { func (c *Context) createFuncMap() template.FuncMap {
funcMap := template.FuncMap{ funcMap := template.FuncMap{
"execEnvs": c.ExecEnvs, "envExec": c.EnvExec,
"exec": c.Exec, "exec": c.Exec,
"isFile": c.IsFile, "isFile": c.IsFile,
"readFile": c.ReadFile, "readFile": c.ReadFile,
@ -41,6 +41,9 @@ func (c *Context) createFuncMap() template.FuncMap {
funcMap["exec"] = func(string, []interface{}, ...string) (string, error) { funcMap["exec"] = func(string, []interface{}, ...string) (string, error) {
return "", nil return "", nil
} }
funcMap["envExec"] = func(map[string]interface{}, string, []interface{}, ...string) (string, error) {
return "", nil
}
funcMap["readFile"] = func(string) (string, error) { funcMap["readFile"] = func(string) (string, error) {
return "", nil return "", nil
} }
@ -50,7 +53,7 @@ func (c *Context) createFuncMap() template.FuncMap {
} }
// TODO: in the next major version, remove this function. // 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) { func (c *Context) EnvExec(envs map[string]interface{}, command string, args []interface{}, inputs ...string) (string, error) {
var input string var input string
if len(inputs) > 0 { if len(inputs) > 0 {
input = inputs[0] input = inputs[0]
@ -66,6 +69,18 @@ func (c *Context) ExecEnvs(envs map[string]string, command string, args []interf
} }
} }
envsLen := len(envs)
strEnvs := make(map[string]string, envsLen)
for k, v := range envs {
switch v.(type) {
case string:
strEnvs[k] = fmt.Sprintf("%v", v)
default:
return "", fmt.Errorf("unexpected type of env \"%s\" in envs %v at index %s", reflect.TypeOf(v), envs, k)
}
}
cmd := exec.Command(command, strArgs...) cmd := exec.Command(command, strArgs...)
cmd.Dir = c.basePath cmd.Dir = c.basePath
if envs != nil { if envs != nil {
@ -128,7 +143,7 @@ func (c *Context) ExecEnvs(envs map[string]string, command string, args []interf
} }
func (c *Context) Exec(command string, args []interface{}, inputs ...string) (string, error) { func (c *Context) Exec(command string, args []interface{}, inputs ...string) (string, error) {
return c.ExecEnvs(nil, command, args, inputs...) return c.EnvExec(nil, command, args, inputs...)
} }
func (c *Context) IsFile(filename string) (bool, error) { func (c *Context) IsFile(filename string) (bool, error) {

View File

@ -268,9 +268,9 @@ func TestExec(t *testing.T) {
require.Error(t, err, "Expected error to be returned when executing command with non-zero exit code") require.Error(t, err, "Expected error to be returned when executing command with non-zero exit code")
} }
// TestExecEnvs tests that ExecEnvs returns the expected output. // TestEnvExec tests that EnvExec returns the expected output.
// TODO: in the next major version, this test should be removed. // TODO: in the next major version, this test should be removed.
func TestExecEnvs(t *testing.T) { func TestEnvExec(t *testing.T) {
ctx := &Context{basePath: "."} ctx := &Context{basePath: "."}
expected := "foo" expected := "foo"
@ -278,14 +278,20 @@ func TestExecEnvs(t *testing.T) {
testKey := "testkey" testKey := "testkey"
// test that the command is executed with environment variables // test that the command is executed with environment variables
output, err := ctx.ExecEnvs(map[string]string{testKey: "foo"}, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "") output, err := ctx.EnvExec(map[string]interface{}{testKey: "foo"}, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "")
require.Nilf(t, err, "Expected no error to be returned when executing command with environment variables") require.Nilf(t, err, "Expected no error to be returned when executing command with environment variables")
require.Equalf(t, expected, output, "Expected %s to be returned when executing command with environment variables", expected) require.Equalf(t, expected, output, "Expected %s to be returned when executing command with environment variables", expected)
// test that the command is executed with invalid environment variables
output, err = ctx.EnvExec(map[string]interface{}{testKey: 123}, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "")
require.Errorf(t, err, "Expected error to be returned when executing command with invalid environment variables")
require.Emptyf(t, output, "Expected empty string to be returned when executing command with invalid environment variables")
// test that the command is executed with no environment variables // test that the command is executed with no environment variables
output, err = ctx.ExecEnvs(nil, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "") output, err = ctx.EnvExec(nil, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "")
require.Nilf(t, err, "Expected no error to be returned when executing command with no environment variables") require.Nilf(t, err, "Expected no error to be returned when executing command with no environment variables")
require.Emptyf(t, output, "Expected empty string to be returned when executing command with no environment variables") require.Emptyf(t, output, "Expected empty string to be returned when executing command with no environment variables")
@ -293,7 +299,7 @@ func TestExecEnvs(t *testing.T) {
// test that the command is executed with os environment variables // test that the command is executed with os environment variables
os.Setenv(testKey, "foo") os.Setenv(testKey, "foo")
defer os.Unsetenv(testKey) defer os.Unsetenv(testKey)
output, err = ctx.ExecEnvs(nil, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "") output, err = ctx.EnvExec(nil, "bash", []interface{}{"-c", fmt.Sprintf("echo -n $%s", testKey)}, "")
require.Nilf(t, err, "Expected no error to be returned when executing command with environment variables") require.Nilf(t, err, "Expected no error to be returned when executing command with environment variables")