opts: simplify ValidateEnv to use os.LookupEnv

os.LookupEnv() was not available yet at the time this was
implemented, but now provides the functionality we need,
so replacing our custom handling.

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2020-08-14 16:38:09 +02:00
parent c5bf79c37e
commit 83eda5313b
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
2 changed files with 113 additions and 52 deletions

View File

@ -1,46 +1,30 @@
package opts package opts
import ( import (
"fmt"
"os" "os"
"runtime"
"strings" "strings"
"github.com/pkg/errors"
) )
// ValidateEnv validates an environment variable and returns it. // ValidateEnv validates an environment variable and returns it.
// If no value is specified, it returns the current value using os.Getenv. // If no value is specified, it obtains its value from the current environment
// //
// As on ParseEnvFile and related to #16585, environment variable names // As on ParseEnvFile and related to #16585, environment variable names
// are not validate what so ever, it's up to application inside docker // are not validated, and it's up to the application inside the container
// to validate them or not. // to validate them or not.
// //
// The only validation here is to check if name is empty, per #25099 // The only validation here is to check if name is empty, per #25099
func ValidateEnv(val string) (string, error) { func ValidateEnv(val string) (string, error) {
arr := strings.Split(val, "=") arr := strings.SplitN(val, "=", 2)
if arr[0] == "" { if arr[0] == "" {
return "", fmt.Errorf("invalid environment variable: %s", val) return "", errors.New("invalid environment variable: " + val)
} }
if len(arr) > 1 { if len(arr) > 1 {
return val, nil return val, nil
} }
if !doesEnvExist(val) { if envVal, ok := os.LookupEnv(arr[0]); ok {
return arr[0] + "=" + envVal, nil
}
return val, nil return val, nil
} }
return fmt.Sprintf("%s=%s", val, os.Getenv(val)), nil
}
func doesEnvExist(name string) bool {
for _, entry := range os.Environ() {
parts := strings.SplitN(entry, "=", 2)
if runtime.GOOS == "windows" {
// Environment variable are case-insensitive on Windows. PaTh, path and PATH are equivalent.
if strings.EqualFold(parts[0], name) {
return true
}
}
if parts[0] == name {
return true
}
}
return false
}

View File

@ -5,38 +5,115 @@ import (
"os" "os"
"runtime" "runtime"
"testing" "testing"
"gotest.tools/v3/assert"
) )
func TestValidateEnv(t *testing.T) { func TestValidateEnv(t *testing.T) {
valids := map[string]string{ type testCase struct {
"a": "a", value string
"something": "something", expected string
"_=a": "_=a", err error
"env1=value1": "env1=value1",
"_env1=value1": "_env1=value1",
"env2=value2=value3": "env2=value2=value3",
"env3=abc!qwe": "env3=abc!qwe",
"env_4=value 4": "env_4=value 4",
"PATH": fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
"PATH=something": "PATH=something",
"asd!qwe": "asd!qwe",
"1asd": "1asd",
"123": "123",
"some space": "some space",
" some space before": " some space before",
"some space after ": "some space after ",
} }
// Environment variables are case in-sensitive on Windows tests := []testCase{
{
value: "a",
expected: "a",
},
{
value: "something",
expected: "something",
},
{
value: "_=a",
expected: "_=a",
},
{
value: "env1=value1",
expected: "env1=value1",
},
{
value: "_env1=value1",
expected: "_env1=value1",
},
{
value: "env2=value2=value3",
expected: "env2=value2=value3",
},
{
value: "env3=abc!qwe",
expected: "env3=abc!qwe",
},
{
value: "env_4=value 4",
expected: "env_4=value 4",
},
{
value: "PATH",
expected: fmt.Sprintf("PATH=%v", os.Getenv("PATH")),
},
{
value: "=a",
err: fmt.Errorf("invalid environment variable: =a"),
},
{
value: "PATH=",
expected: "PATH=",
},
{
value: "PATH=something",
expected: "PATH=something",
},
{
value: "asd!qwe",
expected: "asd!qwe",
},
{
value: "1asd",
expected: "1asd",
},
{
value: "123",
expected: "123",
},
{
value: "some space",
expected: "some space",
},
{
value: " some space before",
expected: " some space before",
},
{
value: "some space after ",
expected: "some space after ",
},
{
value: "=",
err: fmt.Errorf("invalid environment variable: ="),
},
}
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
valids["PaTh"] = fmt.Sprintf("PaTh=%v", os.Getenv("PATH")) // Environment variables are case in-sensitive on Windows
tests = append(tests, testCase{
value: "PaTh",
expected: fmt.Sprintf("PaTh=%v", os.Getenv("PATH")),
err: nil,
})
} }
for value, expected := range valids {
actual, err := ValidateEnv(value) for _, tc := range tests {
if err != nil { tc := tc
t.Fatal(err) t.Run(tc.value, func(t *testing.T) {
} actual, err := ValidateEnv(tc.value)
if actual != expected {
t.Fatalf("Expected [%v], got [%v]", expected, actual) if tc.err == nil {
assert.NilError(t, err)
} else {
assert.Error(t, err, tc.err.Error())
} }
assert.Equal(t, actual, tc.expected)
})
} }
} }