github.com/xsb/terraform@v0.6.13-0.20160314145438-fe415c2f09d7/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 c.addModuleDepthFlag(cmdFlags, &moduleDepth) 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 planErr, stateErr error 40 var path string 41 var plan *terraform.Plan 42 var state *terraform.State 43 if len(args) > 0 { 44 path = args[0] 45 f, err := os.Open(path) 46 if err != nil { 47 c.Ui.Error(fmt.Sprintf("Error loading file: %s", err)) 48 return 1 49 } 50 defer f.Close() 51 52 plan, err = terraform.ReadPlan(f) 53 if err != nil { 54 if _, err := f.Seek(0, 0); err != nil { 55 c.Ui.Error(fmt.Sprintf("Error reading file: %s", err)) 56 return 1 57 } 58 59 plan = nil 60 planErr = err 61 } 62 if plan == nil { 63 state, err = terraform.ReadState(f) 64 if err != nil { 65 stateErr = err 66 } 67 } 68 } else { 69 stateOpts := c.StateOpts() 70 stateOpts.RemoteCacheOnly = true 71 result, err := State(stateOpts) 72 if err != nil { 73 c.Ui.Error(fmt.Sprintf("Error reading state: %s", err)) 74 return 1 75 } 76 state = result.State.State() 77 if state == nil { 78 c.Ui.Output("No state.") 79 return 0 80 } 81 } 82 83 if plan == nil && state == nil { 84 c.Ui.Error(fmt.Sprintf( 85 "Terraform couldn't read the given file as a state or plan file.\n"+ 86 "The errors while attempting to read the file as each format are\n"+ 87 "shown below.\n\n"+ 88 "State read error: %s\n\nPlan read error: %s", 89 stateErr, 90 planErr)) 91 return 1 92 } 93 94 if plan != nil { 95 c.Ui.Output(FormatPlan(&FormatPlanOpts{ 96 Plan: plan, 97 Color: c.Colorize(), 98 ModuleDepth: moduleDepth, 99 })) 100 return 0 101 } 102 103 c.Ui.Output(FormatState(&FormatStateOpts{ 104 State: state, 105 Color: c.Colorize(), 106 ModuleDepth: moduleDepth, 107 })) 108 return 0 109 } 110 111 func (c *ShowCommand) Help() string { 112 helpText := ` 113 Usage: terraform show [options] [path] 114 115 Reads and outputs a Terraform state or plan file in a human-readable 116 form. If no path is specified, the current state will be shown. 117 118 Options: 119 120 -module-depth=n Specifies the depth of modules to show in the output. 121 By default this is -1, which will expand all. 122 123 -no-color If specified, output won't contain any color. 124 125 ` 126 return strings.TrimSpace(helpText) 127 } 128 129 func (c *ShowCommand) Synopsis() string { 130 return "Inspect Terraform state or plan" 131 }