2024-02-08 04:48:59 -05:00
|
|
|
// Copyright (c) The OpenTofu Authors
|
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
// Copyright (c) 2023 HashiCorp, Inc.
|
2023-05-02 11:33:06 -04:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
|
2021-03-18 11:14:58 -04:00
|
|
|
package arguments
|
|
|
|
|
|
|
|
|
|
import (
|
2023-09-20 07:35:35 -04:00
|
|
|
"github.com/opentofu/opentofu/internal/tfdiags"
|
2021-03-18 11:14:58 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
2023-07-26 04:56:44 -04:00
|
|
|
// TestDirectory is the directory containing any test files that should be
|
|
|
|
|
// validated alongside the main configuration. Should be relative to the
|
|
|
|
|
// Path.
|
|
|
|
|
TestDirectory string
|
|
|
|
|
|
2023-09-21 08:38:46 -04:00
|
|
|
// NoTests indicates that OpenTofu should not validate any test files
|
2023-07-26 04:56:44 -04:00
|
|
|
// included with the module.
|
|
|
|
|
NoTests bool
|
|
|
|
|
|
2021-03-18 11:14:58 -04:00
|
|
|
// ViewType specifies which output format to use: human, JSON, or "raw".
|
|
|
|
|
ViewType ViewType
|
2024-07-11 10:16:20 -04:00
|
|
|
|
|
|
|
|
Vars *Vars
|
2021-03-18 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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: ".",
|
2024-07-11 10:16:20 -04:00
|
|
|
Vars: &Vars{},
|
2021-03-18 11:14:58 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var jsonOutput bool
|
2024-07-11 10:16:20 -04:00
|
|
|
cmdFlags := extendedFlagSet("validate", nil, nil, validate.Vars)
|
2021-03-18 11:14:58 -04:00
|
|
|
cmdFlags.BoolVar(&jsonOutput, "json", false, "json")
|
2023-07-26 04:56:44 -04:00
|
|
|
cmdFlags.StringVar(&validate.TestDirectory, "test-directory", "tests", "test-directory")
|
|
|
|
|
cmdFlags.BoolVar(&validate.NoTests, "no-tests", false, "no-tests")
|
2021-03-18 11:14:58 -04:00
|
|
|
|
|
|
|
|
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
|
|
|
|
|
}
|