github.com/iaas-resource-provision/iaas-rpc@v1.0.7-0.20211021023331-ed21f798c408/internal/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  	envNotEmpty = `
    86  Workspace %[1]q is not empty.
    87  
    88  Deleting %[1]q can result in dangling resources: resources that
    89  exist but are no longer manageable by Terraform. Please destroy
    90  these resources first.  If you want to delete this workspace
    91  anyway and risk dangling resources, use the '-force' flag.
    92  `
    93  
    94  	envWarnNotEmpty = `[reset][yellow]WARNING: %q was non-empty.
    95  The resources managed by the deleted workspace may still exist,
    96  but are no longer manageable by Terraform since the state has
    97  been deleted.
    98  `
    99  
   100  	envDelCurrent = `
   101  Workspace %[1]q is your active workspace.
   102  
   103  You cannot delete the currently active workspace. Please switch
   104  to another workspace and try again.
   105  `
   106  
   107  	envInvalidName = `
   108  The workspace name %q is not allowed. The name must contain only URL safe
   109  characters, and no path separators.
   110  `
   111  
   112  	envIsOverriddenNote = `
   113  
   114  The active workspace is being overridden using the TF_WORKSPACE environment
   115  variable.
   116  `
   117  
   118  	envIsOverriddenSelectError = `
   119  The selected workspace is currently overridden using the TF_WORKSPACE
   120  environment variable.
   121  
   122  To select a new workspace, either update this environment variable or unset
   123  it and then run this command again.
   124  `
   125  
   126  	envIsOverriddenNewError = `
   127  The workspace is currently overridden using the TF_WORKSPACE environment
   128  variable. You cannot create a new workspace when using this setting.
   129  
   130  To create a new workspace, either unset this environment variable or update it
   131  to match the workspace name you are trying to create, and then run this command
   132  again.
   133  `
   134  )