github.com/opentofu/opentofu@v1.7.1/internal/command/arguments/validate.go (about) 1 // Copyright (c) The OpenTofu Authors 2 // SPDX-License-Identifier: MPL-2.0 3 // Copyright (c) 2023 HashiCorp, Inc. 4 // SPDX-License-Identifier: MPL-2.0 5 6 package arguments 7 8 import ( 9 "github.com/opentofu/opentofu/internal/tfdiags" 10 ) 11 12 // Validate represents the command-line arguments for the validate command. 13 type Validate struct { 14 // Path is the directory containing the configuration to be validated. If 15 // unspecified, validate will use the current directory. 16 Path string 17 18 // TestDirectory is the directory containing any test files that should be 19 // validated alongside the main configuration. Should be relative to the 20 // Path. 21 TestDirectory string 22 23 // NoTests indicates that OpenTofu should not validate any test files 24 // included with the module. 25 NoTests bool 26 27 // ViewType specifies which output format to use: human, JSON, or "raw". 28 ViewType ViewType 29 } 30 31 // ParseValidate processes CLI arguments, returning a Validate value and errors. 32 // If errors are encountered, a Validate value is still returned representing 33 // the best effort interpretation of the arguments. 34 func ParseValidate(args []string) (*Validate, tfdiags.Diagnostics) { 35 var diags tfdiags.Diagnostics 36 validate := &Validate{ 37 Path: ".", 38 } 39 40 var jsonOutput bool 41 cmdFlags := defaultFlagSet("validate") 42 cmdFlags.BoolVar(&jsonOutput, "json", false, "json") 43 cmdFlags.StringVar(&validate.TestDirectory, "test-directory", "tests", "test-directory") 44 cmdFlags.BoolVar(&validate.NoTests, "no-tests", false, "no-tests") 45 46 if err := cmdFlags.Parse(args); err != nil { 47 diags = diags.Append(tfdiags.Sourceless( 48 tfdiags.Error, 49 "Failed to parse command-line flags", 50 err.Error(), 51 )) 52 } 53 54 args = cmdFlags.Args() 55 if len(args) > 1 { 56 diags = diags.Append(tfdiags.Sourceless( 57 tfdiags.Error, 58 "Too many command line arguments", 59 "Expected at most one positional argument.", 60 )) 61 } 62 63 if len(args) > 0 { 64 validate.Path = args[0] 65 } 66 67 switch { 68 case jsonOutput: 69 validate.ViewType = ViewJSON 70 default: 71 validate.ViewType = ViewHuman 72 } 73 74 return validate, diags 75 }