Skip to content

Commit fc6b590

Browse files
DaveZLBbadboy-huaqiao
authored andcommitted
[feat] Add override environment support
Issue #236 Signed-off-by: zhanglibin <zlibin@vmware.com>
1 parent 06ea564 commit fc6b590

File tree

3 files changed

+89
-8
lines changed

3 files changed

+89
-8
lines changed

go.mod

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,23 @@ module github.com/edgexfoundry/edgex-ui-go
33
go 1.13
44

55
require (
6-
github.com/BurntSushi/toml v0.3.1
76
github.com/armon/go-metrics v0.3.3 // indirect
87
github.com/eclipse/paho.mqtt.golang v1.2.0
98
github.com/edgexfoundry/go-mod-registry v0.1.11
10-
github.com/gomodule/redigo v2.0.0+incompatible // indirect
119
github.com/gorilla/mux v1.7.1
1210
github.com/gorilla/websocket v1.4.0
13-
github.com/hashicorp/consul v1.7.2 // indirect
1411
github.com/hashicorp/consul/api v1.4.0 // indirect
1512
github.com/hashicorp/go-hclog v0.12.2 // indirect
1613
github.com/hashicorp/go-immutable-radix v1.2.0 // indirect
14+
github.com/hashicorp/go-msgpack v0.5.5 // indirect
15+
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
1716
github.com/hashicorp/golang-lru v0.5.4 // indirect
1817
github.com/hashicorp/serf v0.9.0 // indirect
1918
github.com/mattn/go-colorable v0.1.6 // indirect
2019
github.com/mitchellh/mapstructure v1.2.2 // indirect
21-
github.com/pebbe/zmq4 v1.2.0 // indirect
20+
github.com/mitchellh/reflectwalk v1.0.1 // indirect
2221
github.com/pelletier/go-toml v1.7.0
23-
go.mongodb.org/mongo-driver v1.3.1 // indirect
22+
golang.org/x/crypto v0.0.0-20191106202628-ed6320f186d4 // indirect
2423
golang.org/x/sys v0.0.0-20200409092240-59c9f1ba88fa // indirect
2524
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce
2625
)

internal/configs/config.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ package configs
1818
import (
1919
"log"
2020
"path/filepath"
21-
"github.com/BurntSushi/toml"
21+
"github.com/pelletier/go-toml"
2222
"fmt"
2323
)
2424

@@ -86,15 +86,26 @@ func LoadConfig(confFilePath string) error {
8686
if len(confFilePath) == 0 {
8787
confFilePath = defaultConfigFilePath
8888
}
89-
9089
absPath, err := filepath.Abs(confFilePath)
9190
if err != nil {
9291
log.Printf("Could not create absolute path to load configuration: %s; %v", absPath, err.Error())
9392
return err
9493
}
9594
log.Printf("Loading configuration from: %s\n", absPath)
95+
configTree, err := toml.LoadFile(absPath)
96+
if err != nil{
97+
log.Printf("Load Config File Error:%v", err)
98+
return err
99+
}
100+
//Override configuration from Env
101+
env := NewEnvironment()
102+
configTree,err = env.OverrideFromEnvironment(configTree)
103+
if err != nil {
104+
log.Printf("Override from environment error%v", err)
105+
return err
106+
}
96107
var conf ConfigurationStruct
97-
if _, err := toml.DecodeFile(absPath, &conf); err != nil {
108+
if err := configTree.Unmarshal(&conf); err != nil {
98109
log.Printf("Decode Config File Error:%v", err)
99110
return err
100111
}

internal/configs/environment.go

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*******************************************************************************
2+
* Copyright © 2018-2020 VMware, Inc. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5+
* in compliance with the License. You may obtain a copy of the License at
6+
*
7+
* http://www.apache.org/licenses/LICENSE-2.0
8+
*
9+
* Unless required by applicable law or agreed to in writing, software distributed under the License
10+
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11+
* or implied. See the License for the specific language governing permissions and limitations under
12+
* the License.
13+
*
14+
*******************************************************************************/
15+
16+
package configs
17+
18+
import (
19+
"os"
20+
"strings"
21+
"github.com/pelletier/go-toml"
22+
"reflect"
23+
"strconv"
24+
)
25+
26+
type environment struct {
27+
env map[string]interface{}
28+
}
29+
30+
func NewEnvironment() *environment {
31+
osEnv := os.Environ()
32+
e := &environment{
33+
env: make(map[string]interface{}, len(osEnv)),
34+
}
35+
for _, e1 := range osEnv {
36+
kv := strings.Split(e1, "=")
37+
if len(kv) == 2 && len(kv[0]) > 0 && len(kv[1]) > 0 {
38+
e.env[kv[0]] = kv[1]
39+
}
40+
}
41+
return e
42+
}
43+
44+
func (e *environment) OverrideFromEnvironment(tree *toml.Tree) (*toml.Tree, error) {
45+
for k, v := range e.env {
46+
k = strings.Replace(k, "_", ".", -1)
47+
if tree.Has(k) {
48+
// global key
49+
realType := reflect.TypeOf(tree.Get(k)).Kind()
50+
switch realType {
51+
case reflect.Int64:
52+
v, err := strconv.ParseInt(v.(string), 10, 64)
53+
if err != nil {
54+
return nil, err
55+
}
56+
tree.Set(k, v)
57+
break
58+
case reflect.Int:
59+
v, err := strconv.Atoi(v.(string))
60+
if err != nil {
61+
return nil, err
62+
}
63+
tree.Set(k, v)
64+
break
65+
default:
66+
tree.Set(k, v)
67+
}
68+
}
69+
}
70+
return tree, nil
71+
}

0 commit comments

Comments
 (0)