Merge pull request #2238 from hasyimibhar/master

Remove duplicated environment variables
pull/2518/head
James Phillips 2016-11-17 16:18:02 -08:00 committed by GitHub
commit c744792fc4
5 changed files with 53 additions and 32 deletions

View File

@ -20,6 +20,28 @@ import (
"github.com/hashicorp/go-cleanhttp" "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"
// HTTPTokenEnvName defines an environment variable name which sets
// the HTTP token.
HTTPTokenEnvName = "CONSUL_HTTP_TOKEN"
// HTTPAuthEnvName defines an environment variable name which sets
// the HTTP authentication header.
HTTPAuthEnvName = "CONSUL_HTTP_AUTH"
// HTTPSSLEnvName defines an environment variable name which sets
// whether or not to use HTTPS.
HTTPSSLEnvName = "CONSUL_HTTP_SSL"
// HTTPSSLVerifyEnvName defines an environment variable name which sets
// whether or not to disable certificate checking.
HTTPSSLVerifyEnvName = "CONSUL_HTTP_SSL_VERIFY"
)
// QueryOptions are used to parameterize a query // QueryOptions are used to parameterize a query
type QueryOptions struct { type QueryOptions struct {
// Providing a datacenter overwrites the DC provided // Providing a datacenter overwrites the DC provided
@ -181,15 +203,15 @@ func defaultConfig(transportFn func() *http.Transport) *Config {
}, },
} }
if addr := os.Getenv("CONSUL_HTTP_ADDR"); addr != "" { if addr := os.Getenv(HTTPAddrEnvName); addr != "" {
config.Address = addr config.Address = addr
} }
if token := os.Getenv("CONSUL_HTTP_TOKEN"); token != "" { if token := os.Getenv(HTTPTokenEnvName); token != "" {
config.Token = token config.Token = token
} }
if auth := os.Getenv("CONSUL_HTTP_AUTH"); auth != "" { if auth := os.Getenv(HTTPAuthEnvName); auth != "" {
var username, password string var username, password string
if strings.Contains(auth, ":") { if strings.Contains(auth, ":") {
split := strings.SplitN(auth, ":", 2) split := strings.SplitN(auth, ":", 2)
@ -205,10 +227,10 @@ func defaultConfig(transportFn func() *http.Transport) *Config {
} }
} }
if ssl := os.Getenv("CONSUL_HTTP_SSL"); ssl != "" { if ssl := os.Getenv(HTTPSSLEnvName); ssl != "" {
enabled, err := strconv.ParseBool(ssl) enabled, err := strconv.ParseBool(ssl)
if err != nil { if err != nil {
log.Printf("[WARN] client: could not parse CONSUL_HTTP_SSL: %s", err) log.Printf("[WARN] client: could not parse %s: %s", HTTPSSLEnvName, err)
} }
if enabled { if enabled {
@ -216,10 +238,10 @@ func defaultConfig(transportFn func() *http.Transport) *Config {
} }
} }
if verify := os.Getenv("CONSUL_HTTP_SSL_VERIFY"); verify != "" { if verify := os.Getenv(HTTPSSLVerifyEnvName); verify != "" {
doVerify, err := strconv.ParseBool(verify) doVerify, err := strconv.ParseBool(verify)
if err != nil { if err != nil {
log.Printf("[WARN] client: could not parse CONSUL_HTTP_SSL_VERIFY: %s", err) log.Printf("[WARN] client: could not parse %s: %s", HTTPSSLVerifyEnvName, err)
} }
if !doVerify { if !doVerify {

View File

@ -76,16 +76,16 @@ func TestDefaultConfig_env(t *testing.T) {
token := "abcd1234" token := "abcd1234"
auth := "username:password" auth := "username:password"
os.Setenv("CONSUL_HTTP_ADDR", addr) os.Setenv(HTTPAddrEnvName, addr)
defer os.Setenv("CONSUL_HTTP_ADDR", "") defer os.Setenv(HTTPAddrEnvName, "")
os.Setenv("CONSUL_HTTP_TOKEN", token) os.Setenv(HTTPTokenEnvName, token)
defer os.Setenv("CONSUL_HTTP_TOKEN", "") defer os.Setenv(HTTPTokenEnvName, "")
os.Setenv("CONSUL_HTTP_AUTH", auth) os.Setenv(HTTPAuthEnvName, auth)
defer os.Setenv("CONSUL_HTTP_AUTH", "") defer os.Setenv(HTTPAuthEnvName, "")
os.Setenv("CONSUL_HTTP_SSL", "1") os.Setenv(HTTPSSLEnvName, "1")
defer os.Setenv("CONSUL_HTTP_SSL", "") defer os.Setenv(HTTPSSLEnvName, "")
os.Setenv("CONSUL_HTTP_SSL_VERIFY", "0") os.Setenv(HTTPSSLVerifyEnvName, "0")
defer os.Setenv("CONSUL_HTTP_SSL_VERIFY", "") defer os.Setenv(HTTPSSLVerifyEnvName, "")
for i, config := range []*Config{DefaultConfig(), DefaultNonPooledConfig()} { for i, config := range []*Config{DefaultConfig(), DefaultNonPooledConfig()} {
if config.Address != addr { if config.Address != addr {

View File

@ -13,6 +13,12 @@ import (
"sync/atomic" "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 ( var (
clientClosed = fmt.Errorf("client closed") clientClosed = fmt.Errorf("client closed")
) )
@ -84,7 +90,7 @@ func NewRPCClient(addr string) (*RPCClient, error) {
var conn net.Conn var conn net.Conn
var err error var err error
if envAddr := os.Getenv("CONSUL_RPC_ADDR"); envAddr != "" { if envAddr := os.Getenv(RPCAddrEnvName); envAddr != "" {
addr = envAddr addr = envAddr
} }

View File

@ -8,20 +8,10 @@ import (
"github.com/hashicorp/consul/command/agent" "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 // RPCAddrFlag returns a pointer to a string that will be populated
// when the given flagset is parsed with the RPC address of the Consul. // when the given flagset is parsed with the RPC address of the Consul.
func RPCAddrFlag(f *flag.FlagSet) *string { func RPCAddrFlag(f *flag.FlagSet) *string {
defaultRPCAddr := os.Getenv(RPCAddrEnvName) defaultRPCAddr := os.Getenv(agent.RPCAddrEnvName)
if defaultRPCAddr == "" { if defaultRPCAddr == "" {
defaultRPCAddr = "127.0.0.1:8400" 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 // HTTPAddrFlag returns a pointer to a string that will be populated
// when the given flagset is parsed with the HTTP address of the Consul. // when the given flagset is parsed with the HTTP address of the Consul.
func HTTPAddrFlag(f *flag.FlagSet) *string { func HTTPAddrFlag(f *flag.FlagSet) *string {
defaultHTTPAddr := os.Getenv(HTTPAddrEnvName) defaultHTTPAddr := os.Getenv(consulapi.HTTPAddrEnvName)
if defaultHTTPAddr == "" { if defaultHTTPAddr == "" {
defaultHTTPAddr = "127.0.0.1:8500" defaultHTTPAddr = "127.0.0.1:8500"
} }

View File

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