github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/arguments/output.go (about)

     1  package arguments
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/internal/tfdiags"
     5  )
     6  
     7  // Output represents the command-line arguments for the output command.
     8  type Output struct {
     9  	// Name identifies which root module output to show.  If empty, show all
    10  	// outputs.
    11  	Name string
    12  
    13  	// StatePath is an optional path to a state file, from which outputs will
    14  	// be loaded.
    15  	StatePath string
    16  
    17  	// ViewType specifies which output format to use: human, JSON, or "raw".
    18  	ViewType ViewType
    19  }
    20  
    21  // ParseOutput processes CLI arguments, returning an Output value and errors.
    22  // If errors are encountered, an Output value is still returned representing
    23  // the best effort interpretation of the arguments.
    24  func ParseOutput(args []string) (*Output, tfdiags.Diagnostics) {
    25  	var diags tfdiags.Diagnostics
    26  	output := &Output{}
    27  
    28  	var jsonOutput, rawOutput bool
    29  	var statePath string
    30  	cmdFlags := defaultFlagSet("output")
    31  	cmdFlags.BoolVar(&jsonOutput, "json", false, "json")
    32  	cmdFlags.BoolVar(&rawOutput, "raw", false, "raw")
    33  	cmdFlags.StringVar(&statePath, "state", "", "path")
    34  
    35  	if err := cmdFlags.Parse(args); err != nil {
    36  		diags = diags.Append(tfdiags.Sourceless(
    37  			tfdiags.Error,
    38  			"Failed to parse command-line flags",
    39  			err.Error(),
    40  		))
    41  	}
    42  
    43  	args = cmdFlags.Args()
    44  	if len(args) > 1 {
    45  		diags = diags.Append(tfdiags.Sourceless(
    46  			tfdiags.Error,
    47  			"Unexpected argument",
    48  			"The output command expects exactly one argument with the name of an output variable or no arguments to show all outputs.",
    49  		))
    50  	}
    51  
    52  	if jsonOutput && rawOutput {
    53  		diags = diags.Append(tfdiags.Sourceless(
    54  			tfdiags.Error,
    55  			"Invalid output format",
    56  			"The -raw and -json options are mutually-exclusive.",
    57  		))
    58  
    59  		// Since the desired output format is unknowable, fall back to default
    60  		jsonOutput = false
    61  		rawOutput = false
    62  	}
    63  
    64  	output.StatePath = statePath
    65  
    66  	if len(args) > 0 {
    67  		output.Name = args[0]
    68  	}
    69  
    70  	if rawOutput && output.Name == "" {
    71  		diags = diags.Append(tfdiags.Sourceless(
    72  			tfdiags.Error,
    73  			"Output name required",
    74  			"You must give the name of a single output value when using the -raw option.",
    75  		))
    76  	}
    77  
    78  	switch {
    79  	case jsonOutput:
    80  		output.ViewType = ViewJSON
    81  	case rawOutput:
    82  		output.ViewType = ViewRaw
    83  	default:
    84  		output.ViewType = ViewHuman
    85  	}
    86  
    87  	return output, diags
    88  }