summaryrefslogtreecommitdiffstats
path: root/config
diff options
context:
space:
mode:
Diffstat (limited to 'config')
-rw-r--r--config/configLoader.go2
-rw-r--r--config/configProvider.go8
-rw-r--r--config/env.go24
-rw-r--r--config/env_test.go32
4 files changed, 62 insertions, 4 deletions
diff --git a/config/configLoader.go b/config/configLoader.go
index b8aa3fda3..2e37a5b35 100644
--- a/config/configLoader.go
+++ b/config/configLoader.go
@@ -120,8 +120,6 @@ func RenameKeys(m map[string]interface{}) {
func newViper() *viper.Viper {
v := viper.New()
- v.AutomaticEnv()
- v.SetEnvPrefix("hugo")
return v
}
diff --git a/config/configProvider.go b/config/configProvider.go
index 31914c38b..187fb7b10 100644
--- a/config/configProvider.go
+++ b/config/configProvider.go
@@ -35,10 +35,14 @@ type Provider interface {
// we do not attempt to split it into fields.
func GetStringSlicePreserveString(cfg Provider, key string) []string {
sd := cfg.Get(key)
- if sds, ok := sd.(string); ok {
+ return toStringSlicePreserveString(sd)
+}
+
+func toStringSlicePreserveString(v interface{}) []string {
+ if sds, ok := v.(string); ok {
return []string{sds}
}
- return cast.ToStringSlice(sd)
+ return cast.ToStringSlice(v)
}
// SetBaseTestDefaults provides some common config defaults used in tests.
diff --git a/config/env.go b/config/env.go
index adf6f9b68..f482cd247 100644
--- a/config/env.go
+++ b/config/env.go
@@ -17,6 +17,7 @@ import (
"os"
"runtime"
"strconv"
+ "strings"
)
// GetNumWorkerMultiplier returns the base value used to calculate the number
@@ -31,3 +32,26 @@ func GetNumWorkerMultiplier() int {
}
return runtime.NumCPU()
}
+
+// SetEnvVars sets vars on the form key=value in the oldVars slice.
+func SetEnvVars(oldVars *[]string, keyValues ...string) {
+ for i := 0; i < len(keyValues); i += 2 {
+ setEnvVar(oldVars, keyValues[i], keyValues[i+1])
+ }
+}
+
+func SplitEnvVar(v string) (string, string) {
+ parts := strings.Split(v, "=")
+ return parts[0], parts[1]
+}
+
+func setEnvVar(vars *[]string, key, value string) {
+ for i := range *vars {
+ if strings.HasPrefix((*vars)[i], key+"=") {
+ (*vars)[i] = key + "=" + value
+ return
+ }
+ }
+ // New var.
+ *vars = append(*vars, key+"="+value)
+}
diff --git a/config/env_test.go b/config/env_test.go
new file mode 100644
index 000000000..594c3e871
--- /dev/null
+++ b/config/env_test.go
@@ -0,0 +1,32 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package config
+
+import (
+ "testing"
+
+ "github.com/stretchr/testify/require"
+)
+
+func TestSetEnvVars(t *testing.T) {
+ t.Parallel()
+ assert := require.New(t)
+ vars := []string{"FOO=bar", "HUGO=cool", "BAR=foo"}
+ SetEnvVars(&vars, "HUGO", "rocking!", "NEW", "bar")
+ assert.Equal([]string{"FOO=bar", "HUGO=rocking!", "BAR=foo", "NEW=bar"}, vars)
+
+ key, val := SplitEnvVar("HUGO=rocks")
+ assert.Equal("HUGO", key)
+ assert.Equal("rocks", val)
+}