github.com/pulumi/terraform@v1.4.0/pkg/command/arguments/show.go (about)

     1  package arguments
     2  
     3  import (
     4  	"github.com/pulumi/terraform/pkg/tfdiags"
     5  )
     6  
     7  // Show represents the command-line arguments for the show command.
     8  type Show struct {
     9  	// Path is the path to the state file or plan file to be displayed. If
    10  	// unspecified, show will display the latest state snapshot.
    11  	Path string
    12  
    13  	// ViewType specifies which output format to use: human, JSON, or "raw".
    14  	ViewType ViewType
    15  }
    16  
    17  // ParseShow processes CLI arguments, returning a Show value and errors.
    18  // If errors are encountered, a Show value is still returned representing
    19  // the best effort interpretation of the arguments.
    20  func ParseShow(args []string) (*Show, tfdiags.Diagnostics) {
    21  	var diags tfdiags.Diagnostics
    22  	show := &Show{
    23  		Path: "",
    24  	}
    25  
    26  	var jsonOutput bool
    27  	cmdFlags := defaultFlagSet("show")
    28  	cmdFlags.BoolVar(&jsonOutput, "json", false, "json")
    29  
    30  	if err := cmdFlags.Parse(args); err != nil {
    31  		diags = diags.Append(tfdiags.Sourceless(
    32  			tfdiags.Error,
    33  			"Failed to parse command-line flags",
    34  			err.Error(),
    35  		))
    36  	}
    37  
    38  	args = cmdFlags.Args()
    39  	if len(args) > 1 {
    40  		diags = diags.Append(tfdiags.Sourceless(
    41  			tfdiags.Error,
    42  			"Too many command line arguments",
    43  			"Expected at most one positional argument.",
    44  		))
    45  	}
    46  
    47  	if len(args) > 0 {
    48  		show.Path = args[0]
    49  	}
    50  
    51  	switch {
    52  	case jsonOutput:
    53  		show.ViewType = ViewJSON
    54  	default:
    55  		show.ViewType = ViewHuman
    56  	}
    57  
    58  	return show, diags
    59  }