mirror of
https://github.com/hashicorp/terraform.git
synced 2026-02-03 20:50:59 -05:00
Some checks are pending
build / Determine intended Terraform version (push) Waiting to run
build / Determine Go toolchain version (push) Waiting to run
build / Generate release metadata (push) Blocked by required conditions
build / Build for freebsd_386 (push) Blocked by required conditions
build / Build for linux_386 (push) Blocked by required conditions
build / Build for openbsd_386 (push) Blocked by required conditions
build / Build for windows_386 (push) Blocked by required conditions
build / Build for darwin_amd64 (push) Blocked by required conditions
build / Build for freebsd_amd64 (push) Blocked by required conditions
build / Build for linux_amd64 (push) Blocked by required conditions
build / Build for openbsd_amd64 (push) Blocked by required conditions
build / Build for solaris_amd64 (push) Blocked by required conditions
build / Build for windows_amd64 (push) Blocked by required conditions
build / Build for freebsd_arm (push) Blocked by required conditions
build / Build for linux_arm (push) Blocked by required conditions
build / Build for darwin_arm64 (push) Blocked by required conditions
build / Build for linux_arm64 (push) Blocked by required conditions
build / Build for windows_arm64 (push) Blocked by required conditions
build / Build Docker image for linux_386 (push) Blocked by required conditions
build / Build Docker image for linux_amd64 (push) Blocked by required conditions
build / Build Docker image for linux_arm (push) Blocked by required conditions
build / Build Docker image for linux_arm64 (push) Blocked by required conditions
build / Build e2etest for linux_386 (push) Blocked by required conditions
build / Build e2etest for windows_386 (push) Blocked by required conditions
build / Build e2etest for darwin_amd64 (push) Blocked by required conditions
build / Build e2etest for linux_amd64 (push) Blocked by required conditions
build / Build e2etest for windows_amd64 (push) Blocked by required conditions
build / Build e2etest for linux_arm (push) Blocked by required conditions
build / Build e2etest for darwin_arm64 (push) Blocked by required conditions
build / Build e2etest for linux_arm64 (push) Blocked by required conditions
build / Run e2e test for linux_386 (push) Blocked by required conditions
build / Run e2e test for windows_386 (push) Blocked by required conditions
build / Run e2e test for darwin_amd64 (push) Blocked by required conditions
build / Run e2e test for linux_amd64 (push) Blocked by required conditions
build / Run e2e test for windows_amd64 (push) Blocked by required conditions
build / Run e2e test for linux_arm (push) Blocked by required conditions
build / Run e2e test for linux_arm64 (push) Blocked by required conditions
build / Run terraform-exec test for linux amd64 (push) Blocked by required conditions
Quick Checks / Unit Tests (push) Waiting to run
Quick Checks / Race Tests (push) Waiting to run
Quick Checks / End-to-end Tests (push) Waiting to run
Quick Checks / Code Consistency Checks (push) Waiting to run
* Add a generic method for loading an operations backend in non-init commands * Refactor commands to use new prepareBackend method: group 1 * Refactor commands to use new prepareBackend method: group 2, where config parsing needs to be explicitly added * Refactor commands to use new prepareBackend method: group 3, where we can use already parsed config * Additional, more nested, places where logic for accessing backends needs to be refactored * Remove duplicated comment * Add test coverage of `(m *Meta) prepareBackend()` * Add TODO related to using plans for backend/state_store config in apply commands * Add `testStateStoreMockWithChunkNegotiation` test helper * Add assertions to tests about the backend (remote-state, local, etc) in use within operations backend * Stop prepareBackend taking locks as argument * Code comment in prepareBackend * Replace c.Meta.prepareBackend with c.prepareBackend * Change `c.Meta.loadSingleModule` to `c.loadSingleModule` * Rename (Meta).prepareBackend to (Meta).backend, update godoc comment to make relationship to (Meta).Backend more obvious. * Revert change from config.Module to config.Root.Module * Update `(m *Meta) backend` method to parse config itself, and also to adhere to calling code's viewtype instructions * Update all tests and calling code following previous commit * Change how an operations backend is obtained by autocomplete code * Update autocomplete to return nil if no workspace names are returned from the backend * Add test coverage for autocompleting workspace names when using a pluggable state store * Fix output command: pass view type data to new `backend` method * Fix in plan command: pass correct view type to `backend` method * Fix `providers schema` command to use correct viewtype when preparing a backend
156 lines
3 KiB
Go
156 lines
3 KiB
Go
// Copyright (c) HashiCorp, Inc.
|
|
// SPDX-License-Identifier: BUSL-1.1
|
|
|
|
package command
|
|
|
|
import (
|
|
"fmt"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/cli"
|
|
"github.com/hashicorp/terraform/internal/command/arguments"
|
|
"github.com/posener/complete"
|
|
)
|
|
|
|
type WorkspaceSelectCommand struct {
|
|
Meta
|
|
LegacyName bool
|
|
}
|
|
|
|
func (c *WorkspaceSelectCommand) Run(args []string) int {
|
|
args = c.Meta.process(args)
|
|
envCommandShowWarning(c.Ui, c.LegacyName)
|
|
|
|
var orCreate bool
|
|
cmdFlags := c.Meta.defaultFlagSet("workspace select")
|
|
cmdFlags.BoolVar(&orCreate, "or-create", false, "create workspace if it does not exist")
|
|
cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
|
|
if err := cmdFlags.Parse(args); err != nil {
|
|
c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error()))
|
|
return 1
|
|
}
|
|
|
|
args = cmdFlags.Args()
|
|
if len(args) != 1 {
|
|
c.Ui.Error("Expected a single argument: NAME.\n")
|
|
return cli.RunResultHelp
|
|
}
|
|
|
|
configPath, err := ModulePath(args[1:])
|
|
if err != nil {
|
|
c.Ui.Error(err.Error())
|
|
return 1
|
|
}
|
|
|
|
current, isOverridden := c.WorkspaceOverridden()
|
|
if isOverridden {
|
|
c.Ui.Error(envIsOverriddenSelectError)
|
|
return 1
|
|
}
|
|
|
|
// Load the backend
|
|
view := arguments.ViewHuman
|
|
b, diags := c.backend(configPath, view)
|
|
if diags.HasErrors() {
|
|
c.showDiagnostics(diags)
|
|
return 1
|
|
}
|
|
|
|
if err != nil {
|
|
c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err))
|
|
return 1
|
|
}
|
|
|
|
// This command will not write state
|
|
c.ignoreRemoteVersionConflict(b)
|
|
|
|
name := args[0]
|
|
if !validWorkspaceName(name) {
|
|
c.Ui.Error(fmt.Sprintf(envInvalidName, name))
|
|
return 1
|
|
}
|
|
|
|
states, wDiags := b.Workspaces()
|
|
if wDiags.HasErrors() {
|
|
c.Ui.Error(wDiags.Err().Error())
|
|
return 1
|
|
}
|
|
c.showDiagnostics(diags) // output warnings, if any
|
|
|
|
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 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"
|
|
}
|