Files
opentf/internal/command/workspace_list.go
2026-03-03 12:44:56 +02:00

164 lines
4.9 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 command
import (
"fmt"
"strings"
"github.com/opentofu/opentofu/internal/command/arguments"
"github.com/opentofu/opentofu/internal/command/flags"
"github.com/opentofu/opentofu/internal/command/views"
"github.com/posener/complete"
"github.com/opentofu/opentofu/internal/tfdiags"
)
type WorkspaceListCommand struct {
Meta
LegacyName bool
}
func (c *WorkspaceListCommand) Run(rawArgs []string) int {
ctx := c.CommandContext()
common, rawArgs := arguments.ParseView(rawArgs)
c.View.Configure(common)
// Because the legacy UI was using println to show diagnostics and the new view is using, by default, print,
// in order to keep functional parity, we setup the view to add a new line after each diagnostic.
c.View.DiagsWithNewline()
// Propagate -no-color for legacy use of Ui. The remote backend and
// cloud package use this; it should be removed when/if they are
// migrated to views.
c.Meta.color = !common.NoColor
c.Meta.Color = c.Meta.color
// Parse and validate flags
args, closer, diags := arguments.ParseWorkspaceList(rawArgs)
defer closer()
// Instantiate the view, even if there are flag errors, so that we render
// diagnostics according to the desired view
view := views.NewWorkspace(args.ViewOptions, c.View)
// ... and initialise the Meta.Ui to wrap Meta.View into a new implementation
// that is able to print by using View abstraction and use the Meta.Ui
// to ask for the user input.
c.Meta.configureUiFromView(args.ViewOptions)
if diags.HasErrors() {
view.Diagnostics(diags)
return 1
}
c.GatherVariables(args.Vars)
view.WarnWhenUsedAsEnvCmd(c.LegacyName)
configPath := c.WorkingDir.NormalizePath(c.WorkingDir.RootModuleDir())
// Load the encryption configuration
enc, encDiags := c.EncryptionFromPath(ctx, configPath)
if encDiags.HasErrors() {
view.Diagnostics(encDiags)
return 1
}
backendConfig, backendDiags := c.loadBackendConfig(ctx, configPath)
diags = diags.Append(backendDiags)
if diags.HasErrors() {
view.Diagnostics(diags)
return 1
}
// Load the backend
b, backendDiags := c.Backend(ctx, &BackendOpts{
Config: backendConfig,
}, enc.State())
diags = diags.Append(backendDiags)
if backendDiags.HasErrors() {
view.Diagnostics(diags)
return 1
}
// This command will not write state
c.ignoreRemoteVersionConflict(b)
workspaces, err := b.Workspaces(ctx)
if err != nil {
view.Diagnostics(tfdiags.Diagnostics{tfdiags.Sourceless(
tfdiags.Error,
"Error loading workspaces",
fmt.Sprintf("Listing workspaces failed: %s", err),
)})
return 1
}
env, isOverridden := c.WorkspaceOverridden(ctx)
view.ListWorkspaces(workspaces, env)
if isOverridden {
view.WorkspaceOverwrittenByEnvVarWarn()
}
return 0
}
func (c *WorkspaceListCommand) AutocompleteArgs() complete.Predictor {
return complete.PredictDirs("")
}
func (c *WorkspaceListCommand) AutocompleteFlags() complete.Flags {
return nil
}
func (c *WorkspaceListCommand) Help() string {
helpText := `
Usage: tofu [global options] workspace list [options]
List OpenTofu workspaces.
Options:
-var 'foo=bar' Set a value for one of the input variables in the root
module of the configuration. Use this option more than
once to set more than one variable.
-var-file=filename Load variable values from the given file, in addition
to the default files terraform.tfvars and *.auto.tfvars.
Use this option more than once to include more than one
variables file.
-json The output of the command is printed in json format.
-json-into=out.json Produce the same output as -json, but sent directly
to the given file. This allows automation to preserve
the original human-readable output streams, while
capturing more detailed logs for machine analysis.
`
return strings.TrimSpace(helpText)
}
func (c *WorkspaceListCommand) Synopsis() string {
return "List Workspaces"
}
// TODO meta-refactor: move this to arguments once all commands are using the same shim logic
func (c *WorkspaceListCommand) GatherVariables(args *arguments.Vars) {
// FIXME the arguments package currently trivially gathers variable related
// arguments in a heterogeneous slice, in order to minimize the number of
// code paths gathering variables during the transition to this structure.
// Once all commands that gather variables have been converted to this
// structure, we could move the variable gathering code to the arguments
// package directly, removing this shim layer.
varArgs := args.All()
items := make([]flags.RawFlag, len(varArgs))
for i := range varArgs {
items[i].Name = varArgs[i].Name
items[i].Value = varArgs[i].Value
}
c.Meta.variableArgs = flags.RawFlags{Items: &items}
}