github.com/terramate-io/tf@v0.0.0-20230830114523-fce866b4dfcd/command/output.go (about)

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