diff --git a/api/api.go b/api/api.go index b0fe6d83b7..01430a6421 100644 --- a/api/api.go +++ b/api/api.go @@ -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 } diff --git a/api/api_test.go b/api/api_test.go index 71f01c483a..786998dd1a 100644 --- a/api/api_test.go +++ b/api/api_test.go @@ -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) diff --git a/command/agent/rpc_client.go b/command/agent/rpc_client.go index 3ce90b1634..744866b415 100644 --- a/command/agent/rpc_client.go +++ b/command/agent/rpc_client.go @@ -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 } diff --git a/command/rpc.go b/command/rpc.go index 2c63a9b227..dc0cefbd47 100644 --- a/command/rpc.go +++ b/command/rpc.go @@ -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" } diff --git a/command/rpc_test.go b/command/rpc_test.go index 194c06b63c..effe759024 100644 --- a/command/rpc_test.go +++ b/command/rpc_test.go @@ -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)