command: remote exec takes -token parameter

This commit is contained in:
Ryan Uber 2015-06-22 17:16:28 -07:00
parent 71aae88f3f
commit 320ab1448d
2 changed files with 16 additions and 9 deletions

View File

@ -55,6 +55,7 @@ const (
type rExecConf struct {
datacenter string
prefix string
token string
foreignDC bool
localDC string
@ -136,6 +137,7 @@ func (c *ExecCommand) Run(args []string) int {
cmdFlags.DurationVar(&c.conf.replWait, "wait-repl", rExecReplicationWait, "")
cmdFlags.DurationVar(&c.conf.wait, "wait", rExecQuietWait, "")
cmdFlags.BoolVar(&c.conf.verbose, "verbose", false, "")
cmdFlags.StringVar(&c.conf.token, "token", "", "")
httpAddr := HTTPAddrFlag(cmdFlags)
if err := cmdFlags.Parse(args); err != nil {
return 1
@ -173,7 +175,11 @@ func (c *ExecCommand) Run(args []string) int {
}
// Create and test the HTTP client
client, err := HTTPClientDC(*httpAddr, c.conf.datacenter)
client, err := HTTPClientConfig(func(clientConf *consulapi.Config) {
clientConf.Address = *httpAddr
clientConf.Datacenter = c.conf.datacenter
clientConf.Token = c.conf.token
})
if err != nil {
c.Ui.Error(fmt.Sprintf("Error connecting to Consul agent: %s", err))
return 1
@ -625,6 +631,8 @@ Options:
-wait-repl=200ms Period to wait for replication before firing event. This is an
optimization to allow stale reads to be performed.
-verbose Enables verbose output
-token="" ACL token to use during requests. Defaults to that
of the agent.
`
return strings.TrimSpace(helpText)
}

View File

@ -47,16 +47,15 @@ func HTTPAddrFlag(f *flag.FlagSet) *string {
// HTTPClient returns a new Consul HTTP client with the given address.
func HTTPClient(addr string) (*consulapi.Client, error) {
return HTTPClientDC(addr, "")
return HTTPClientConfig(func(c *consulapi.Config) {
c.Address = addr
})
}
// HTTPClientDC returns a new Consul HTTP client with the given address and datacenter
func HTTPClientDC(addr, dc string) (*consulapi.Client, error) {
// 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) {
conf := consulapi.DefaultConfig()
if envAddr := os.Getenv(HTTPAddrEnvName); addr == "" && envAddr != "" {
addr = envAddr
}
conf.Address = addr
conf.Datacenter = dc
fn(conf)
return consulapi.NewClient(conf)
}