[lxc-devel] [distrobuilder/master] Use map for environment variables

monstermunchkin on Github lxc-bot at linuxcontainers.org
Wed Dec 19 14:05:08 UTC 2018


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 310 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20181219/15df2fa6/attachment.bin>
-------------- next part --------------
From 3ecf67d55da1eb63ac7f9e7cad61b83965f9c37d Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 19 Dec 2018 15:01:40 +0100
Subject: [PATCH 1/2] shared: Use map for environment variables

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/chroot.go | 45 +++++++++++++++++++++------------------------
 shared/util.go   | 24 +++++++++++++-----------
 2 files changed, 34 insertions(+), 35 deletions(-)

diff --git a/shared/chroot.go b/shared/chroot.go
index 0b28201..eb5695a 100644
--- a/shared/chroot.go
+++ b/shared/chroot.go
@@ -199,30 +199,27 @@ func SetupChroot(rootfs string) (func() error, error) {
 		return nil, err
 	}
 
+	env := Environment{
+		"PATH": EnvVariable{
+			Value: "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
+			Set:   true,
+		},
+		"SHELL": EnvVariable{
+			Value: "/bin/sh",
+			Set:   true,
+		},
+		"TERM": EnvVariable{
+			Value: "xterm",
+			Set:   true,
+		},
+		"DEBIAN_FRONTEND": EnvVariable{
+			Value: "noninteractive",
+			Set:   true,
+		},
+	}
+
 	// Set environment variables
-	oldEnvVariables := SetEnvVariables(
-		[]EnvVariable{
-			{
-				Key:   "PATH",
-				Value: "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin",
-				Set:   true,
-			},
-			{
-				Key:   "SHELL",
-				Value: "/bin/sh",
-				Set:   true,
-			},
-			{
-				Key:   "TERM",
-				Value: "xterm",
-				Set:   true,
-			},
-			{
-				Key:   "DEBIAN_FRONTEND",
-				Value: "noninteractive",
-				Set:   true,
-			},
-		})
+	oldEnv := SetEnvVariables(env)
 
 	// Setup policy-rc.d override
 	policyCleanup := false
@@ -249,7 +246,7 @@ exit 101
 		}
 
 		// Reset old environment variables
-		SetEnvVariables(oldEnvVariables)
+		SetEnvVariables(oldEnv)
 
 		// Switch back to the host rootfs
 		err = root.Chdir()
diff --git a/shared/util.go b/shared/util.go
index 4cc9389..9be3077 100644
--- a/shared/util.go
+++ b/shared/util.go
@@ -21,11 +21,12 @@ import (
 
 // EnvVariable represents a environment variable.
 type EnvVariable struct {
-	Key   string
 	Value string
 	Set   bool
 }
 
+type Environment map[string]EnvVariable
+
 // Copy copies a file.
 func Copy(src, dest string) error {
 	var err error
@@ -244,22 +245,23 @@ func RenderTemplate(template string, iface interface{}) (string, error) {
 
 // SetEnvVariables sets the provided environment variables and returns the
 // old ones.
-func SetEnvVariables(env []EnvVariable) []EnvVariable {
-	oldEnv := make([]EnvVariable, len(env))
+func SetEnvVariables(env Environment) Environment {
+	oldEnv := Environment{}
 
-	for i := 0; i < len(env); i++ {
+	for k, v := range env {
 		// Check whether the env variables are set at the moment
-		oldVal, set := os.LookupEnv(env[i].Key)
+		oldVal, set := os.LookupEnv(k)
 
 		// Store old env variables
-		oldEnv[i].Key = env[i].Key
-		oldEnv[i].Value = oldVal
-		oldEnv[i].Set = set
+		oldEnv[k] = EnvVariable{
+			Value: oldVal,
+			Set:   set,
+		}
 
-		if env[i].Set {
-			os.Setenv(env[i].Key, env[i].Value)
+		if v.Set {
+			os.Setenv(k, v.Value)
 		} else {
-			os.Unsetenv(env[i].Key)
+			os.Unsetenv(k)
 		}
 	}
 

From 7e4c6b00c3c778eae1e03beb21ad10adeef27c66 Mon Sep 17 00:00:00 2001
From: Thomas Hipp <thomas.hipp at canonical.com>
Date: Wed, 19 Dec 2018 15:02:10 +0100
Subject: [PATCH 2/2] test: Update TestSetEnvVariables

Signed-off-by: Thomas Hipp <thomas.hipp at canonical.com>
---
 shared/util_test.go | 18 ++++++++++++------
 1 file changed, 12 insertions(+), 6 deletions(-)

diff --git a/shared/util_test.go b/shared/util_test.go
index 360be5e..8f27f5c 100644
--- a/shared/util_test.go
+++ b/shared/util_test.go
@@ -182,18 +182,24 @@ func TestSetEnvVariables(t *testing.T) {
 	// Initial variables
 	os.Setenv("FOO", "bar")
 
-	env := []EnvVariable{
-		{"FOO", "bla", true},
-		{"BAR", "blub", true},
+	env := Environment{
+		"FOO": EnvVariable{
+			Value: "bla",
+			Set:   true,
+		},
+		"BAR": EnvVariable{
+			Value: "blub",
+			Set:   true,
+		},
 	}
 
 	// Set new env variables
 	oldEnv := SetEnvVariables(env)
 
-	for _, e := range env {
-		v, set := os.LookupEnv(e.Key)
+	for k, v := range env {
+		val, set := os.LookupEnv(k)
 		require.True(t, set)
-		require.Equal(t, e.Value, v)
+		require.Equal(t, v.Value, val)
 	}
 
 	// Reset env variables


More information about the lxc-devel mailing list