github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/command/show.go (about) 1 package command 2 3 import ( 4 "flag" 5 "fmt" 6 "os" 7 "strings" 8 9 statelib "github.com/hashicorp/terraform/state" 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 cmdFlags.IntVar(&moduleDepth, "module-depth", 0, "module-depth") 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 70 } else { 71 // We should use the default state if it exists. 72 stateStore := &statelib.LocalState{Path: DefaultStateFilename} 73 if err := stateStore.RefreshState(); err != nil { 74 c.Ui.Error(fmt.Sprintf("Error reading state: %s", err)) 75 return 1 76 } 77 78 state = stateStore.State() 79 if state == nil { 80 c.Ui.Output("No state.") 81 return 0 82 } 83 } 84 85 if plan == nil && state == nil { 86 c.Ui.Error(fmt.Sprintf( 87 "Terraform couldn't read the given file as a state or plan file.\n"+ 88 "The errors while attempting to read the file as each format are\n"+ 89 "shown below.\n\n"+ 90 "State read error: %s\n\nPlan read error: %s", 91 stateErr, 92 planErr)) 93 return 1 94 } 95 96 if plan != nil { 97 c.Ui.Output(FormatPlan(&FormatPlanOpts{ 98 Plan: plan, 99 Color: c.Colorize(), 100 ModuleDepth: moduleDepth, 101 })) 102 return 0 103 } 104 105 c.Ui.Output(FormatState(&FormatStateOpts{ 106 State: state, 107 Color: c.Colorize(), 108 ModuleDepth: moduleDepth, 109 })) 110 return 0 111 } 112 113 func (c *ShowCommand) Help() string { 114 helpText := ` 115 Usage: terraform show [options] [path] 116 117 Reads and outputs a Terraform state or plan file in a human-readable 118 form. If no path is specified, the current state will be shown. 119 120 Options: 121 122 -module-depth=n Specifies the depth of modules to show in the output. 123 By default this is zero. -1 will expand all. 124 125 -no-color If specified, output won't contain any color. 126 127 ` 128 return strings.TrimSpace(helpText) 129 } 130 131 func (c *ShowCommand) Synopsis() string { 132 return "Inspect Terraform state or plan" 133 }