github.com/graywolf-at-work-2/terraform-vendor@v1.4.5/internal/command/workspace_select.go (about) 1 package command 2 3 import ( 4 "fmt" 5 "strings" 6 7 "github.com/hashicorp/terraform/internal/tfdiags" 8 "github.com/mitchellh/cli" 9 "github.com/posener/complete" 10 ) 11 12 type WorkspaceSelectCommand struct { 13 Meta 14 LegacyName bool 15 } 16 17 func (c *WorkspaceSelectCommand) Run(args []string) int { 18 args = c.Meta.process(args) 19 envCommandShowWarning(c.Ui, c.LegacyName) 20 21 var orCreate bool 22 cmdFlags := c.Meta.defaultFlagSet("workspace select") 23 cmdFlags.BoolVar(&orCreate, "or-create", false, "create workspace if it does not exist") 24 cmdFlags.Usage = func() { c.Ui.Error(c.Help()) } 25 if err := cmdFlags.Parse(args); err != nil { 26 c.Ui.Error(fmt.Sprintf("Error parsing command-line flags: %s\n", err.Error())) 27 return 1 28 } 29 30 args = cmdFlags.Args() 31 if len(args) != 1 { 32 c.Ui.Error("Expected a single argument: NAME.\n") 33 return cli.RunResultHelp 34 } 35 36 configPath, err := ModulePath(args[1:]) 37 if err != nil { 38 c.Ui.Error(err.Error()) 39 return 1 40 } 41 42 var diags tfdiags.Diagnostics 43 44 backendConfig, backendDiags := c.loadBackendConfig(configPath) 45 diags = diags.Append(backendDiags) 46 if diags.HasErrors() { 47 c.showDiagnostics(diags) 48 return 1 49 } 50 51 current, isOverridden := c.WorkspaceOverridden() 52 if isOverridden { 53 c.Ui.Error(envIsOverriddenSelectError) 54 return 1 55 } 56 57 // Load the backend 58 b, backendDiags := c.Backend(&BackendOpts{ 59 Config: backendConfig, 60 }) 61 diags = diags.Append(backendDiags) 62 if backendDiags.HasErrors() { 63 c.showDiagnostics(diags) 64 return 1 65 } 66 67 if err != nil { 68 c.Ui.Error(fmt.Sprintf("Failed to load backend: %s", err)) 69 return 1 70 } 71 72 // This command will not write state 73 c.ignoreRemoteVersionConflict(b) 74 75 name := args[0] 76 if !validWorkspaceName(name) { 77 c.Ui.Error(fmt.Sprintf(envInvalidName, name)) 78 return 1 79 } 80 81 states, err := b.Workspaces() 82 if err != nil { 83 c.Ui.Error(err.Error()) 84 return 1 85 } 86 87 if name == current { 88 // already using this workspace 89 return 0 90 } 91 92 found := false 93 for _, s := range states { 94 if name == s { 95 found = true 96 break 97 } 98 } 99 100 var newState bool 101 102 if !found { 103 if orCreate { 104 _, err = b.StateMgr(name) 105 if err != nil { 106 c.Ui.Error(err.Error()) 107 return 1 108 } 109 newState = true 110 } else { 111 c.Ui.Error(fmt.Sprintf(envDoesNotExist, name)) 112 return 1 113 } 114 } 115 116 err = c.SetWorkspace(name) 117 if err != nil { 118 c.Ui.Error(err.Error()) 119 return 1 120 } 121 122 if newState { 123 c.Ui.Output(c.Colorize().Color(fmt.Sprintf( 124 strings.TrimSpace(envCreated), name))) 125 } else { 126 c.Ui.Output( 127 c.Colorize().Color( 128 fmt.Sprintf(envChanged, name), 129 ), 130 ) 131 } 132 133 return 0 134 } 135 136 func (c *WorkspaceSelectCommand) AutocompleteArgs() complete.Predictor { 137 return completePredictSequence{ 138 c.completePredictWorkspaceName(), 139 complete.PredictDirs(""), 140 } 141 } 142 143 func (c *WorkspaceSelectCommand) AutocompleteFlags() complete.Flags { 144 return nil 145 } 146 147 func (c *WorkspaceSelectCommand) Help() string { 148 helpText := ` 149 Usage: terraform [global options] workspace select NAME 150 151 Select a different Terraform workspace. 152 153 Options: 154 155 -or-create=false Create the Terraform workspace if it doesn't exist. 156 157 ` 158 return strings.TrimSpace(helpText) 159 } 160 161 func (c *WorkspaceSelectCommand) Synopsis() string { 162 return "Select a workspace" 163 }