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