github.com/paultyng/terraform@v0.6.11-0.20180227224804-66ff8f8bed40/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  		dispPlan := format.NewPlan(plan)
   114  		c.Ui.Output(dispPlan.Format(c.Colorize()))
   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  }