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