command: Allow workspace delete with invalid name
We are validating the workspace name for all workspace commands. Due to a bug with the TF_WORKSPACE environment variable, it has been possible to accidentally create a workspace with an invalid name. This commit removes the valid workspace name check for workspace delete to allow users to clean up any invalid workspaces.
This commit is contained in:
parent
b239570abb
commit
8252920e9a
|
@ -335,6 +335,39 @@ func TestWorkspace_delete(t *testing.T) {
|
||||||
t.Fatalf("wrong workspace: %q", current)
|
t.Fatalf("wrong workspace: %q", current)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestWorkspace_deleteInvalid(t *testing.T) {
|
||||||
|
td := tempDir(t)
|
||||||
|
os.MkdirAll(td, 0755)
|
||||||
|
defer os.RemoveAll(td)
|
||||||
|
defer testChdir(t, td)()
|
||||||
|
|
||||||
|
// choose an invalid workspace name
|
||||||
|
workspace := "test workspace"
|
||||||
|
path := filepath.Join(local.DefaultWorkspaceDir, workspace)
|
||||||
|
|
||||||
|
// create the workspace directories
|
||||||
|
if err := os.MkdirAll(path, 0755); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
ui := new(cli.MockUi)
|
||||||
|
delCmd := &WorkspaceDeleteCommand{
|
||||||
|
Meta: Meta{Ui: ui},
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete the workspace
|
||||||
|
if code := delCmd.Run([]string{workspace}); code != 0 {
|
||||||
|
t.Fatalf("error deleting workspace: %s", ui.ErrorWriter)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, err := os.Stat(path); err == nil {
|
||||||
|
t.Fatalf("should have deleted workspace, but %s still exists", path)
|
||||||
|
} else if !os.IsNotExist(err) {
|
||||||
|
t.Fatalf("unexpected error for workspace path: %s", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestWorkspace_deleteWithState(t *testing.T) {
|
func TestWorkspace_deleteWithState(t *testing.T) {
|
||||||
td := tempDir(t)
|
td := tempDir(t)
|
||||||
os.MkdirAll(td, 0755)
|
os.MkdirAll(td, 0755)
|
||||||
|
|
|
@ -40,13 +40,6 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int {
|
||||||
return cli.RunResultHelp
|
return cli.RunResultHelp
|
||||||
}
|
}
|
||||||
|
|
||||||
workspace := args[0]
|
|
||||||
|
|
||||||
if !validWorkspaceName(workspace) {
|
|
||||||
c.Ui.Error(fmt.Sprintf(envInvalidName, workspace))
|
|
||||||
return 1
|
|
||||||
}
|
|
||||||
|
|
||||||
configPath, err := ModulePath(args[1:])
|
configPath, err := ModulePath(args[1:])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.Ui.Error(err.Error())
|
c.Ui.Error(err.Error())
|
||||||
|
@ -78,6 +71,7 @@ func (c *WorkspaceDeleteCommand) Run(args []string) int {
|
||||||
return 1
|
return 1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
workspace := args[0]
|
||||||
exists := false
|
exists := false
|
||||||
for _, ws := range workspaces {
|
for _, ws := range workspaces {
|
||||||
if workspace == ws {
|
if workspace == ws {
|
||||||
|
|
Loading…
Reference in New Issue