2023-05-02 11:33:06 -04:00
|
|
|
// Copyright IBM Corp. 2014, 2026
|
2023-08-10 18:43:27 -04:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-05-02 11:33:06 -04:00
|
|
|
|
2014-07-13 13:25:42 -04:00
|
|
|
package command
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
2018-03-27 18:31:05 -04:00
|
|
|
|
2021-05-17 15:07:38 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/command/arguments"
|
|
|
|
|
"github.com/hashicorp/terraform/internal/command/views"
|
2021-05-17 15:43:35 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/states"
|
2021-05-17 13:11:06 -04:00
|
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
2014-07-13 13:25:42 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// OutputCommand is a Command implementation that reads an output
|
|
|
|
|
// from a Terraform state and prints it.
|
|
|
|
|
type OutputCommand struct {
|
|
|
|
|
Meta
|
|
|
|
|
}
|
|
|
|
|
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
func (c *OutputCommand) Run(rawArgs []string) int {
|
2021-02-10 17:31:43 -05:00
|
|
|
// Parse and apply global view arguments
|
|
|
|
|
common, rawArgs := arguments.ParseView(rawArgs)
|
|
|
|
|
c.View.Configure(common)
|
2014-07-13 13:25:42 -04:00
|
|
|
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
// Parse and validate flags
|
|
|
|
|
args, diags := arguments.ParseOutput(rawArgs)
|
|
|
|
|
if diags.HasErrors() {
|
|
|
|
|
c.View.Diagnostics(diags)
|
|
|
|
|
c.View.HelpPrompt("output")
|
2014-07-13 13:25:42 -04:00
|
|
|
return 1
|
|
|
|
|
}
|
2015-06-25 19:20:12 -04:00
|
|
|
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
view := views.NewOutput(args.ViewType, c.View)
|
|
|
|
|
|
|
|
|
|
// Fetch data from state
|
2025-11-03 12:57:20 -05:00
|
|
|
outputs, diags := c.Outputs(args.StatePath, args.ViewType)
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
if diags.HasErrors() {
|
|
|
|
|
view.Diagnostics(diags)
|
2020-12-08 19:28:35 -05:00
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
// Render the view
|
|
|
|
|
viewDiags := view.Output(args.Name, outputs)
|
|
|
|
|
diags = diags.Append(viewDiags)
|
|
|
|
|
|
|
|
|
|
view.Diagnostics(diags)
|
|
|
|
|
|
|
|
|
|
if diags.HasErrors() {
|
2020-12-08 19:28:35 -05:00
|
|
|
return 1
|
|
|
|
|
}
|
|
|
|
|
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
return 0
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-03 12:57:20 -05:00
|
|
|
func (c *OutputCommand) Outputs(statePath string, view arguments.ViewType) (map[string]*states.OutputValue, tfdiags.Diagnostics) {
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
var diags tfdiags.Diagnostics
|
2014-07-13 13:25:42 -04:00
|
|
|
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
// Allow state path override
|
2019-04-12 07:37:27 -04:00
|
|
|
if statePath != "" {
|
|
|
|
|
c.Meta.statePath = statePath
|
|
|
|
|
}
|
|
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
// Load the backend
|
2025-11-03 12:57:20 -05:00
|
|
|
b, backendDiags := c.backend(".", view)
|
2018-03-27 18:31:05 -04:00
|
|
|
diags = diags.Append(backendDiags)
|
2025-11-03 12:57:20 -05:00
|
|
|
if backendDiags.HasErrors() {
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
return nil, diags
|
2017-01-18 23:50:45 -05:00
|
|
|
}
|
|
|
|
|
|
2024-05-06 13:02:01 -04:00
|
|
|
// Command can be aborted by interruption signals
|
|
|
|
|
ctx, done := c.InterruptibleContext(c.CommandContext())
|
|
|
|
|
defer done()
|
|
|
|
|
|
backend: Validate remote backend Terraform version
When using the enhanced remote backend, a subset of all Terraform
operations are supported. Of these, only plan and apply can be executed
on the remote infrastructure (e.g. Terraform Cloud). Other operations
run locally and use the remote backend for state storage.
This causes problems when the local version of Terraform does not match
the configured version from the remote workspace. If the two versions
are incompatible, an `import` or `state mv` operation can cause the
remote workspace to be unusable until a manual fix is applied.
To prevent this from happening accidentally, this commit introduces a
check that the local Terraform version and the configured remote
workspace Terraform version are compatible. This check is skipped for
commands which do not write state, and can also be disabled by the use
of a new command-line flag, `-ignore-remote-version`.
Terraform version compatibility is defined as:
- For all releases before 0.14.0, local must exactly equal remote, as
two different versions cannot share state;
- 0.14.0 to 1.0.x are compatible, as we will not change the state
version number until at least Terraform 1.1.0;
- Versions after 1.1.0 must have the same major and minor versions, as
we will not change the state version number in a patch release.
If the two versions are incompatible, a diagnostic is displayed,
advising that the error can be suppressed with `-ignore-remote-version`.
When this flag is used, the diagnostic is still displayed, but as a
warning instead of an error.
Commands which will not write state can assert this fact by calling the
helper `meta.ignoreRemoteBackendVersionConflict`, which will disable the
checks. Those which can write state should instead call the helper
`meta.remoteBackendVersionCheck`, which will return diagnostics for
display.
In addition to these explicit paths for managing the version check, we
have an implicit check in the remote backend's state manager
initialization method. Both of the above helpers will disable this
check. This fallback is in place to ensure that future code paths which
access state cannot accidentally skip the remote version check.
2020-11-13 16:43:56 -05:00
|
|
|
// This is a read-only command
|
2021-08-24 15:28:12 -04:00
|
|
|
c.ignoreRemoteVersionConflict(b)
|
backend: Validate remote backend Terraform version
When using the enhanced remote backend, a subset of all Terraform
operations are supported. Of these, only plan and apply can be executed
on the remote infrastructure (e.g. Terraform Cloud). Other operations
run locally and use the remote backend for state storage.
This causes problems when the local version of Terraform does not match
the configured version from the remote workspace. If the two versions
are incompatible, an `import` or `state mv` operation can cause the
remote workspace to be unusable until a manual fix is applied.
To prevent this from happening accidentally, this commit introduces a
check that the local Terraform version and the configured remote
workspace Terraform version are compatible. This check is skipped for
commands which do not write state, and can also be disabled by the use
of a new command-line flag, `-ignore-remote-version`.
Terraform version compatibility is defined as:
- For all releases before 0.14.0, local must exactly equal remote, as
two different versions cannot share state;
- 0.14.0 to 1.0.x are compatible, as we will not change the state
version number until at least Terraform 1.1.0;
- Versions after 1.1.0 must have the same major and minor versions, as
we will not change the state version number in a patch release.
If the two versions are incompatible, a diagnostic is displayed,
advising that the error can be suppressed with `-ignore-remote-version`.
When this flag is used, the diagnostic is still displayed, but as a
warning instead of an error.
Commands which will not write state can assert this fact by calling the
helper `meta.ignoreRemoteBackendVersionConflict`, which will disable the
checks. Those which can write state should instead call the helper
`meta.remoteBackendVersionCheck`, which will return diagnostics for
display.
In addition to these explicit paths for managing the version check, we
have an implicit check in the remote backend's state manager
initialization method. Both of the above helpers will disable this
check. This fallback is in place to ensure that future code paths which
access state cannot accidentally skip the remote version check.
2020-11-13 16:43:56 -05:00
|
|
|
|
2020-06-16 12:23:15 -04:00
|
|
|
env, err := c.Workspace()
|
|
|
|
|
if err != nil {
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
diags = diags.Append(fmt.Errorf("Error selecting workspace: %s", err))
|
|
|
|
|
return nil, diags
|
2020-06-16 12:23:15 -04:00
|
|
|
}
|
2017-02-28 13:13:03 -05:00
|
|
|
|
2017-01-18 23:50:45 -05:00
|
|
|
// Get the state
|
2025-12-09 12:24:38 -05:00
|
|
|
sMgr, sDiags := b.StateMgr(env)
|
2025-09-04 06:14:35 -04:00
|
|
|
if sDiags.HasErrors() {
|
|
|
|
|
diags = diags.Append(fmt.Errorf("Failed to load state: %s", sDiags.Err()))
|
cli: Add initial command views abstraction
Terraform supports multiple output formats for several sub-commands.
The default format is user-readable text, but many sub-commands support
a `-json` flag to output a machine-readable format for the result. The
output command also supports a `-raw` flag for a simpler, scripting-
focused machine readable format.
This commit adds a "views" abstraction, intended to help ensure
consistency between the various output formats. This extracts the render
specific code from the command package, and moves it into a views
package. Each command is expected to create an interface for its view,
and one or more implementations of that interface.
By doing so, we separate the concerns of generating the sub-command
result from rendering the result in the specified output format. This
should make it easier to ensure that all output formats will be updated
together when changes occur in the result-generating phase.
There are some other consequences of this restructuring:
- Views now directly access the terminal streams, rather than the
now-redundant cli.Ui instance;
- With the reorganization of commands, parsing CLI arguments is now the
responsibility of a separate "arguments" package.
For now, views are added only for the output sub-command, as an example.
Because this command uses code which is shared with the apply and
refresh commands, those are also partially updated.
2021-01-27 15:51:40 -05:00
|
|
|
return nil, diags
|
2014-07-13 13:25:42 -04:00
|
|
|
}
|
|
|
|
|
|
2025-12-09 12:24:38 -05:00
|
|
|
output, err := sMgr.GetRootOutputValues(ctx)
|
2022-03-17 00:47:06 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, diags.Append(err)
|
2018-11-16 20:24:06 -05:00
|
|
|
}
|
|
|
|
|
|
2024-10-03 13:10:30 -04:00
|
|
|
return output, diags
|
2014-07-13 13:25:42 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *OutputCommand) Help() string {
|
|
|
|
|
helpText := `
|
2021-02-22 09:25:56 -05:00
|
|
|
Usage: terraform [global options] output [options] [NAME]
|
2014-07-13 13:25:42 -04:00
|
|
|
|
|
|
|
|
Reads an output variable from a Terraform state file and prints
|
2016-10-31 07:34:56 -04:00
|
|
|
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.
|
2014-07-13 13:25:42 -04:00
|
|
|
|
|
|
|
|
Options:
|
|
|
|
|
|
|
|
|
|
-state=path Path to the state file to read. Defaults to
|
2025-08-14 04:57:43 -04:00
|
|
|
"terraform.tfstate".
|
|
|
|
|
Legacy option for the local backend only. See
|
|
|
|
|
the local backend's documentation for more
|
|
|
|
|
information.
|
2015-06-25 19:20:12 -04:00
|
|
|
|
|
|
|
|
-no-color If specified, output won't contain any color.
|
2014-07-13 13:25:42 -04:00
|
|
|
|
2016-07-13 12:38:19 -04:00
|
|
|
-json If specified, machine readable output will be
|
2020-12-08 19:28:35 -05:00
|
|
|
printed in JSON format.
|
2016-07-13 12:38:19 -04:00
|
|
|
|
2020-12-08 19:28:35 -05:00
|
|
|
-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.
|
2014-07-13 13:25:42 -04:00
|
|
|
`
|
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (c *OutputCommand) Synopsis() string {
|
2020-10-23 19:55:32 -04:00
|
|
|
return "Show output values from your root module"
|
2014-07-13 13:25:42 -04:00
|
|
|
}
|