github.com/opentofu/opentofu@v1.7.1/internal/command/workspace_command.go (about)

     1  // Copyright (c) The OpenTofu Authors
     2  // SPDX-License-Identifier: MPL-2.0
     3  // Copyright (c) 2023 HashiCorp, Inc.
     4  // SPDX-License-Identifier: MPL-2.0
     5  
     6  package command
     7  
     8  import (
     9  	"net/url"
    10  	"strings"
    11  
    12  	"github.com/mitchellh/cli"
    13  )
    14  
    15  // WorkspaceCommand is a Command Implementation that manipulates workspaces,
    16  // which allow multiple distinct states and variables from a single config.
    17  type WorkspaceCommand struct {
    18  	Meta
    19  	LegacyName bool
    20  }
    21  
    22  func (c *WorkspaceCommand) Run(args []string) int {
    23  	c.Meta.process(args)
    24  	envCommandShowWarning(c.Ui, c.LegacyName)
    25  
    26  	cmdFlags := c.Meta.extendedFlagSet("workspace")
    27  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    28  
    29  	return cli.RunResultHelp
    30  }
    31  
    32  func (c *WorkspaceCommand) Help() string {
    33  	helpText := `
    34  Usage: tofu [global options] workspace
    35  
    36    new, list, show, select and delete OpenTofu workspaces.
    37  
    38  `
    39  	return strings.TrimSpace(helpText)
    40  }
    41  
    42  func (c *WorkspaceCommand) Synopsis() string {
    43  	return "Workspace management"
    44  }
    45  
    46  // validWorkspaceName returns true is this name is valid to use as a workspace name.
    47  // Since most named states are accessed via a filesystem path or URL, check if
    48  // escaping the name would be required.
    49  func validWorkspaceName(name string) bool {
    50  	return name == url.PathEscape(name)
    51  }
    52  
    53  func envCommandShowWarning(ui cli.Ui, show bool) {
    54  	if !show {
    55  		return
    56  	}
    57  
    58  	ui.Warn(`Warning: the "tofu env" family of commands is deprecated.
    59  
    60  "Workspace" is now the preferred term for what earlier OpenTofu versions
    61  called "environment", to reduce ambiguity caused by the latter term colliding
    62  with other concepts.
    63  
    64  The "tofu workspace" commands should be used instead. "tofu env"
    65  will be removed in a future OpenTofu version.
    66  `)
    67  }
    68  
    69  const (
    70  	envExists = `Workspace %q already exists`
    71  
    72  	envDoesNotExist = `
    73  Workspace %q doesn't exist.
    74  
    75  You can create this workspace with the "new" subcommand 
    76  or include the "-or-create" flag with the "select" subcommand.`
    77  
    78  	envChanged = `[reset][green]Switched to workspace %q.`
    79  
    80  	envCreated = `
    81  [reset][green][bold]Created and switched to workspace %q![reset][green]
    82  
    83  You're now on a new, empty workspace. Workspaces isolate their state,
    84  so if you run "tofu plan" OpenTofu will not see any existing state
    85  for this configuration.
    86  `
    87  
    88  	envDeleted = `[reset][green]Deleted workspace %q!`
    89  
    90  	envWarnNotEmpty = `[reset][yellow]WARNING: %q was non-empty.
    91  The resources managed by the deleted workspace may still exist,
    92  but are no longer manageable by OpenTofu since the state has
    93  been deleted.
    94  `
    95  
    96  	envDelCurrent = `
    97  Workspace %[1]q is your active workspace.
    98  
    99  You cannot delete the currently active workspace. Please switch
   100  to another workspace and try again.
   101  `
   102  
   103  	envInvalidName = `
   104  The workspace name %q is not allowed. The name must contain only URL safe
   105  characters, and no path separators.
   106  `
   107  
   108  	envIsOverriddenNote = `
   109  
   110  The active workspace is being overridden using the TF_WORKSPACE environment
   111  variable.
   112  `
   113  
   114  	envIsOverriddenSelectError = `
   115  The selected workspace is currently overridden using the TF_WORKSPACE
   116  environment variable.
   117  
   118  To select a new workspace, either update this environment variable or unset
   119  it and then run this command again.
   120  `
   121  
   122  	envIsOverriddenNewError = `
   123  The workspace is currently overridden using the TF_WORKSPACE environment
   124  variable. You cannot create a new workspace when using this setting.
   125  
   126  To create a new workspace, either unset this environment variable or update it
   127  to match the workspace name you are trying to create, and then run this command
   128  again.
   129  `
   130  )