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