mirror of
https://github.com/hashicorp/terraform.git
synced 2026-05-28 04:03:27 -04:00
Some checks failed
build / Determine intended Terraform version (push) Has been cancelled
build / Determine Go toolchain version (push) Has been cancelled
Quick Checks / Unit Tests (push) Has been cancelled
Quick Checks / Race Tests (push) Has been cancelled
Quick Checks / End-to-end Tests (push) Has been cancelled
Quick Checks / Code Consistency Checks (push) Has been cancelled
build / Generate release metadata (push) Has been cancelled
build / Build for freebsd_386 (push) Has been cancelled
build / Build for linux_386 (push) Has been cancelled
build / Build for openbsd_386 (push) Has been cancelled
build / Build for windows_386 (push) Has been cancelled
build / Build for darwin_amd64 (push) Has been cancelled
build / Build for freebsd_amd64 (push) Has been cancelled
build / Build for linux_amd64 (push) Has been cancelled
build / Build for openbsd_amd64 (push) Has been cancelled
build / Build for solaris_amd64 (push) Has been cancelled
build / Build for windows_amd64 (push) Has been cancelled
build / Build for freebsd_arm (push) Has been cancelled
build / Build for linux_arm (push) Has been cancelled
build / Build for darwin_arm64 (push) Has been cancelled
build / Build for linux_arm64 (push) Has been cancelled
build / Build for windows_arm64 (push) Has been cancelled
build / Build Docker image for linux_386 (push) Has been cancelled
build / Build Docker image for linux_amd64 (push) Has been cancelled
build / Build Docker image for linux_arm (push) Has been cancelled
build / Build Docker image for linux_arm64 (push) Has been cancelled
build / Build e2etest for linux_386 (push) Has been cancelled
build / Build e2etest for windows_386 (push) Has been cancelled
build / Build e2etest for darwin_amd64 (push) Has been cancelled
build / Build e2etest for linux_amd64 (push) Has been cancelled
build / Build e2etest for windows_amd64 (push) Has been cancelled
build / Build e2etest for linux_arm (push) Has been cancelled
build / Build e2etest for darwin_arm64 (push) Has been cancelled
build / Build e2etest for linux_arm64 (push) Has been cancelled
build / Run e2e test for linux_386 (push) Has been cancelled
build / Run e2e test for windows_386 (push) Has been cancelled
build / Run e2e test for darwin_amd64 (push) Has been cancelled
build / Run e2e test for linux_amd64 (push) Has been cancelled
build / Run e2e test for windows_amd64 (push) Has been cancelled
build / Run e2e test for linux_arm (push) Has been cancelled
build / Run e2e test for linux_arm64 (push) Has been cancelled
build / Run terraform-exec test for linux amd64 (push) Has been cancelled
* refactor: Move method `ValidWorkspaceName` and related const into arguments package, update references This function is primarily used to validate arguments, but it's used once (in (m *Meta) Workspace) in a different context, so I've left the original function signature in the command package. * feat: Add `workspace new`-related code to arguments package * refactor: Update `workspace new` to use the arguments package when parsing arguments Note that this changes the format of errors returned when arg parsing fails - this is now wrapped in a diagnostic so now starts with `\nError: ` and has an extra trailing newline. * feat: Add `workspace select`-related code to arguments package * refactor: Update `workspace select` to use the arguments package when parsing arguments Note that this changes the format of errors returned when arg parsing fails - this is now wrapped in a diagnostic so now starts with `\nError: ` and has an extra trailing newline.
138 lines
2.8 KiB
Go
138 lines
2.8 KiB
Go
// Copyright IBM Corp. 2014, 2026
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/cli"
|
|
"github.com/hashicorp/terraform/internal/command/arguments"
|
|
"github.com/hashicorp/terraform/internal/tfdiags"
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
type WorkspaceSelectCommand struct {
|
|
Meta
|
|
LegacyName bool
|
|
}
|
|
|
|
func (c *WorkspaceSelectCommand) Run(rawArgs []string) int {
|
|
var diags tfdiags.Diagnostics
|
|
|
|
// Process global flags and configure the view/UI.
|
|
rawArgs = c.Meta.process(rawArgs)
|
|
envCommandShowWarning(c.Ui, c.LegacyName)
|
|
|
|
// Process command-specific arguments.
|
|
// Currently there are no arguments for this command, so ignore the returned value for now.
|
|
args, diags := arguments.ParseWorkspaceSelect(rawArgs)
|
|
if diags.HasErrors() {
|
|
c.showDiagnostics(diags)
|
|
return cli.RunResultHelp
|
|
}
|
|
|
|
// Block selecting a workspace if an environment variable will override the new selection anyway.
|
|
current, isOverridden := c.WorkspaceOverridden()
|
|
if isOverridden {
|
|
c.Ui.Error(envIsOverriddenSelectError)
|
|
return 1
|
|
}
|
|
|
|
// Load the backend
|
|
configPath := c.WorkingDir.RootModuleDir()
|
|
b, diags := c.backend(configPath, args.ViewType)
|
|
if diags.HasErrors() {
|
|
c.showDiagnostics(diags)
|
|
return 1
|
|
}
|
|
|
|
// This command will not write state
|
|
c.ignoreRemoteVersionConflict(b)
|
|
|
|
states, wDiags := b.Workspaces()
|
|
if wDiags.HasErrors() {
|
|
c.Ui.Error(wDiags.Err().Error())
|
|
return 1
|
|
}
|
|
c.showDiagnostics(diags) // output warnings, if any
|
|
|
|
name := args.Name
|
|
if name == current {
|
|
// already using this workspace
|
|
return 0
|
|
}
|
|
|
|
found := false
|
|
for _, s := range states {
|
|
if name == s {
|
|
found = true
|
|
break
|
|
}
|
|
}
|
|
|
|
var newState bool
|
|
|
|
if !found {
|
|
if args.OrCreate {
|
|
_, sDiags := b.StateMgr(name)
|
|
if sDiags.HasErrors() {
|
|
c.Ui.Error(sDiags.Err().Error())
|
|
return 1
|
|
}
|
|
newState = true
|
|
} else {
|
|
c.Ui.Error(fmt.Sprintf(envDoesNotExist, name))
|
|
return 1
|
|
}
|
|
}
|
|
|
|
err := c.SetWorkspace(name)
|
|
if err != nil {
|
|
c.Ui.Error(err.Error())
|
|
return 1
|
|
}
|
|
|
|
if newState {
|
|
c.Ui.Output(c.Colorize().Color(fmt.Sprintf(
|
|
strings.TrimSpace(envCreated), name)))
|
|
} else {
|
|
c.Ui.Output(
|
|
c.Colorize().Color(
|
|
fmt.Sprintf(envChanged, name),
|
|
),
|
|
)
|
|
}
|
|
|
|
return 0
|
|
}
|
|
|
|
func (c *WorkspaceSelectCommand) AutocompleteArgs() complete.Predictor {
|
|
return completePredictSequence{
|
|
c.completePredictWorkspaceName(),
|
|
complete.PredictDirs(""),
|
|
}
|
|
}
|
|
|
|
func (c *WorkspaceSelectCommand) AutocompleteFlags() complete.Flags {
|
|
return nil
|
|
}
|
|
|
|
func (c *WorkspaceSelectCommand) Help() string {
|
|
helpText := `
|
|
Usage: terraform [global options] workspace select NAME
|
|
|
|
Select a different Terraform workspace.
|
|
|
|
Options:
|
|
|
|
-or-create=false Create the Terraform workspace if it doesn't exist.
|
|
|
|
`
|
|
return strings.TrimSpace(helpText)
|
|
}
|
|
|
|
func (c *WorkspaceSelectCommand) Synopsis() string {
|
|
return "Select a workspace"
|
|
}
|