Skip to content

Commit

Permalink
Move some env helpers to lib
Browse files Browse the repository at this point in the history
We need these to be accessible from cmd/state as well.
  • Loading branch information
Ivan Mirić committed Dec 16, 2022
1 parent 9846935 commit 3db11f2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
16 changes: 0 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,22 +150,6 @@ func getFlags(defaultFlags globalFlags, env map[string]string) globalFlags {
return result
}

func parseEnvKeyValue(kv string) (string, string) {
if idx := strings.IndexRune(kv, '='); idx != -1 {
return kv[:idx], kv[idx+1:]
}
return kv, ""
}

func buildEnvMap(environ []string) map[string]string {
env := make(map[string]string, len(environ))
for _, kv := range environ {
k, v := parseEnvKeyValue(kv)
env[k] = v
}
return env
}

// This is to keep all fields needed for the main/root k6 command
type rootCommand struct {
globalState *globalState
Expand Down
2 changes: 1 addition & 1 deletion cmd/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func getRuntimeOptions(flags *pflag.FlagSet, environment map[string]string) (lib
return opts, err
}
for _, kv := range envVars {
k, v := parseEnvKeyValue(kv)
k, v := lib.ParseEnvKeyValue(kv)
// Allow only alphanumeric ASCII variable names for now
if !userEnvVarName.MatchString(k) {
return opts, fmt.Errorf("invalid environment variable name '%s'", k)
Expand Down
22 changes: 22 additions & 0 deletions lib/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package lib

import "strings"

// ParseEnvKeyValue splits an environment variable string into key and value.
func ParseEnvKeyValue(kv string) (string, string) {
if idx := strings.IndexRune(kv, '='); idx != -1 {
return kv[:idx], kv[idx+1:]
}
return kv, ""
}

// BuildEnvMap returns a map from raw environment values, such as returned from
// os.Environ().
func BuildEnvMap(environ []string) map[string]string {
env := make(map[string]string, len(environ))
for _, kv := range environ {
k, v := ParseEnvKeyValue(kv)
env[k] = v
}
return env
}

0 comments on commit 3db11f2

Please sign in to comment.