github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/command/output.go (about)

     1  package command
     2  
     3  import (
     4  	"fmt"
     5  	"strings"
     6  
     7  	"github.com/iaas-resource-provision/iaas-rpc/internal/command/arguments"
     8  	"github.com/iaas-resource-provision/iaas-rpc/internal/command/views"
     9  	"github.com/iaas-resource-provision/iaas-rpc/internal/states"
    10  	"github.com/iaas-resource-provision/iaas-rpc/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.ignoreRemoteBackendVersionConflict(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  	if err := stateStore.RefreshState(); err != nil {
    86  		diags = diags.Append(fmt.Errorf("Failed to load state: %s", err))
    87  		return nil, diags
    88  	}
    89  
    90  	state := stateStore.State()
    91  	if state == nil {
    92  		state = states.NewState()
    93  	}
    94  
    95  	return state.RootModule().OutputValues, nil
    96  }
    97  
    98  func (c *OutputCommand) Help() string {
    99  	helpText := `
   100  Usage: terraform [global options] output [options] [NAME]
   101  
   102    Reads an output variable from a Terraform state file and prints
   103    the value. With no additional arguments, output will display all
   104    the outputs for the root module.  If NAME is not specified, all
   105    outputs are printed.
   106  
   107  Options:
   108  
   109    -state=path      Path to the state file to read. Defaults to
   110                     "resource_state.json".
   111  
   112    -no-color        If specified, output won't contain any color.
   113  
   114    -json            If specified, machine readable output will be
   115                     printed in JSON format.
   116  
   117    -raw             For value types that can be automatically
   118                     converted to a string, will print the raw
   119                     string directly, rather than a human-oriented
   120                     representation of the value.
   121  `
   122  	return strings.TrimSpace(helpText)
   123  }
   124  
   125  func (c *OutputCommand) Synopsis() string {
   126  	return "Show output values from your root module"
   127  }