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

     1  package arguments
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/internal/tfdiags"
     5  )
     6  
     7  // Plan represents the command-line arguments for the plan command.
     8  type Plan struct {
     9  	// State, Operation, and Vars are the common extended flags
    10  	State     *State
    11  	Operation *Operation
    12  	Vars      *Vars
    13  
    14  	// DetailedExitCode enables different exit codes for error, success with
    15  	// changes, and success with no changes.
    16  	DetailedExitCode bool
    17  
    18  	// InputEnabled is used to disable interactive input for unspecified
    19  	// variable and backend config values. Default is true.
    20  	InputEnabled bool
    21  
    22  	// OutPath contains an optional path to store the plan file
    23  	OutPath string
    24  
    25  	// ViewType specifies which output format to use
    26  	ViewType ViewType
    27  }
    28  
    29  // ParsePlan processes CLI arguments, returning a Plan value and errors.
    30  // If errors are encountered, a Plan value is still returned representing
    31  // the best effort interpretation of the arguments.
    32  func ParsePlan(args []string) (*Plan, tfdiags.Diagnostics) {
    33  	var diags tfdiags.Diagnostics
    34  	plan := &Plan{
    35  		State:     &State{},
    36  		Operation: &Operation{},
    37  		Vars:      &Vars{},
    38  	}
    39  
    40  	cmdFlags := extendedFlagSet("plan", plan.State, plan.Operation, plan.Vars)
    41  	cmdFlags.BoolVar(&plan.DetailedExitCode, "detailed-exitcode", false, "detailed-exitcode")
    42  	cmdFlags.BoolVar(&plan.InputEnabled, "input", true, "input")
    43  	cmdFlags.StringVar(&plan.OutPath, "out", "", "out")
    44  
    45  	var json bool
    46  	cmdFlags.BoolVar(&json, "json", false, "json")
    47  
    48  	if err := cmdFlags.Parse(args); err != nil {
    49  		diags = diags.Append(tfdiags.Sourceless(
    50  			tfdiags.Error,
    51  			"Failed to parse command-line flags",
    52  			err.Error(),
    53  		))
    54  	}
    55  
    56  	args = cmdFlags.Args()
    57  
    58  	if len(args) > 0 {
    59  		diags = diags.Append(tfdiags.Sourceless(
    60  			tfdiags.Error,
    61  			"Too many command line arguments",
    62  			"To specify a working directory for the plan, use the global -chdir flag.",
    63  		))
    64  	}
    65  
    66  	diags = diags.Append(plan.Operation.Parse())
    67  
    68  	// JSON view currently does not support input, so we disable it here
    69  	if json {
    70  		plan.InputEnabled = false
    71  	}
    72  
    73  	switch {
    74  	case json:
    75  		plan.ViewType = ViewJSON
    76  	default:
    77  		plan.ViewType = ViewHuman
    78  	}
    79  
    80  	return plan, diags
    81  }