mirror of
https://github.com/opentffoundation/opentf.git
synced 2026-03-11 04:00:50 -04:00
Feedback after 0.9 was that the term "environment" was confusing due to it colliding with several other concepts, such as OS environment variables, a non-aligned Terraform Enterprise concept, and differing ideas of "environment" within various organizations. This new term "workspace" is intended to ease some of that confusion. This term is not used anywhere else in Terraform today, and we expect it to not be used in a manner that would be confusing within user organizations. This begins a deprecation cycle for the "terraform env" family of commands, instead moving to an equivalent set of "terraform workspace" commands. There are some remaining references to the old "environment" concept in the code, which will be cleaned up in a separate change. This change is instead focused on text visible in the UI and wording within code comments for the benefit of human maintainers of the code.
145 lines
3.7 KiB
Go
145 lines
3.7 KiB
Go
package command
|
|
|
|
import (
|
|
"net/url"
|
|
"strings"
|
|
|
|
"github.com/mitchellh/cli"
|
|
)
|
|
|
|
// WorkspaceCommand is a Command Implementation that manipulates workspaces,
|
|
// which allow multiple distinct states and variables from a single config.
|
|
type WorkspaceCommand struct {
|
|
Meta
|
|
LegacyName bool
|
|
}
|
|
|
|
func (c *WorkspaceCommand) Run(args []string) int {
|
|
args = c.Meta.process(args, true)
|
|
|
|
envCommandShowWarning(c.Ui, c.LegacyName)
|
|
|
|
cmdFlags := c.Meta.flagSet("workspace")
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
c.Ui.Output(c.Help())
|
|
return 0
|
|
}
|
|
|
|
func (c *WorkspaceCommand) Help() string {
|
|
helpText := `
|
|
Usage: terraform workspace
|
|
|
|
Create, change and delete Terraform workspaces.
|
|
|
|
|
|
Subcommands:
|
|
|
|
list List workspaces.
|
|
select Select a workspace.
|
|
new Create a new workspace.
|
|
delete Delete an existing workspace.
|
|
`
|
|
return strings.TrimSpace(helpText)
|
|
}
|
|
|
|
func (c *WorkspaceCommand) Synopsis() string {
|
|
return "Workspace management"
|
|
}
|
|
|
|
// validEnvName returns true is this name is valid to use as a workspace name.
|
|
// Since most named states are accessed via a filesystem path or URL, check if
|
|
// escaping the name would be required.
|
|
func validEnvName(name string) bool {
|
|
return name == url.PathEscape(name)
|
|
}
|
|
|
|
func envCommandShowWarning(ui cli.Ui, show bool) {
|
|
if !show {
|
|
return
|
|
}
|
|
|
|
ui.Warn(`Warning: the "terraform env" family of commands is deprecated.
|
|
|
|
"Workspace" is now the preferred term for what earlier Terraform versions
|
|
called "environment", to reduce ambiguity caused by the latter term colliding
|
|
with other concepts.
|
|
|
|
The "terraform workspace" commands should be used instead. "terraform env"
|
|
will be removed in a future Terraform version.
|
|
`)
|
|
}
|
|
|
|
const (
|
|
envNotSupported = `Backend does not support multiple workspaces`
|
|
|
|
envExists = `Workspace %q already exists`
|
|
|
|
envDoesNotExist = `
|
|
Workspace %q doesn't exist.
|
|
|
|
You can create this workspace with the "new" subcommand.`
|
|
|
|
envChanged = `[reset][green]Switched to workspace %q.`
|
|
|
|
envCreated = `
|
|
[reset][green][bold]Created and switched to workspace %q![reset][green]
|
|
|
|
You're now on a new, empty workspace. Workspaces isolate their state,
|
|
so if you run "terraform plan" Terraform will not see any existing state
|
|
for this configuration.
|
|
`
|
|
|
|
envDeleted = `[reset][green]Deleted workspace %q!`
|
|
|
|
envNotEmpty = `
|
|
Workspace %[1]q is not empty.
|
|
|
|
Deleting %[1]q can result in dangling resources: resources that
|
|
exist but are no longer manageable by Terraform. Please destroy
|
|
these resources first. If you want to delete this workspace
|
|
anyway and risk dangling resources, use the '-force' flag.
|
|
`
|
|
|
|
envWarnNotEmpty = `[reset][yellow]WARNING: %q was non-empty.
|
|
The resources managed by the deleted workspace may still exist,
|
|
but are no longer manageable by Terraform since the state has
|
|
been deleted.
|
|
`
|
|
|
|
envDelCurrent = `
|
|
Workspace %[1]q is your active workspace.
|
|
|
|
You cannot delete the currently active workspace. Please switch
|
|
to another workspace and try again.
|
|
`
|
|
|
|
envInvalidName = `
|
|
The workspace name %q is not allowed. The name must contain only URL safe
|
|
characters, and no path separators.
|
|
`
|
|
|
|
envIsOverriddenNote = `
|
|
|
|
The active workspace is being overridden using the TF_WORKSPACE environment
|
|
variable.
|
|
`
|
|
|
|
envIsOverriddenSelectError = `
|
|
The selected workspace is currently overridden using the TF_WORKSPACE
|
|
environment variable.
|
|
|
|
To select a new workspace, either update this environment variable or unset
|
|
it and then run this command again.
|
|
`
|
|
|
|
envIsOverriddenNewError = `
|
|
The workspace is currently overridden using the TF_WORKSPACE environment
|
|
variable. You cannot create a new workspace when using this setting.
|
|
|
|
To create a new workspace, either unset this environment variable or update it
|
|
to match the workspace name you are trying to create, and then run this command
|
|
again.
|
|
`
|
|
)
|