diff options
author | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-02 09:42:01 +0800 |
---|---|---|
committer | Lvbo163 <lv.bo163@zte.com.cn> | 2018-08-02 09:42:01 +0800 |
commit | 933e3c6f0ce1640c7d00d70c929266603136cb3b (patch) | |
tree | 1057eaa4706000d3c4dc3171a4240bfdb126c7d7 | |
parent | 2919645aa79e54f9a3f0da8d799dc266f961f64f (diff) |
support config consul by env
add env variable 'ConsulAddress' to config consul
Issue-ID: MSB-247
Change-Id: I37770d098751fa448199d692166081584106bf3a
Signed-off-by: Lvbo163 <lv.bo163@zte.com.cn>
-rw-r--r-- | msb2pilot/src/msb2pilot/consul/controller.go | 7 | ||||
-rw-r--r-- | msb2pilot/src/msb2pilot/consul/controller_test.go | 23 | ||||
-rw-r--r-- | msb2pilot/src/msb2pilot/models/config.go | 16 |
3 files changed, 45 insertions, 1 deletions
diff --git a/msb2pilot/src/msb2pilot/consul/controller.go b/msb2pilot/src/msb2pilot/consul/controller.go index 1ba2cd8..b6e798f 100644 --- a/msb2pilot/src/msb2pilot/consul/controller.go +++ b/msb2pilot/src/msb2pilot/consul/controller.go @@ -13,7 +13,9 @@ package consul import ( "msb2pilot/log" + "msb2pilot/models" "msb2pilot/util" + "os" "path/filepath" "github.com/hashicorp/consul/api" @@ -44,6 +46,11 @@ func init() { } func getConsulAddress(path string) string { + res := os.Getenv(models.EnvConsulAddress) + if res != "" { + return res + } + cfg, err := loadCfgInfo(path) if err != nil { log.Log.Error("load consul config info error", err) diff --git a/msb2pilot/src/msb2pilot/consul/controller_test.go b/msb2pilot/src/msb2pilot/consul/controller_test.go index 1b05653..a6569bd 100644 --- a/msb2pilot/src/msb2pilot/consul/controller_test.go +++ b/msb2pilot/src/msb2pilot/consul/controller_test.go @@ -12,33 +12,54 @@ package consul import ( + "msb2pilot/models" + "os" "testing" ) func TestSetConsulAddress(t *testing.T) { cases := []struct { - path, want string + env, path, want string }{ { + env: "testEnv", + path: "", + want: `testEnv`, + }, + { + env: "", path: cfgFilePath, want: `http://127.0.0.1:8500`, }, { + env: "testEnvWithPath", + path: cfgFilePath, + want: `testEnvWithPath`, + }, + { + env: "", path: ``, want: `http://localhost:8500`, }, { + env: "", path: `controller.go`, want: `http://localhost:8500`, }, } + oldEnv := os.Getenv(models.EnvConsulAddress) + for _, cas := range cases { + os.Setenv(models.EnvConsulAddress, cas.env) + res := getConsulAddress(cas.path) if res != cas.want { t.Errorf("getConsulAddress() => want %s, got %s", cas.want, res) } } + + os.Setenv(models.EnvConsulAddress, oldEnv) } func TestLoadCfgInfo(t *testing.T) { diff --git a/msb2pilot/src/msb2pilot/models/config.go b/msb2pilot/src/msb2pilot/models/config.go new file mode 100644 index 0000000..7f34514 --- /dev/null +++ b/msb2pilot/src/msb2pilot/models/config.go @@ -0,0 +1,16 @@ +/** + * Copyright (c) 2018 ZTE Corporation. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and the Apache License 2.0 which both accompany this distribution, + * and are available at http://www.eclipse.org/legal/epl-v10.html + * and http://www.apache.org/licenses/LICENSE-2.0 + * + * Contributors: + * ZTE - initial Project + */ +package models + +const ( + EnvConsulAddress = "ConsulAddress" //http://localhost:8500 +) |