2013-12-31 00:09:39 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2014-12-15 18:16:43 +00:00
|
|
|
"os"
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2013-12-31 00:09:39 +00:00
|
|
|
"github.com/hashicorp/consul/command/agent"
|
|
|
|
)
|
|
|
|
|
2015-01-17 01:45:13 +00:00
|
|
|
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"
|
|
|
|
)
|
2014-12-15 18:16:43 +00:00
|
|
|
|
2013-12-31 00:09:39 +00:00
|
|
|
// 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 {
|
2014-12-15 18:16:43 +00:00
|
|
|
defaultRPCAddr := os.Getenv(RPCAddrEnvName)
|
|
|
|
if defaultRPCAddr == "" {
|
|
|
|
defaultRPCAddr = "127.0.0.1:8400"
|
|
|
|
}
|
|
|
|
return f.String("rpc-addr", defaultRPCAddr,
|
2013-12-31 00:09:39 +00:00
|
|
|
"RPC address of the Consul agent")
|
|
|
|
}
|
|
|
|
|
|
|
|
// RPCClient returns a new Consul RPC client with the given address.
|
|
|
|
func RPCClient(addr string) (*agent.RPCClient, error) {
|
|
|
|
return agent.NewRPCClient(addr)
|
|
|
|
}
|
2014-08-21 23:02:41 +00:00
|
|
|
|
|
|
|
// 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 {
|
2015-01-17 01:45:13 +00:00
|
|
|
defaultHTTPAddr := os.Getenv(HTTPAddrEnvName)
|
|
|
|
if defaultHTTPAddr == "" {
|
|
|
|
defaultHTTPAddr = "127.0.0.1:8500"
|
|
|
|
}
|
|
|
|
return f.String("http-addr", defaultHTTPAddr,
|
2014-08-21 23:02:41 +00:00
|
|
|
"HTTP address of the Consul agent")
|
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPClient returns a new Consul HTTP client with the given address.
|
|
|
|
func HTTPClient(addr string) (*consulapi.Client, error) {
|
2015-06-23 00:16:28 +00:00
|
|
|
return HTTPClientConfig(func(c *consulapi.Config) {
|
|
|
|
c.Address = addr
|
|
|
|
})
|
2014-09-01 04:50:09 +00:00
|
|
|
}
|
|
|
|
|
2015-06-23 00:16:28 +00:00
|
|
|
// HTTPClientConfig is used to return a new API client and modify its
|
|
|
|
// configuration by passing in a config modifier function.
|
|
|
|
func HTTPClientConfig(fn func(c *consulapi.Config)) (*consulapi.Client, error) {
|
2014-08-21 23:02:41 +00:00
|
|
|
conf := consulapi.DefaultConfig()
|
2015-06-23 00:16:28 +00:00
|
|
|
fn(conf)
|
2014-08-21 23:02:41 +00:00
|
|
|
return consulapi.NewClient(conf)
|
|
|
|
}
|