2017-02-08 00:16:33 +00:00
|
|
|
package base
|
2016-10-09 04:10:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/consul/api"
|
|
|
|
"github.com/mitchellh/cli"
|
|
|
|
text "github.com/tonnerre/golang-text"
|
|
|
|
)
|
|
|
|
|
|
|
|
// maxLineLength is the maximum width of any line.
|
|
|
|
const maxLineLength int = 72
|
|
|
|
|
|
|
|
// FlagSetFlags is an enum to define what flags are present in the
|
|
|
|
// default FlagSet returned.
|
|
|
|
type FlagSetFlags uint
|
|
|
|
|
|
|
|
const (
|
2017-02-08 01:56:49 +00:00
|
|
|
FlagSetNone FlagSetFlags = 1 << iota
|
|
|
|
FlagSetClientHTTP FlagSetFlags = 1 << iota
|
|
|
|
FlagSetServerHTTP FlagSetFlags = 1 << iota
|
2017-02-08 00:16:33 +00:00
|
|
|
|
|
|
|
FlagSetHTTP = FlagSetClientHTTP | FlagSetServerHTTP
|
2016-10-09 04:10:40 +00:00
|
|
|
)
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
type Command struct {
|
2016-10-09 04:10:40 +00:00
|
|
|
Ui cli.Ui
|
|
|
|
Flags FlagSetFlags
|
|
|
|
|
|
|
|
flagSet *flag.FlagSet
|
2017-02-11 00:21:51 +00:00
|
|
|
hidden *flag.FlagSet
|
2016-10-09 04:10:40 +00:00
|
|
|
|
|
|
|
// These are the options which correspond to the HTTP API options
|
2017-04-14 20:37:29 +00:00
|
|
|
httpAddr StringValue
|
|
|
|
token StringValue
|
|
|
|
caFile StringValue
|
|
|
|
caPath StringValue
|
|
|
|
certFile StringValue
|
|
|
|
keyFile StringValue
|
|
|
|
tlsServerName StringValue
|
|
|
|
|
2017-02-24 05:08:43 +00:00
|
|
|
datacenter StringValue
|
|
|
|
stale BoolValue
|
2016-10-09 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// HTTPClient returns a client with the parsed flags. It panics if the command
|
|
|
|
// does not accept HTTP flags or if the flags have not been parsed.
|
2017-02-08 00:16:33 +00:00
|
|
|
func (c *Command) HTTPClient() (*api.Client, error) {
|
|
|
|
if !c.hasClientHTTP() && !c.hasServerHTTP() {
|
2016-10-09 04:10:40 +00:00
|
|
|
panic("no http flags defined")
|
|
|
|
}
|
2017-02-08 00:16:33 +00:00
|
|
|
if !c.flagSet.Parsed() {
|
2016-10-09 04:10:40 +00:00
|
|
|
panic("flags have not been parsed")
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
config := api.DefaultConfig()
|
2017-02-08 01:05:16 +00:00
|
|
|
c.httpAddr.Merge(&config.Address)
|
|
|
|
c.token.Merge(&config.Token)
|
2017-04-14 20:37:29 +00:00
|
|
|
c.caFile.Merge(&config.TLSConfig.CAFile)
|
|
|
|
c.caPath.Merge(&config.TLSConfig.CAPath)
|
|
|
|
c.certFile.Merge(&config.TLSConfig.CertFile)
|
|
|
|
c.keyFile.Merge(&config.TLSConfig.KeyFile)
|
|
|
|
c.tlsServerName.Merge(&config.TLSConfig.Address)
|
2017-02-08 01:05:16 +00:00
|
|
|
c.datacenter.Merge(&config.Datacenter)
|
2017-02-08 00:16:33 +00:00
|
|
|
return api.NewClient(config)
|
2016-10-09 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 01:36:01 +00:00
|
|
|
func (c *Command) HTTPAddr() string {
|
|
|
|
return c.httpAddr.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) HTTPToken() string {
|
|
|
|
return c.token.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Command) HTTPDatacenter() string {
|
|
|
|
return c.datacenter.String()
|
|
|
|
}
|
|
|
|
|
2017-02-09 23:19:34 +00:00
|
|
|
func (c *Command) HTTPStale() bool {
|
|
|
|
var stale bool
|
|
|
|
c.stale.Merge(&stale)
|
|
|
|
return stale
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
// httpFlagsClient is the list of flags that apply to HTTP connections.
|
|
|
|
func (c *Command) httpFlagsClient(f *flag.FlagSet) *flag.FlagSet {
|
2016-10-09 04:10:40 +00:00
|
|
|
if f == nil {
|
|
|
|
f = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
}
|
|
|
|
|
2017-04-14 20:37:29 +00:00
|
|
|
f.Var(&c.caFile, "ca-file",
|
|
|
|
"Path to a CA file to use for TLS when communicating with Consul. This "+
|
|
|
|
"can also be specified via the CONSUL_CACERT environment variable.")
|
|
|
|
f.Var(&c.caPath, "ca-path",
|
|
|
|
"Path to a directory of CA certificates to use for TLS when communicating "+
|
|
|
|
"with Consul. is enabled. This can also be specified via the CONSUL_CAPATH "+
|
|
|
|
"environment variable.")
|
|
|
|
f.Var(&c.certFile, "client-cert",
|
|
|
|
"Path to a client cert file to use for TLS when `verify_incoming` is enabled. This "+
|
|
|
|
"can also be specified via the CONSUL_CLIENT_CERT environment variable.")
|
|
|
|
f.Var(&c.keyFile, "client-key",
|
|
|
|
"Path to a client key file to use for TLS when `verify_incoming` is enabled. This "+
|
|
|
|
"can also be specified via the CONSUL_CLIENT_KEY environment variable.")
|
2017-02-08 01:05:16 +00:00
|
|
|
f.Var(&c.httpAddr, "http-addr",
|
2017-02-10 01:49:17 +00:00
|
|
|
"The `address` and port of the Consul HTTP agent. The value can be an IP "+
|
2016-10-09 04:10:40 +00:00
|
|
|
"address or DNS address, but it must also include the port. This can "+
|
|
|
|
"also be specified via the CONSUL_HTTP_ADDR environment variable. The "+
|
2017-02-10 23:08:39 +00:00
|
|
|
"default value is http://127.0.0.1:8500. The scheme can also be set to "+
|
|
|
|
"HTTPS by setting the environment variable CONSUL_HTTP_SSL=true.")
|
2017-02-08 01:05:16 +00:00
|
|
|
f.Var(&c.token, "token",
|
2016-10-09 04:10:40 +00:00
|
|
|
"ACL token to use in the request. This can also be specified via the "+
|
|
|
|
"CONSUL_HTTP_TOKEN environment variable. If unspecified, the query will "+
|
|
|
|
"default to the token of the Consul agent at the HTTP address.")
|
2017-04-14 20:37:29 +00:00
|
|
|
f.Var(&c.tlsServerName, "tls-server-name",
|
|
|
|
"The server name to use as the SNI host when connecting via TLS. This "+
|
|
|
|
"can also be specified via the CONSUL_TLS_SERVER_NAME environment variable.")
|
2017-02-08 00:16:33 +00:00
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// httpFlagsServer is the list of flags that apply to HTTP connections.
|
|
|
|
func (c *Command) httpFlagsServer(f *flag.FlagSet) *flag.FlagSet {
|
|
|
|
if f == nil {
|
|
|
|
f = flag.NewFlagSet("", flag.ContinueOnError)
|
|
|
|
}
|
|
|
|
|
2017-02-08 01:05:16 +00:00
|
|
|
f.Var(&c.datacenter, "datacenter",
|
2017-02-08 00:16:33 +00:00
|
|
|
"Name of the datacenter to query. If unspecified, this will default to "+
|
|
|
|
"the datacenter of the queried agent.")
|
2017-02-08 01:05:16 +00:00
|
|
|
f.Var(&c.stale, "stale",
|
2016-10-09 04:10:40 +00:00
|
|
|
"Permit any Consul server (non-leader) to respond to this request. This "+
|
|
|
|
"allows for lower latency and higher throughput, but can result in "+
|
|
|
|
"stale data. This option has no effect on non-read operations. The "+
|
|
|
|
"default value is false.")
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFlagSet creates a new flag set for the given command. It automatically
|
|
|
|
// generates help output and adds the appropriate API flags.
|
2017-02-08 00:16:33 +00:00
|
|
|
func (c *Command) NewFlagSet(command cli.Command) *flag.FlagSet {
|
2016-10-09 04:10:40 +00:00
|
|
|
f := flag.NewFlagSet("", flag.ContinueOnError)
|
2017-02-08 00:16:33 +00:00
|
|
|
f.Usage = func() { c.Ui.Error(command.Help()) }
|
|
|
|
|
|
|
|
if c.hasClientHTTP() {
|
|
|
|
c.httpFlagsClient(f)
|
|
|
|
}
|
2016-10-09 04:10:40 +00:00
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
if c.hasServerHTTP() {
|
|
|
|
c.httpFlagsServer(f)
|
2016-10-09 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
errR, errW := io.Pipe()
|
|
|
|
errScanner := bufio.NewScanner(errR)
|
|
|
|
go func() {
|
|
|
|
for errScanner.Scan() {
|
2017-02-08 00:16:33 +00:00
|
|
|
c.Ui.Error(errScanner.Text())
|
2016-10-09 04:10:40 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
f.SetOutput(errW)
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
c.flagSet = f
|
2017-02-11 00:21:51 +00:00
|
|
|
c.hidden = flag.NewFlagSet("", flag.ContinueOnError)
|
2016-10-09 04:10:40 +00:00
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:21:51 +00:00
|
|
|
// HideFlags is used to set hidden flags that will not be shown in help text
|
|
|
|
func (c *Command) HideFlags(flags ...string) {
|
|
|
|
for _, f := range flags {
|
|
|
|
c.hidden.String(f, "", "")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-09 04:10:40 +00:00
|
|
|
// Parse is used to parse the underlying flag set.
|
2017-02-08 00:16:33 +00:00
|
|
|
func (c *Command) Parse(args []string) error {
|
|
|
|
return c.flagSet.Parse(args)
|
2016-10-09 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Help returns the help for this flagSet.
|
2017-02-08 00:16:33 +00:00
|
|
|
func (c *Command) Help() string {
|
2017-02-10 01:49:17 +00:00
|
|
|
// Some commands with subcommands (kv/snapshot) call this without initializing
|
|
|
|
// any flags first, so exit early to avoid a panic
|
2017-02-10 01:00:38 +00:00
|
|
|
if c.flagSet == nil {
|
|
|
|
return ""
|
|
|
|
}
|
2017-02-08 00:16:33 +00:00
|
|
|
return c.helpFlagsFor(c.flagSet)
|
|
|
|
}
|
|
|
|
|
|
|
|
// hasClientHTTP returns true if this meta command contains client HTTP flags.
|
|
|
|
func (c *Command) hasClientHTTP() bool {
|
|
|
|
return c.Flags&FlagSetClientHTTP != 0
|
2016-10-09 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
// hasServerHTTP returns true if this meta command contains server HTTP flags.
|
|
|
|
func (c *Command) hasServerHTTP() bool {
|
|
|
|
return c.Flags&FlagSetServerHTTP != 0
|
2016-10-09 04:10:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// helpFlagsFor visits all flags in the given flag set and prints formatted
|
|
|
|
// help output. This function is sad because there's no "merging" of command
|
|
|
|
// line flags. We explicitly pull out our "common" options into another section
|
|
|
|
// by doing string comparisons :(.
|
2017-02-08 00:16:33 +00:00
|
|
|
func (c *Command) helpFlagsFor(f *flag.FlagSet) string {
|
|
|
|
httpFlagsClient := c.httpFlagsClient(nil)
|
|
|
|
httpFlagsServer := c.httpFlagsServer(nil)
|
2016-10-09 04:10:40 +00:00
|
|
|
|
|
|
|
var out bytes.Buffer
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
firstHTTP := true
|
|
|
|
if c.hasClientHTTP() {
|
|
|
|
if firstHTTP {
|
|
|
|
printTitle(&out, "HTTP API Options")
|
|
|
|
firstHTTP = false
|
|
|
|
}
|
|
|
|
httpFlagsClient.VisitAll(func(f *flag.Flag) {
|
|
|
|
printFlag(&out, f)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
if c.hasServerHTTP() {
|
|
|
|
if firstHTTP {
|
|
|
|
printTitle(&out, "HTTP API Options")
|
|
|
|
firstHTTP = false
|
|
|
|
}
|
|
|
|
httpFlagsServer.VisitAll(func(f *flag.Flag) {
|
|
|
|
printFlag(&out, f)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
firstCommand := true
|
2017-02-07 04:45:58 +00:00
|
|
|
f.VisitAll(func(f *flag.Flag) {
|
|
|
|
// Skip HTTP flags as they will be grouped separately
|
2017-02-11 00:21:51 +00:00
|
|
|
if flagContains(httpFlagsClient, f) || flagContains(httpFlagsServer, f) || flagContains(c.hidden, f) {
|
2017-02-07 04:45:58 +00:00
|
|
|
return
|
|
|
|
}
|
2017-02-08 00:16:33 +00:00
|
|
|
if firstCommand {
|
2017-02-07 04:45:58 +00:00
|
|
|
printTitle(&out, "Command Options")
|
2017-02-08 00:16:33 +00:00
|
|
|
firstCommand = false
|
2017-02-07 04:45:58 +00:00
|
|
|
}
|
|
|
|
printFlag(&out, f)
|
|
|
|
})
|
2016-10-09 04:10:40 +00:00
|
|
|
|
|
|
|
return strings.TrimRight(out.String(), "\n")
|
|
|
|
}
|
|
|
|
|
|
|
|
// printTitle prints a consistently-formatted title to the given writer.
|
|
|
|
func printTitle(w io.Writer, s string) {
|
|
|
|
fmt.Fprintf(w, "%s\n\n", s)
|
|
|
|
}
|
|
|
|
|
|
|
|
// printFlag prints a single flag to the given writer.
|
|
|
|
func printFlag(w io.Writer, f *flag.Flag) {
|
|
|
|
example, _ := flag.UnquoteUsage(f)
|
|
|
|
if example != "" {
|
|
|
|
fmt.Fprintf(w, " -%s=<%s>\n", f.Name, example)
|
|
|
|
} else {
|
|
|
|
fmt.Fprintf(w, " -%s\n", f.Name)
|
|
|
|
}
|
|
|
|
|
|
|
|
indented := wrapAtLength(f.Usage, 5)
|
|
|
|
fmt.Fprintf(w, "%s\n\n", indented)
|
|
|
|
}
|
|
|
|
|
|
|
|
// flagContains returns true if the given flag is contained in the given flag
|
|
|
|
// set or false otherwise.
|
|
|
|
func flagContains(fs *flag.FlagSet, f *flag.Flag) bool {
|
|
|
|
var skip bool
|
|
|
|
|
|
|
|
fs.VisitAll(func(hf *flag.Flag) {
|
|
|
|
if skip {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-02-11 00:21:51 +00:00
|
|
|
if f.Name == hf.Name {
|
2016-10-09 04:10:40 +00:00
|
|
|
skip = true
|
|
|
|
return
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return skip
|
|
|
|
}
|
|
|
|
|
2017-02-08 00:16:33 +00:00
|
|
|
// wrapAtLength wraps the given text at the maxLineLength, taking into account
|
2016-10-09 04:10:40 +00:00
|
|
|
// any provided left padding.
|
|
|
|
func wrapAtLength(s string, pad int) string {
|
|
|
|
wrapped := text.Wrap(s, maxLineLength-pad)
|
|
|
|
lines := strings.Split(wrapped, "\n")
|
|
|
|
for i, line := range lines {
|
|
|
|
lines[i] = strings.Repeat(" ", pad) + line
|
|
|
|
}
|
|
|
|
return strings.Join(lines, "\n")
|
|
|
|
}
|