diff --git a/api/api.go b/api/api.go index c418cd2293..7ed8532941 100644 --- a/api/api.go +++ b/api/api.go @@ -71,6 +71,10 @@ const ( // client in this package but is defined here for consistency with all the // other ENV names we use. GRPCAddrEnvName = "CONSUL_GRPC_ADDR" + + // HTTPNamespaceEnvVar defines an environment variable name which sets + // the HTTP Namespace to be used by default. This can still be overridden. + HTTPNamespaceEnvName = "CONSUL_NAMESPACE" ) // QueryOptions are used to parameterize a query @@ -427,6 +431,10 @@ func defaultConfig(transportFn func() *http.Transport) *Config { } } + if v := os.Getenv(HTTPNamespaceEnvName); v != "" { + config.Namespace = v + } + return config } diff --git a/command/flags/http.go b/command/flags/http.go index c473dc1d0e..947c5947d9 100644 --- a/command/flags/http.go +++ b/command/flags/http.go @@ -76,8 +76,7 @@ func (f *HTTPFlags) ServerFlags() *flag.FlagSet { func (f *HTTPFlags) NamespaceFlags() *flag.FlagSet { fs := flag.NewFlagSet("", flag.ContinueOnError) - // TODO (namespaces) Do we want to allow setting via an env var? CONSUL_NAMESPACE - fs.Var(&f.namespace, "ns", + fs.Var(&f.namespace, "namespace", "Specifies the namespace to query. If not provided, the namespace will"+ "default to the `default` namespace. Namespaces is a Consul Enterprise feature.") return fs diff --git a/website/source/docs/commands/_http_api_namespace_options.html.md b/website/source/docs/commands/_http_api_namespace_options.html.md index 76a28d0c46..2a33ef5ee0 100644 --- a/website/source/docs/commands/_http_api_namespace_options.html.md +++ b/website/source/docs/commands/_http_api_namespace_options.html.md @@ -1,2 +1,3 @@ -* `-ns=` - Specifies the namespace to query. If not provided, the namespace - will default to the `default` namespace. Namespaces is a Consul Enterprise feature added in v1.7.0. +* `-namespace=` - Specifies the namespace to query. If not provided, the namespace will default + to the value of the `CONSUL_NAMESPACE` environment variable or `default` if neither is set. Namespaces + is a Consul Enterprise feature added in v1.7.0.