terraform/internal/command/output.go
Sarah French a83b7402c2
Some checks are pending
build / Determine intended Terraform version (push) Waiting to run
build / Determine Go toolchain version (push) Waiting to run
build / Generate release metadata (push) Blocked by required conditions
build / Build for freebsd_386 (push) Blocked by required conditions
build / Build for linux_386 (push) Blocked by required conditions
build / Build for openbsd_386 (push) Blocked by required conditions
build / Build for windows_386 (push) Blocked by required conditions
build / Build for darwin_amd64 (push) Blocked by required conditions
build / Build for freebsd_amd64 (push) Blocked by required conditions
build / Build for linux_amd64 (push) Blocked by required conditions
build / Build for openbsd_amd64 (push) Blocked by required conditions
build / Build for solaris_amd64 (push) Blocked by required conditions
build / Build for windows_amd64 (push) Blocked by required conditions
build / Build for freebsd_arm (push) Blocked by required conditions
build / Build for linux_arm (push) Blocked by required conditions
build / Build for darwin_arm64 (push) Blocked by required conditions
build / Build for linux_arm64 (push) Blocked by required conditions
build / Build for windows_arm64 (push) Blocked by required conditions
build / Build Docker image for linux_386 (push) Blocked by required conditions
build / Build Docker image for linux_amd64 (push) Blocked by required conditions
build / Build Docker image for linux_arm (push) Blocked by required conditions
build / Build Docker image for linux_arm64 (push) Blocked by required conditions
build / Build e2etest for linux_386 (push) Blocked by required conditions
build / Build e2etest for windows_386 (push) Blocked by required conditions
build / Build e2etest for darwin_amd64 (push) Blocked by required conditions
build / Build e2etest for linux_amd64 (push) Blocked by required conditions
build / Build e2etest for windows_amd64 (push) Blocked by required conditions
build / Build e2etest for linux_arm (push) Blocked by required conditions
build / Build e2etest for darwin_arm64 (push) Blocked by required conditions
build / Build e2etest for linux_arm64 (push) Blocked by required conditions
build / Run e2e test for linux_386 (push) Blocked by required conditions
build / Run e2e test for windows_386 (push) Blocked by required conditions
build / Run e2e test for darwin_amd64 (push) Blocked by required conditions
build / Run e2e test for linux_amd64 (push) Blocked by required conditions
build / Run e2e test for windows_amd64 (push) Blocked by required conditions
build / Run e2e test for linux_arm (push) Blocked by required conditions
build / Run e2e test for linux_arm64 (push) Blocked by required conditions
build / Run terraform-exec test for linux amd64 (push) Blocked by required conditions
Quick Checks / Unit Tests (push) Waiting to run
Quick Checks / Race Tests (push) Waiting to run
Quick Checks / End-to-end Tests (push) Waiting to run
Quick Checks / Code Consistency Checks (push) Waiting to run
PSS: Add tests showing state subcommands being used with PSS (#37891)
* test: Add E2E tests for `state list` and `state show` commands

* test: Update `mockPluggableStateStorageProvider` to log a warning during tests where the values in `MockStates` aren't compatible with the `ReadStateBytesFn` default function. Make existing test set an appropriate value in `MockStates`.

* test: Update `mockPluggableStateStorageProvider` helper to include a resource schema

* test: Add command-level test for `state list` showing integration with pluggable state storage code.

* test: Add command-level test for `state show` showing integration with pluggable state storage code.

* test: Add command-level test for `state pull` showing integration with pluggable state storage code.

* test: Add command-level test for `state identities` showing integration with pluggable state storage code.

* test: Add command-level test for `state rm` showing integration with pluggable state storage code.

* test: Add command-level test for `state mv` showing integration with pluggable state storage code.

* test: Add command-level test for `state push` showing integration with pluggable state storage code.

* test: Add command-level test for `state replace-provider` showing integration with pluggable state storage code.

* test: Change shared test fixture to not be named after a specific command under test.

This test fixure is reused across tests that need the config to define a state store but otherwise rely on the mock provider to set up the test scenario.

* test: Update test to use shared test fixture

* test: Remove redundant test fixture

The internal/command/testdata/state-commands-state-store and internal/command/testdata/state-store-unchanged test fixtures are the same.

* fix: Re-add logic for setting chunk size in the context of E2E tests using grpcwrap package

This was removed, incorrectly, in https://github.com/hashicorp/terraform/pull/37899

* refactor: Let panic happen if there's incompatibility between mock returned from `mockPluggableStateStorageProvider` and the `MockStates` that's been set in the mock

* test: Refactor to contain paths in reused variable, remove unnecessary .gitkeep

* test: Remove unneeded test code
2025-12-09 17:24:38 +00:00

132 lines
3.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package command
import (
"fmt"
"strings"
"github.com/hashicorp/terraform/internal/command/arguments"
"github.com/hashicorp/terraform/internal/command/views"
"github.com/hashicorp/terraform/internal/states"
"github.com/hashicorp/terraform/internal/tfdiags"
)
// OutputCommand is a Command implementation that reads an output
// from a Terraform state and prints it.
type OutputCommand struct {
Meta
}
func (c *OutputCommand) Run(rawArgs []string) int {
// Parse and apply global view arguments
common, rawArgs := arguments.ParseView(rawArgs)
c.View.Configure(common)
// Parse and validate flags
args, diags := arguments.ParseOutput(rawArgs)
if diags.HasErrors() {
c.View.Diagnostics(diags)
c.View.HelpPrompt("output")
return 1
}
view := views.NewOutput(args.ViewType, c.View)
// Fetch data from state
outputs, diags := c.Outputs(args.StatePath, args.ViewType)
if diags.HasErrors() {
view.Diagnostics(diags)
return 1
}
// Render the view
viewDiags := view.Output(args.Name, outputs)
diags = diags.Append(viewDiags)
view.Diagnostics(diags)
if diags.HasErrors() {
return 1
}
return 0
}
func (c *OutputCommand) Outputs(statePath string, view arguments.ViewType) (map[string]*states.OutputValue, tfdiags.Diagnostics) {
var diags tfdiags.Diagnostics
// Allow state path override
if statePath != "" {
c.Meta.statePath = statePath
}
// Load the backend
b, backendDiags := c.backend(".", view)
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
return nil, diags
}
// Command can be aborted by interruption signals
ctx, done := c.InterruptibleContext(c.CommandContext())
defer done()
// This is a read-only command
c.ignoreRemoteVersionConflict(b)
env, err := c.Workspace()
if err != nil {
diags = diags.Append(fmt.Errorf("Error selecting workspace: %s", err))
return nil, diags
}
// Get the state
sMgr, sDiags := b.StateMgr(env)
if sDiags.HasErrors() {
diags = diags.Append(fmt.Errorf("Failed to load state: %s", sDiags.Err()))
return nil, diags
}
output, err := sMgr.GetRootOutputValues(ctx)
if err != nil {
return nil, diags.Append(err)
}
return output, diags
}
func (c *OutputCommand) Help() string {
helpText := `
Usage: terraform [global options] output [options] [NAME]
Reads an output variable from a Terraform state file and prints
the value. With no additional arguments, output will display all
the outputs for the root module. If NAME is not specified, all
outputs are printed.
Options:
-state=path Path to the state file to read. Defaults to
"terraform.tfstate".
Legacy option for the local backend only. See
the local backend's documentation for more
information.
-no-color If specified, output won't contain any color.
-json If specified, machine readable output will be
printed in JSON format.
-raw For value types that can be automatically
converted to a string, will print the raw
string directly, rather than a human-oriented
representation of the value.
`
return strings.TrimSpace(helpText)
}
func (c *OutputCommand) Synopsis() string {
return "Show output values from your root module"
}