Skip to content

Commit

Permalink
refactor(cmd/root): simplify parseEnvs (#2162)
Browse files Browse the repository at this point in the history
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 <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Jan 24, 2024
1 parent 6a8c42a commit 424fd5e
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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())
Expand Down

0 comments on commit 424fd5e

Please sign in to comment.