Remove redundant hardcoded environment variables

The following hardcoded environment variables are removed:

* CONSUL_RPC_ADDR
* CONSUL_HTTP_ADDR
This commit is contained in:
Hasyimi Bahrudin 2016-08-03 14:10:11 +08:00
parent 94c14275c0
commit b2ad8114fe
5 changed files with 23 additions and 18 deletions

View File

@ -20,6 +20,12 @@ import (
"github.com/hashicorp/go-cleanhttp"
)
const (
// HTTPAddrEnvName defines an environment variable name which sets
// the HTTP address if there is no -http-addr specified.
HTTPAddrEnvName = "CONSUL_HTTP_ADDR"
)
// QueryOptions are used to parameterize a query
type QueryOptions struct {
// Providing a datacenter overwrites the DC provided
@ -178,7 +184,7 @@ func defaultConfig(transportFn func() *http.Transport) *Config {
},
}
if addr := os.Getenv("CONSUL_HTTP_ADDR"); addr != "" {
if addr := os.Getenv(HTTPAddrEnvName); addr != "" {
config.Address = addr
}

View File

@ -76,8 +76,8 @@ func TestDefaultConfig_env(t *testing.T) {
token := "abcd1234"
auth := "username:password"
os.Setenv("CONSUL_HTTP_ADDR", addr)
defer os.Setenv("CONSUL_HTTP_ADDR", "")
os.Setenv(HTTPAddrEnvName, addr)
defer os.Setenv(HTTPAddrEnvName, "")
os.Setenv("CONSUL_HTTP_TOKEN", token)
defer os.Setenv("CONSUL_HTTP_TOKEN", "")
os.Setenv("CONSUL_HTTP_AUTH", auth)

View File

@ -13,6 +13,12 @@ import (
"sync/atomic"
)
const (
// RPCAddrEnvName defines an environment variable name which sets
// an RPC address if there is no -rpc-addr specified.
RPCAddrEnvName = "CONSUL_RPC_ADDR"
)
var (
clientClosed = fmt.Errorf("client closed")
)
@ -84,7 +90,7 @@ func NewRPCClient(addr string) (*RPCClient, error) {
var conn net.Conn
var err error
if envAddr := os.Getenv("CONSUL_RPC_ADDR"); envAddr != "" {
if envAddr := os.Getenv(RPCAddrEnvName); envAddr != "" {
addr = envAddr
}

View File

@ -8,20 +8,10 @@ import (
"github.com/hashicorp/consul/command/agent"
)
const (
// RPCAddrEnvName defines an environment variable name which sets
// an RPC address if there is no -rpc-addr specified.
RPCAddrEnvName = "CONSUL_RPC_ADDR"
// HTTPAddrEnvName defines an environment variable name which sets
// the HTTP address if there is no -http-addr specified.
HTTPAddrEnvName = "CONSUL_HTTP_ADDR"
)
// RPCAddrFlag returns a pointer to a string that will be populated
// when the given flagset is parsed with the RPC address of the Consul.
func RPCAddrFlag(f *flag.FlagSet) *string {
defaultRPCAddr := os.Getenv(RPCAddrEnvName)
defaultRPCAddr := os.Getenv(agent.RPCAddrEnvName)
if defaultRPCAddr == "" {
defaultRPCAddr = "127.0.0.1:8400"
}
@ -37,7 +27,7 @@ func RPCClient(addr string) (*agent.RPCClient, error) {
// HTTPAddrFlag returns a pointer to a string that will be populated
// when the given flagset is parsed with the HTTP address of the Consul.
func HTTPAddrFlag(f *flag.FlagSet) *string {
defaultHTTPAddr := os.Getenv(HTTPAddrEnvName)
defaultHTTPAddr := os.Getenv(consulapi.HTTPAddrEnvName)
if defaultHTTPAddr == "" {
defaultHTTPAddr = "127.0.0.1:8500"
}

View File

@ -4,6 +4,9 @@ import (
"flag"
"os"
"testing"
consulapi "github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/command/agent"
)
const (
@ -21,11 +24,11 @@ func getParsedAddr(t *testing.T, addrType, cliVal, envVal string) string {
switch addrType {
case "rpc":
fn = RPCAddrFlag
envVar = RPCAddrEnvName
envVar = agent.RPCAddrEnvName
cliFlag = "-rpc-addr"
case "http":
fn = HTTPAddrFlag
envVar = HTTPAddrEnvName
envVar = consulapi.HTTPAddrEnvName
cliFlag = "-http-addr"
default:
t.Fatalf("unknown address type %s", addrType)