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