2023-05-02 10:33:06 -05:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2017-05-30 17:06:13 -05:00
|
|
|
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 {
|
2022-06-24 18:57:44 -05:00
|
|
|
c.Meta.process(args)
|
|
|
|
envCommandShowWarning(c.Ui, c.LegacyName)
|
|
|
|
|
|
|
|
cmdFlags := c.Meta.extendedFlagSet("workspace")
|
|
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
|
|
|
2022-06-24 18:33:11 -05:00
|
|
|
return cli.RunResultHelp
|
2017-05-30 17:06:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WorkspaceCommand) Help() string {
|
|
|
|
helpText := `
|
2023-09-21 07:38:46 -05:00
|
|
|
Usage: tofu [global options] workspace
|
2017-05-30 17:06:13 -05:00
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
new, list, show, select and delete OpenTofu workspaces.
|
2017-05-30 17:06:13 -05:00
|
|
|
|
|
|
|
`
|
|
|
|
return strings.TrimSpace(helpText)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *WorkspaceCommand) Synopsis() string {
|
|
|
|
return "Workspace management"
|
|
|
|
}
|
|
|
|
|
2017-05-30 19:13:43 -05:00
|
|
|
// validWorkspaceName returns true is this name is valid to use as a workspace name.
|
2017-05-30 17:06:13 -05:00
|
|
|
// Since most named states are accessed via a filesystem path or URL, check if
|
|
|
|
// escaping the name would be required.
|
2017-05-30 19:13:43 -05:00
|
|
|
func validWorkspaceName(name string) bool {
|
2017-05-30 17:06:13 -05:00
|
|
|
return name == url.PathEscape(name)
|
|
|
|
}
|
|
|
|
|
|
|
|
func envCommandShowWarning(ui cli.Ui, show bool) {
|
|
|
|
if !show {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
ui.Warn(`Warning: the "tofu env" family of commands is deprecated.
|
2017-05-30 17:06:13 -05:00
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
"Workspace" is now the preferred term for what earlier OpenTofu versions
|
2017-05-30 17:06:13 -05:00
|
|
|
called "environment", to reduce ambiguity caused by the latter term colliding
|
|
|
|
with other concepts.
|
|
|
|
|
2023-09-21 07:38:46 -05:00
|
|
|
The "tofu workspace" commands should be used instead. "tofu env"
|
|
|
|
will be removed in a future OpenTofu version.
|
2017-05-30 17:06:13 -05:00
|
|
|
`)
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
envExists = `Workspace %q already exists`
|
|
|
|
|
|
|
|
envDoesNotExist = `
|
|
|
|
Workspace %q doesn't exist.
|
|
|
|
|
2022-08-31 12:36:31 -05:00
|
|
|
You can create this workspace with the "new" subcommand
|
2022-08-31 14:13:15 -05:00
|
|
|
or include the "-or-create" flag with the "select" subcommand.`
|
2017-05-30 17:06:13 -05:00
|
|
|
|
|
|
|
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,
|
2023-09-21 07:38:46 -05:00
|
|
|
so if you run "tofu plan" OpenTofu will not see any existing state
|
2017-05-30 17:06:13 -05:00
|
|
|
for this configuration.
|
|
|
|
`
|
|
|
|
|
|
|
|
envDeleted = `[reset][green]Deleted workspace %q!`
|
|
|
|
|
|
|
|
envWarnNotEmpty = `[reset][yellow]WARNING: %q was non-empty.
|
|
|
|
The resources managed by the deleted workspace may still exist,
|
2023-09-21 07:38:46 -05:00
|
|
|
but are no longer manageable by OpenTofu since the state has
|
2017-05-30 17:06:13 -05:00
|
|
|
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.
|
|
|
|
`
|
|
|
|
)
|