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