-
-
Notifications
You must be signed in to change notification settings - Fork 252
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Ori Shoshan <ori@wing.security>
- Loading branch information
Showing
4 changed files
with
58 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// +build darwin dragonfly freebsd linux netbsd openbsd solaris | ||
|
||
package env | ||
|
||
import "strings" | ||
|
||
func toMap(env []string) map[string]string { | ||
r := map[string]string{} | ||
for _, e := range env { | ||
p := strings.SplitN(e, "=", 2) | ||
r[p[0]] = p[1] | ||
} | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package env | ||
|
||
import "strings" | ||
|
||
func toMap(env []string) map[string]string { | ||
r := map[string]string{} | ||
for _, e := range env { | ||
p := strings.SplitN(e, "=", 2) | ||
|
||
// On Windows, environment variables can start with '='. If so, Split at next character. | ||
// See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58 | ||
prefixEqualSign := false | ||
if len(e) > 0 && e[0] == '=' { | ||
e = e[1:] | ||
prefixEqualSign = true | ||
} | ||
p = strings.SplitN(e, "=", 2) | ||
if prefixEqualSign { | ||
p[0] = "=" + p[0] | ||
} | ||
|
||
r[p[0]] = p[1] | ||
} | ||
return r | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package env | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// On Windows, environment variables can start with '='. This test verifies this behavior without relying on a Windows environment. | ||
// See env_windows.go in the Go source: https://github.com/golang/go/blob/master/src/syscall/env_windows.go#L58 | ||
func TestToMapWindows(t *testing.T) { | ||
envVars := []string{"=::=::\\", "=C:=C:\\test", "VAR=REGULARVAR"} | ||
result := toMap(envVars) | ||
require.Equal(t, map[string]string{ | ||
"=::": "::\\", | ||
"=C:": "C:\\test", | ||
"VAR": "REGULARVAR", | ||
}, result) | ||
} |