github.com/vic3lord/terraform@v0.8.0-rc1.0.20170626102919-16c6dd2cb372/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  	args = c.Meta.process(args, true)
    19  
    20  	envCommandShowWarning(c.Ui, c.LegacyName)
    21  
    22  	cmdFlags := c.Meta.flagSet("workspace")
    23  	cmdFlags.Usage = func() { c.Ui.Error(c.Help()) }
    24  
    25  	c.Ui.Output(c.Help())
    26  	return 0
    27  }
    28  
    29  func (c *WorkspaceCommand) Help() string {
    30  	helpText := `
    31  Usage: terraform workspace
    32  
    33    Create, change and delete Terraform workspaces.
    34  
    35  
    36  Subcommands:
    37  
    38      list      List workspaces.
    39      select    Select a workspace.
    40      new       Create a new workspace.
    41      delete    Delete an existing workspace.
    42  `
    43  	return strings.TrimSpace(helpText)
    44  }
    45  
    46  func (c *WorkspaceCommand) Synopsis() string {
    47  	return "Workspace management"
    48  }
    49  
    50  // validWorkspaceName returns true is this name is valid to use as a workspace name.
    51  // Since most named states are accessed via a filesystem path or URL, check if
    52  // escaping the name would be required.
    53  func validWorkspaceName(name string) bool {
    54  	return name == url.PathEscape(name)
    55  }
    56  
    57  func envCommandShowWarning(ui cli.Ui, show bool) {
    58  	if !show {
    59  		return
    60  	}
    61  
    62  	ui.Warn(`Warning: the "terraform env" family of commands is deprecated.
    63  
    64  "Workspace" is now the preferred term for what earlier Terraform versions
    65  called "environment", to reduce ambiguity caused by the latter term colliding
    66  with other concepts.
    67  
    68  The "terraform workspace" commands should be used instead. "terraform env"
    69  will be removed in a future Terraform version.
    70  `)
    71  }
    72  
    73  const (
    74  	envNotSupported = `Backend does not support multiple workspaces`
    75  
    76  	envExists = `Workspace %q already exists`
    77  
    78  	envDoesNotExist = `
    79  Workspace %q doesn't exist.
    80  
    81  You can create this workspace with the "new" subcommand.`
    82  
    83  	envChanged = `[reset][green]Switched to workspace %q.`
    84  
    85  	envCreated = `
    86  [reset][green][bold]Created and switched to workspace %q![reset][green]
    87  
    88  You're now on a new, empty workspace. Workspaces isolate their state,
    89  so if you run "terraform plan" Terraform will not see any existing state
    90  for this configuration.
    91  `
    92  
    93  	envDeleted = `[reset][green]Deleted workspace %q!`
    94  
    95  	envNotEmpty = `
    96  Workspace %[1]q is not empty.
    97  
    98  Deleting %[1]q can result in dangling resources: resources that
    99  exist but are no longer manageable by Terraform. Please destroy
   100  these resources first.  If you want to delete this workspace
   101  anyway and risk dangling resources, use the '-force' flag.
   102  `
   103  
   104  	envWarnNotEmpty = `[reset][yellow]WARNING: %q was non-empty.
   105  The resources managed by the deleted workspace may still exist,
   106  but are no longer manageable by Terraform since the state has
   107  been deleted.
   108  `
   109  
   110  	envDelCurrent = `
   111  Workspace %[1]q is your active workspace.
   112  
   113  You cannot delete the currently active workspace. Please switch
   114  to another workspace and try again.
   115  `
   116  
   117  	envInvalidName = `
   118  The workspace name %q is not allowed. The name must contain only URL safe
   119  characters, and no path separators.
   120  `
   121  
   122  	envIsOverriddenNote = `
   123  
   124  The active workspace is being overridden using the TF_WORKSPACE environment
   125  variable.
   126  `
   127  
   128  	envIsOverriddenSelectError = `
   129  The selected workspace is currently overridden using the TF_WORKSPACE
   130  environment variable.
   131  
   132  To select a new workspace, either update this environment variable or unset
   133  it and then run this command again.
   134  `
   135  
   136  	envIsOverriddenNewError = `
   137  The workspace is currently overridden using the TF_WORKSPACE environment
   138  variable. You cannot create a new workspace when using this setting.
   139  
   140  To create a new workspace, either unset this environment variable or update it
   141  to match the workspace name you are trying to create, and then run this command
   142  again.
   143  `
   144  )