github.com/cycloidio/terraform@v1.1.10-0.20220513142504-76d5c768dc63/command/workspace_command.go (about)

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