github.com/nevins-b/terraform@v0.3.8-0.20170215184714-bbae22007d5a/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 // Get the state 78 stateStore, err := b.State() 79 if err != nil { 80 c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err)) 81 return 1 82 } 83 84 if err := stateStore.RefreshState(); err != nil { 85 c.Ui.Error(fmt.Sprintf("Failed to load state: %s", err)) 86 return 1 87 } 88 89 state = stateStore.State() 90 if state == nil { 91 c.Ui.Output("No state.") 92 return 0 93 } 94 } 95 96 if plan == nil && state == nil { 97 c.Ui.Error(fmt.Sprintf( 98 "Terraform couldn't read the given file as a state or plan file.\n"+ 99 "The errors while attempting to read the file as each format are\n"+ 100 "shown below.\n\n"+ 101 "State read error: %s\n\nPlan read error: %s", 102 stateErr, 103 planErr)) 104 return 1 105 } 106 107 if plan != nil { 108 c.Ui.Output(format.Plan(&format.PlanOpts{ 109 Plan: plan, 110 Color: c.Colorize(), 111 ModuleDepth: moduleDepth, 112 })) 113 return 0 114 } 115 116 c.Ui.Output(format.State(&format.StateOpts{ 117 State: state, 118 Color: c.Colorize(), 119 ModuleDepth: moduleDepth, 120 })) 121 return 0 122 } 123 124 func (c *ShowCommand) Help() string { 125 helpText := ` 126 Usage: terraform show [options] [path] 127 128 Reads and outputs a Terraform state or plan file in a human-readable 129 form. If no path is specified, the current state will be shown. 130 131 Options: 132 133 -module-depth=n Specifies the depth of modules to show in the output. 134 By default this is -1, which will expand all. 135 136 -no-color If specified, output won't contain any color. 137 138 ` 139 return strings.TrimSpace(helpText) 140 } 141 142 func (c *ShowCommand) Synopsis() string { 143 return "Inspect Terraform state or plan" 144 }