From 424fd5e02b4be9fce8aae518420b0499c08cb31b Mon Sep 17 00:00:00 2001 From: Eng Zer Jun Date: Wed, 24 Jan 2024 11:06:31 +0800 Subject: [PATCH] refactor(cmd/root): simplify `parseEnvs` (#2162) Prior to this commit, `parseEnvs` accept two parameters: 1. env []string 2. envs map[string]string `parseEnvs` then do a `nil` check for `env`. However, we never pass a `nil` `env` to `parseEnvs` in `newRunCommand`. This commit simplify the `parseEnvs` function to accept just one `env []string` parameter and return the result as `map[string]string` instead. Signed-off-by: Eng Zer Jun --- cmd/root.go | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/cmd/root.go b/cmd/root.go index 319fdd5..e506699 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -297,19 +297,17 @@ func cleanup(inputs *Input) func(*cobra.Command, []string) { } } -func parseEnvs(env []string, envs map[string]string) bool { - if env != nil { - for _, envVar := range env { - e := strings.SplitN(envVar, `=`, 2) - if len(e) == 2 { - envs[e[0]] = e[1] - } else { - envs[e[0]] = "" - } +func parseEnvs(env []string) map[string]string { + envs := make(map[string]string, len(env)) + for _, envVar := range env { + e := strings.SplitN(envVar, `=`, 2) + if len(e) == 2 { + envs[e[0]] = e[1] + } else { + envs[e[0]] = "" } - return true } - return false + return envs } func readYamlFile(file string) (map[string]string, error) { @@ -415,13 +413,11 @@ func newRunCommand(ctx context.Context, input *Input) func(*cobra.Command, []str } log.Debugf("Loading environment from %s", input.Envfile()) - envs := make(map[string]string) - _ = parseEnvs(input.envs, envs) + envs := parseEnvs(input.envs) _ = readEnvs(input.Envfile(), envs) log.Debugf("Loading action inputs from %s", input.Inputfile()) - inputs := make(map[string]string) - _ = parseEnvs(input.inputs, inputs) + inputs := parseEnvs(input.inputs) _ = readEnvs(input.Inputfile(), inputs) log.Debugf("Loading secrets from %s", input.Secretfile())