2015-06-01 13:25:51 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"flag"
|
|
|
|
"io"
|
2015-09-14 20:13:52 +00:00
|
|
|
"os"
|
|
|
|
"strings"
|
2015-06-01 13:25:51 +00:00
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
"github.com/hashicorp/nomad/api"
|
2015-06-01 13:25:51 +00:00
|
|
|
"github.com/mitchellh/cli"
|
2016-05-13 00:17:02 +00:00
|
|
|
"github.com/mitchellh/colorstring"
|
2015-06-01 13:25:51 +00:00
|
|
|
)
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
const (
|
|
|
|
// Names of environment variables used to supply various
|
|
|
|
// config options to the Nomad CLI.
|
|
|
|
EnvNomadAddress = "NOMAD_ADDR"
|
2016-06-07 18:16:04 +00:00
|
|
|
EnvNomadRegion = "NOMAD_REGION"
|
2016-01-14 20:57:43 +00:00
|
|
|
|
|
|
|
// Constants for CLI identifier length
|
2016-01-15 22:32:38 +00:00
|
|
|
shortId = 8
|
|
|
|
fullId = 36
|
2015-09-14 20:13:52 +00:00
|
|
|
)
|
|
|
|
|
2015-06-01 13:25:51 +00:00
|
|
|
// FlagSetFlags is an enum to define what flags are present in the
|
|
|
|
// default FlagSet returned by Meta.FlagSet.
|
|
|
|
type FlagSetFlags uint
|
|
|
|
|
|
|
|
const (
|
|
|
|
FlagSetNone FlagSetFlags = 0
|
2015-09-14 20:13:52 +00:00
|
|
|
FlagSetClient FlagSetFlags = 1 << iota
|
|
|
|
FlagSetDefault = FlagSetClient
|
2015-06-01 13:25:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Meta contains the meta-options and functionality that nearly every
|
|
|
|
// Nomad command inherits.
|
|
|
|
type Meta struct {
|
|
|
|
Ui cli.Ui
|
|
|
|
|
|
|
|
// These are set by the command line flags.
|
|
|
|
flagAddress string
|
2016-05-13 00:17:02 +00:00
|
|
|
|
|
|
|
// Whether to not-colorize output
|
|
|
|
noColor bool
|
2016-06-07 18:16:04 +00:00
|
|
|
|
|
|
|
// The region to send API requests
|
|
|
|
region string
|
2015-06-01 13:25:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// FlagSet returns a FlagSet with the common flags that every
|
|
|
|
// command implements. The exact behavior of FlagSet can be configured
|
|
|
|
// using the flags as the second parameter, for example to disable
|
|
|
|
// server settings on the commands that don't talk to a server.
|
|
|
|
func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet {
|
|
|
|
f := flag.NewFlagSet(n, flag.ContinueOnError)
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
// FlagSetClient is used to enable the settings for specifying
|
|
|
|
// client connectivity options.
|
|
|
|
if fs&FlagSetClient != 0 {
|
2015-06-01 13:25:51 +00:00
|
|
|
f.StringVar(&m.flagAddress, "address", "", "")
|
2016-06-07 18:16:04 +00:00
|
|
|
f.StringVar(&m.region, "region", "", "")
|
2016-05-13 00:17:02 +00:00
|
|
|
f.BoolVar(&m.noColor, "no-color", false, "")
|
2015-06-01 13:25:51 +00:00
|
|
|
}
|
|
|
|
|
2015-09-29 21:16:55 +00:00
|
|
|
// Create an io.Writer that writes to our UI properly for errors.
|
2015-06-01 13:25:51 +00:00
|
|
|
// This is kind of a hack, but it does the job. Basically: create
|
|
|
|
// a pipe, use a scanner to break it into lines, and output each line
|
|
|
|
// to the UI. Do this forever.
|
|
|
|
errR, errW := io.Pipe()
|
|
|
|
errScanner := bufio.NewScanner(errR)
|
|
|
|
go func() {
|
|
|
|
for errScanner.Scan() {
|
|
|
|
m.Ui.Error(errScanner.Text())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
f.SetOutput(errW)
|
|
|
|
|
|
|
|
return f
|
|
|
|
}
|
2015-09-14 20:13:52 +00:00
|
|
|
|
|
|
|
// Client is used to initialize and return a new API client using
|
|
|
|
// the default command line arguments and env vars.
|
|
|
|
func (m *Meta) Client() (*api.Client, error) {
|
|
|
|
config := api.DefaultConfig()
|
|
|
|
if v := os.Getenv(EnvNomadAddress); v != "" {
|
|
|
|
config.Address = v
|
|
|
|
}
|
|
|
|
if m.flagAddress != "" {
|
|
|
|
config.Address = m.flagAddress
|
|
|
|
}
|
2016-06-07 18:16:04 +00:00
|
|
|
if v := os.Getenv(EnvNomadRegion); v != "" {
|
|
|
|
config.Region = v
|
|
|
|
}
|
|
|
|
if m.region != "" {
|
|
|
|
config.Region = m.region
|
|
|
|
}
|
2015-09-14 20:13:52 +00:00
|
|
|
return api.NewClient(config)
|
|
|
|
}
|
|
|
|
|
2016-05-13 00:17:02 +00:00
|
|
|
func (m *Meta) Colorize() *colorstring.Colorize {
|
|
|
|
return &colorstring.Colorize{
|
|
|
|
Colors: colorstring.DefaultColors,
|
|
|
|
Disable: m.noColor,
|
|
|
|
Reset: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-14 20:13:52 +00:00
|
|
|
// generalOptionsUsage returns the help string for the global options.
|
|
|
|
func generalOptionsUsage() string {
|
|
|
|
helpText := `
|
|
|
|
-address=<addr>
|
|
|
|
The address of the Nomad server.
|
|
|
|
Overrides the NOMAD_ADDR environment variable if set.
|
|
|
|
Default = http://127.0.0.1:4646
|
2016-06-07 18:16:04 +00:00
|
|
|
|
|
|
|
-region=<region>
|
2016-06-07 18:33:55 +00:00
|
|
|
The region of the Nomad servers to forward commands to.
|
2016-06-07 18:16:04 +00:00
|
|
|
Overrides the NOMAD_REGION environment variable if set.
|
|
|
|
Defaults to the Agent's local region.
|
2015-09-14 20:13:52 +00:00
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|