opentofu/internal/command/arguments/console.go
Andrei Ciobanu 866b067c0d
Some checks are pending
build / Build for freebsd_386 (push) Waiting to run
build / Build for linux_386 (push) Waiting to run
build / Build for openbsd_386 (push) Waiting to run
build / Build for windows_386 (push) Waiting to run
build / Build for freebsd_amd64 (push) Waiting to run
build / Build for linux_amd64 (push) Waiting to run
build / Build for openbsd_amd64 (push) Waiting to run
build / Build for solaris_amd64 (push) Waiting to run
build / Build for windows_amd64 (push) Waiting to run
build / Build for freebsd_arm (push) Waiting to run
build / Build for linux_arm (push) Waiting to run
build / Build for linux_arm64 (push) Waiting to run
build / Build for darwin_amd64 (push) Waiting to run
build / Build for darwin_arm64 (push) Waiting to run
build / End-to-end Tests for linux_386 (push) Waiting to run
build / End-to-end Tests for windows_386 (push) Waiting to run
build / End-to-end Tests for darwin_amd64 (push) Waiting to run
build / End-to-end Tests for linux_amd64 (push) Waiting to run
build / End-to-end Tests for windows_amd64 (push) Waiting to run
Quick Checks / List files changed for pull request (push) Waiting to run
Quick Checks / Unit tests for linux_386 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_amd64 (push) Blocked by required conditions
Quick Checks / Unit tests for windows_amd64 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_arm (push) Blocked by required conditions
Quick Checks / Unit tests for darwin_arm64 (push) Blocked by required conditions
Quick Checks / Unit tests for linux_arm64 (push) Blocked by required conditions
Quick Checks / Race Tests (push) Blocked by required conditions
Quick Checks / End-to-end Tests (push) Blocked by required conditions
Quick Checks / Code Consistency Checks (push) Blocked by required conditions
Quick Checks / License Checks (push) Waiting to run
Website checks / List files changed for pull request (push) Waiting to run
Website checks / Build (push) Blocked by required conditions
Website checks / Test Installation Instructions (push) Blocked by required conditions
Unify patterns across the refactored commands (#3941)
Signed-off-by: Andrei Ciobanu <andrei.ciobanu@opentofu.org>
2026-03-26 17:19:27 +02:00

73 lines
2.3 KiB
Go

// Copyright (c) The OpenTofu Authors
// SPDX-License-Identifier: MPL-2.0
// Copyright (c) 2023 HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package arguments
import (
"github.com/opentofu/opentofu/internal/tfdiags"
)
// Console represents the command-line arguments for the console command.
type Console struct {
// StatePath is the path to the state file to use for the console session.
StatePath string
// ViewOptions specifies which view options to use
ViewOptions ViewOptions
// Vars holds and provides information for the flags related to variables that a user can give into the process
Vars *Vars
// Backend is used here to register and parse the flags for state locking
Backend Backend
}
// ParseConsole processes CLI arguments, returning a Console value, a closer function, and errors.
// If errors are encountered, a Console value is still returned representing
// the best effort interpretation of the arguments.
func ParseConsole(args []string) (*Console, func(), tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
console := &Console{
Vars: &Vars{},
}
cmdFlags := extendedFlagSet("console", nil, nil, console.Vars)
console.Backend.AddStateFlags(cmdFlags)
cmdFlags.StringVar(&console.StatePath, "state", DefaultStateFilename, "path")
console.ViewOptions.AddFlags(cmdFlags, true)
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) > 0 {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Too many command line arguments",
"Expected at most one positional argument.",
))
}
closer, moreDiags := console.ViewOptions.Parse()
diags = diags.Append(moreDiags)
// If the user provided the -json flag, we don't allow it since the UX is just poor in this case.
// We allow only the streaming of the evaluated values in a json file, by using the `-json-into` flag.
if console.ViewOptions.ViewType == ViewJSON {
diags = diags.Append(tfdiags.Sourceless(
tfdiags.Error,
"Output only in json is not allowed",
"In case you want to stream the output of the console into json, use the \"-json-into\" instead.",
))
// Revert the view type to be able to print the diagnostic properly
console.ViewOptions.ViewType = ViewHuman
}
return console, closer, diags
}