github.com/richardmarshall/terraform@v0.9.5-0.20170429023105-15704cc6ee35/command/show.go (about)

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