github.com/bengesoff/terraform@v0.3.1-0.20141018223233-b25a53629922/command/show.go (about)

     1  package command
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"os"
     7  	"strings"
     8  
     9  	"github.com/hashicorp/terraform/terraform"
    10  )
    11  
    12  // ShowCommand is a Command implementation that reads and outputs the
    13  // contents of a Terraform plan or state file.
    14  type ShowCommand struct {
    15  	Meta
    16  }
    17  
    18  func (c *ShowCommand) Run(args []string) int {
    19  	var moduleDepth int
    20  
    21  	args = c.Meta.process(args, false)
    22  
    23  	cmdFlags := flag.NewFlagSet("show", flag.ContinueOnError)
    24  	cmdFlags.IntVar(&moduleDepth, "module-depth", 0, "module-depth")
    25  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    26  	if err := cmdFlags.Parse(args); err != nil {
    27  		return 1
    28  	}
    29  
    30  	args = cmdFlags.Args()
    31  	if len(args) > 1 {
    32  		c.Ui.Error(
    33  			"The show command expects at most one argument with the path\n" +
    34  				"to a Terraform state or plan file.\n")
    35  		cmdFlags.Usage()
    36  		return 1
    37  	}
    38  
    39  	var path string
    40  	if len(args) > 0 {
    41  		path = args[0]
    42  	} else {
    43  		// We should use the default state if it exists.
    44  		path = DefaultStateFilename
    45  		if _, err := os.Stat(DefaultStateFilename); err != nil {
    46  			if os.IsNotExist(err) {
    47  				c.Ui.Output("No state.")
    48  				return 0
    49  			}
    50  		}
    51  	}
    52  
    53  	var plan *terraform.Plan
    54  	var state *terraform.State
    55  
    56  	f, err := os.Open(path)
    57  	if err != nil {
    58  		c.Ui.Error(fmt.Sprintf("Error loading file: %s", err))
    59  		return 1
    60  	}
    61  
    62  	var planErr, stateErr error
    63  	plan, err = terraform.ReadPlan(f)
    64  	if err != nil {
    65  		if _, err := f.Seek(0, 0); err != nil {
    66  			c.Ui.Error(fmt.Sprintf("Error reading file: %s", err))
    67  			return 1
    68  		}
    69  
    70  		plan = nil
    71  		planErr = err
    72  	}
    73  	if plan == nil {
    74  		state, err = terraform.ReadState(f)
    75  		if err != nil {
    76  			stateErr = err
    77  		}
    78  	}
    79  	if plan == nil && state == nil {
    80  		c.Ui.Error(fmt.Sprintf(
    81  			"Terraform couldn't read the given file as a state or plan file.\n"+
    82  				"The errors while attempting to read the file as each format are\n"+
    83  				"shown below.\n\n"+
    84  				"State read error: %s\n\nPlan read error: %s",
    85  			stateErr,
    86  			planErr))
    87  		return 1
    88  	}
    89  
    90  	if plan != nil {
    91  		c.Ui.Output(FormatPlan(&FormatPlanOpts{
    92  			Plan:        plan,
    93  			Color:       c.Colorize(),
    94  			ModuleDepth: moduleDepth,
    95  		}))
    96  		return 0
    97  	}
    98  
    99  	c.Ui.Output(FormatState(&FormatStateOpts{
   100  		State:       state,
   101  		Color:       c.Colorize(),
   102  		ModuleDepth: moduleDepth,
   103  	}))
   104  	return 0
   105  }
   106  
   107  func (c *ShowCommand) Help() string {
   108  	helpText := `
   109  Usage: terraform show [options] [path]
   110  
   111    Reads and outputs a Terraform state or plan file in a human-readable
   112    form. If no path is specified, the current state will be shown.
   113  
   114  Options:
   115  
   116    -module-depth=n     Specifies the depth of modules to show in the output.
   117                        By default this is zero. -1 will expand all.
   118  
   119    -no-color           If specified, output won't contain any color.
   120  
   121  `
   122  	return strings.TrimSpace(helpText)
   123  }
   124  
   125  func (c *ShowCommand) Synopsis() string {
   126  	return "Inspect Terraform state or plan"
   127  }