github.com/franklinhu/terraform@v0.6.9-0.20151202232446-81f7fb1e6f9e/command/output.go (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"sort"
     7  	"strings"
     8  )
     9  
    10  // OutputCommand is a Command implementation that reads an output
    11  // from a Terraform state and prints it.
    12  type OutputCommand struct {
    13  	Meta
    14  }
    15  
    16  func (c *OutputCommand) Run(args []string) int {
    17  	args = c.Meta.process(args, false)
    18  
    19  	var module string
    20  	cmdFlags := flag.NewFlagSet("output", flag.ContinueOnError)
    21  	cmdFlags.StringVar(&c.Meta.statePath, "state", DefaultStateFilename, "path")
    22  	cmdFlags.StringVar(&module, "module", "", "module")
    23  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    24  
    25  	if err := cmdFlags.Parse(args); err != nil {
    26  		return 1
    27  	}
    28  
    29  	args = cmdFlags.Args()
    30  	if len(args) > 1 {
    31  		c.Ui.Error(
    32  			"The output command expects exactly one argument with the name\n" +
    33  				"of an output variable or no arguments to show all outputs.\n")
    34  		cmdFlags.Usage()
    35  		return 1
    36  	}
    37  
    38  	name := ""
    39  	if len(args) > 0 {
    40  		name = args[0]
    41  	}
    42  
    43  	stateStore, err := c.Meta.State()
    44  	if err != nil {
    45  		c.Ui.Error(fmt.Sprintf("Error reading state: %s", err))
    46  		return 1
    47  	}
    48  
    49  	if module == "" {
    50  		module = "root"
    51  	} else {
    52  		module = "root." + module
    53  	}
    54  
    55  	// Get the proper module we want to get outputs for
    56  	modPath := strings.Split(module, ".")
    57  
    58  	state := stateStore.State()
    59  	mod := state.ModuleByPath(modPath)
    60  
    61  	if mod == nil {
    62  		c.Ui.Error(fmt.Sprintf(
    63  			"The module %s could not be found. There is nothing to output.",
    64  			module))
    65  		return 1
    66  	}
    67  
    68  	if state.Empty() || len(mod.Outputs) == 0 {
    69  		c.Ui.Error(fmt.Sprintf(
    70  			"The state file has no outputs defined. Define an output\n" +
    71  				"in your configuration with the `output` directive and re-run\n" +
    72  				"`terraform apply` for it to become available."))
    73  		return 1
    74  	}
    75  
    76  	if name == "" {
    77  		ks := make([]string, 0, len(mod.Outputs))
    78  		for k, _ := range mod.Outputs {
    79  			ks = append(ks, k)
    80  		}
    81  		sort.Strings(ks)
    82  
    83  		for _, k := range ks {
    84  			v := mod.Outputs[k]
    85  			c.Ui.Output(fmt.Sprintf("%s = %s", k, v))
    86  		}
    87  		return 0
    88  	}
    89  
    90  	v, ok := mod.Outputs[name]
    91  	if !ok {
    92  		c.Ui.Error(fmt.Sprintf(
    93  			"The output variable requested could not be found in the state\n" +
    94  				"file. If you recently added this to your configuration, be\n" +
    95  				"sure to run `terraform apply`, since the state won't be updated\n" +
    96  				"with new output variables until that command is run."))
    97  		return 1
    98  	}
    99  
   100  	c.Ui.Output(v)
   101  	return 0
   102  }
   103  
   104  func (c *OutputCommand) Help() string {
   105  	helpText := `
   106  Usage: terraform output [options] [NAME]
   107  
   108    Reads an output variable from a Terraform state file and prints
   109    the value.  If NAME is not specified, all outputs are printed.
   110  
   111  Options:
   112  
   113    -state=path      Path to the state file to read. Defaults to
   114                     "terraform.tfstate".
   115  
   116    -no-color        If specified, output won't contain any color.
   117  
   118    -module=name     If specified, returns the outputs for a
   119                     specific module
   120  
   121  `
   122  	return strings.TrimSpace(helpText)
   123  }
   124  
   125  func (c *OutputCommand) Synopsis() string {
   126  	return "Read an output from a state file"
   127  }