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

     1  package arguments
     2  
     3  import (
     4  	"github.com/hashicorp/terraform/internal/tfdiags"
     5  )
     6  
     7  // Validate represents the command-line arguments for the validate command.
     8  type Validate struct {
     9  	// Path is the directory containing the configuration to be validated. If
    10  	// unspecified, validate will use the current directory.
    11  	Path string
    12  
    13  	// ViewType specifies which output format to use: human, JSON, or "raw".
    14  	ViewType ViewType
    15  }
    16  
    17  // ParseValidate processes CLI arguments, returning a Validate value and errors.
    18  // If errors are encountered, a Validate value is still returned representing
    19  // the best effort interpretation of the arguments.
    20  func ParseValidate(args []string) (*Validate, tfdiags.Diagnostics) {
    21  	var diags tfdiags.Diagnostics
    22  	validate := &Validate{
    23  		Path: ".",
    24  	}
    25  
    26  	var jsonOutput bool
    27  	cmdFlags := defaultFlagSet("validate")
    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  		validate.Path = args[0]
    49  	}
    50  
    51  	switch {
    52  	case jsonOutput:
    53  		validate.ViewType = ViewJSON
    54  	default:
    55  		validate.ViewType = ViewHuman
    56  	}
    57  
    58  	return validate, diags
    59  }