github.com/dkerwin/nomad@v0.3.3-0.20160525181927-74554135514b/command/meta.go (about) 1 package command 2 3 import ( 4 "bufio" 5 "flag" 6 "io" 7 "os" 8 "strings" 9 10 "github.com/hashicorp/nomad/api" 11 "github.com/mitchellh/cli" 12 "github.com/mitchellh/colorstring" 13 ) 14 15 const ( 16 // Names of environment variables used to supply various 17 // config options to the Nomad CLI. 18 EnvNomadAddress = "NOMAD_ADDR" 19 20 // Constants for CLI identifier length 21 shortId = 8 22 fullId = 36 23 ) 24 25 // FlagSetFlags is an enum to define what flags are present in the 26 // default FlagSet returned by Meta.FlagSet. 27 type FlagSetFlags uint 28 29 const ( 30 FlagSetNone FlagSetFlags = 0 31 FlagSetClient FlagSetFlags = 1 << iota 32 FlagSetDefault = FlagSetClient 33 ) 34 35 // Meta contains the meta-options and functionality that nearly every 36 // Nomad command inherits. 37 type Meta struct { 38 Ui cli.Ui 39 40 // These are set by the command line flags. 41 flagAddress string 42 43 // Whether to not-colorize output 44 noColor bool 45 } 46 47 // FlagSet returns a FlagSet with the common flags that every 48 // command implements. The exact behavior of FlagSet can be configured 49 // using the flags as the second parameter, for example to disable 50 // server settings on the commands that don't talk to a server. 51 func (m *Meta) FlagSet(n string, fs FlagSetFlags) *flag.FlagSet { 52 f := flag.NewFlagSet(n, flag.ContinueOnError) 53 54 // FlagSetClient is used to enable the settings for specifying 55 // client connectivity options. 56 if fs&FlagSetClient != 0 { 57 f.StringVar(&m.flagAddress, "address", "", "") 58 f.BoolVar(&m.noColor, "no-color", false, "") 59 } 60 61 // Create an io.Writer that writes to our UI properly for errors. 62 // This is kind of a hack, but it does the job. Basically: create 63 // a pipe, use a scanner to break it into lines, and output each line 64 // to the UI. Do this forever. 65 errR, errW := io.Pipe() 66 errScanner := bufio.NewScanner(errR) 67 go func() { 68 for errScanner.Scan() { 69 m.Ui.Error(errScanner.Text()) 70 } 71 }() 72 f.SetOutput(errW) 73 74 return f 75 } 76 77 // Client is used to initialize and return a new API client using 78 // the default command line arguments and env vars. 79 func (m *Meta) Client() (*api.Client, error) { 80 config := api.DefaultConfig() 81 if v := os.Getenv(EnvNomadAddress); v != "" { 82 config.Address = v 83 } 84 if m.flagAddress != "" { 85 config.Address = m.flagAddress 86 } 87 return api.NewClient(config) 88 } 89 90 func (m *Meta) Colorize() *colorstring.Colorize { 91 return &colorstring.Colorize{ 92 Colors: colorstring.DefaultColors, 93 Disable: m.noColor, 94 Reset: true, 95 } 96 } 97 98 // generalOptionsUsage returns the help string for the global options. 99 func generalOptionsUsage() string { 100 helpText := ` 101 -address=<addr> 102 The address of the Nomad server. 103 Overrides the NOMAD_ADDR environment variable if set. 104 Default = http://127.0.0.1:4646 105 ` 106 return strings.TrimSpace(helpText) 107 }