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