github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/output.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/terraform/internal/command/arguments" 8 "github.com/hashicorp/terraform/internal/command/views" 9 "github.com/hashicorp/terraform/internal/states" 10 "github.com/hashicorp/terraform/internal/tfdiags" 11 ) 12 13 // OutputCommand is a Command implementation that reads an output 14 // from a Terraform state and prints it. 15 type OutputCommand struct { 16 Meta 17 } 18 19 func (c *OutputCommand) Run(rawArgs []string) int { 20 // Parse and apply global view arguments 21 common, rawArgs := arguments.ParseView(rawArgs) 22 c.View.Configure(common) 23 24 // Parse and validate flags 25 args, diags := arguments.ParseOutput(rawArgs) 26 if diags.HasErrors() { 27 c.View.Diagnostics(diags) 28 c.View.HelpPrompt("output") 29 return 1 30 } 31 32 view := views.NewOutput(args.ViewType, c.View) 33 34 // Fetch data from state 35 outputs, diags := c.Outputs(args.StatePath) 36 if diags.HasErrors() { 37 view.Diagnostics(diags) 38 return 1 39 } 40 41 // Render the view 42 viewDiags := view.Output(args.Name, outputs) 43 diags = diags.Append(viewDiags) 44 45 view.Diagnostics(diags) 46 47 if diags.HasErrors() { 48 return 1 49 } 50 51 return 0 52 } 53 54 func (c *OutputCommand) Outputs(statePath string) (map[string]*states.OutputValue, tfdiags.Diagnostics) { 55 var diags tfdiags.Diagnostics 56 57 // Allow state path override 58 if statePath != "" { 59 c.Meta.statePath = statePath 60 } 61 62 // Load the backend 63 b, backendDiags := c.Backend(nil) 64 diags = diags.Append(backendDiags) 65 if diags.HasErrors() { 66 return nil, diags 67 } 68 69 // This is a read-only command 70 c.ignoreRemoteVersionConflict(b) 71 72 env, err := c.Workspace() 73 if err != nil { 74 diags = diags.Append(fmt.Errorf("Error selecting workspace: %s", err)) 75 return nil, diags 76 } 77 78 // Get the state 79 stateStore, err := b.StateMgr(env) 80 if err != nil { 81 diags = diags.Append(fmt.Errorf("Failed to load state: %s", err)) 82 return nil, diags 83 } 84 85 output, err := stateStore.GetRootOutputValues() 86 if err != nil { 87 return nil, diags.Append(err) 88 } 89 90 return output, diags 91 } 92 93 func (c *OutputCommand) Help() string { 94 helpText := ` 95 Usage: terraform [global options] output [options] [NAME] 96 97 Reads an output variable from a Terraform state file and prints 98 the value. With no additional arguments, output will display all 99 the outputs for the root module. If NAME is not specified, all 100 outputs are printed. 101 102 Options: 103 104 -state=path Path to the state file to read. Defaults to 105 "terraform.tfstate". Ignored when remote 106 state is used. 107 108 -no-color If specified, output won't contain any color. 109 110 -json If specified, machine readable output will be 111 printed in JSON format. 112 113 -raw For value types that can be automatically 114 converted to a string, will print the raw 115 string directly, rather than a human-oriented 116 representation of the value. 117 ` 118 return strings.TrimSpace(helpText) 119 } 120 121 func (c *OutputCommand) Synopsis() string { 122 return "Show output values from your root module" 123 }