github.com/leowmjw/otto@v0.2.1-0.20160126165905-6400716cf085/ui/ui.go (about)

     1  package ui
     2  
     3  import "os"
     4  
     5  // Ui is the component of Otto responsible for reading/writing to the
     6  // console.
     7  //
     8  // All Ui implementations MUST expect colorstring[1] style inputs. If
     9  // the output interface doesn't support colors, these must be stripped.
    10  // The StripColors helper in this package can be used to do this. For
    11  // terminals, the Colorize helper in this package can be used.
    12  //
    13  // [1]: github.com/mitchellh/colorstring
    14  type Ui interface {
    15  	// Header, Message, and Raw are all methods for outputting messages
    16  	// to the Ui, all with different styles. Header and Message should
    17  	// be used liberally, and Raw should be scarcely used if possible.
    18  	//
    19  	// Header outputs the message with a style denoting it is a sectional
    20  	// message. An example: "==> TEXT" might be header text.
    21  	//
    22  	// Message outputs the message with a style, but one that is less
    23  	// important looking than Header. Example: "    TEXT" might be
    24  	// the message text, prefixed with spaces so that it lines up with
    25  	// header items.
    26  	//
    27  	// Raw outputs a message with no styling.
    28  	Header(string)
    29  	Message(string)
    30  	Raw(string)
    31  
    32  	// Input is used to ask the user for input. This should be done
    33  	// sparingly or very early on since Otto is meant to be an automated
    34  	// tool.
    35  	Input(*InputOpts) (string, error)
    36  }
    37  
    38  // InputOpts are options for asking for input.
    39  type InputOpts struct {
    40  	// Id is a unique ID for the question being asked that might be
    41  	// used for logging or to look up a prior answered question.
    42  	Id string
    43  
    44  	// Query is a human-friendly question for inputting this value.
    45  	Query string
    46  
    47  	// Description is a description about what this option is. Be wary
    48  	// that this will probably be in a terminal so split lines as you see
    49  	// necessary.
    50  	Description string
    51  
    52  	// Default will be the value returned if no data is entered.
    53  	Default string
    54  
    55  	// Hide will hide the text while it is being typed.
    56  	Hide bool
    57  
    58  	// EnvVars is a list of environment variables where the value can be looked
    59  	// up, in priority order. If any of these environment Variables are
    60  	// non-empty, they will be returned as the value for this input and the user
    61  	// will not be prompted.
    62  	EnvVars []string
    63  }
    64  
    65  // EnvVarValue reads the configured list of EnvVars, returns the first
    66  // non-empty value it finds, otherwise returns an empty string.
    67  func (o *InputOpts) EnvVarValue() string {
    68  	for _, envVar := range o.EnvVars {
    69  		if val := os.Getenv(envVar); val != "" {
    70  			return val
    71  		}
    72  	}
    73  	return ""
    74  }