cloud: Display the newly renamed workspaces after a migration

This commit is contained in:
Chris Arcand 2021-11-03 11:22:46 -05:00
parent c3bf5d4512
commit 6f25ad3e17
1 changed files with 29 additions and 8 deletions

View File

@ -1,6 +1,7 @@
package command package command
import ( import (
"bytes"
"context" "context"
"errors" "errors"
"fmt" "fmt"
@ -666,28 +667,48 @@ func (m *Meta) backendMigrateState_S_TFC(opts *backendMigrateOpts, sourceWorkspa
} }
// After migrating multiple workspaces, we need to reselect the current workspace as it may // After migrating multiple workspaces, we need to reselect the current workspace as it may
// have been renamed. Query the backend first to be sure it now exists, and if it does, // have been renamed. Query the backend first to be sure it now exists.
// select it.
workspaces, err := opts.Destination.Workspaces() workspaces, err := opts.Destination.Workspaces()
if err != nil { if err != nil {
return err return err
} }
var workspacePresent bool
for _, name := range workspaces { for _, name := range workspaces {
if name == newCurrentWorkspace { if name == newCurrentWorkspace {
if err = m.SetWorkspace(name); err != nil { workspacePresent = true
return err
}
return nil
} }
} }
// If we couldn't select the workspace automatically from the backend (maybe it was empty // If we couldn't select the workspace automatically from the backend (maybe it was empty
// and wasn't migrated, for instance), ask the user to select one. // and wasn't migrated, for instance), ask the user to select one instead and be done.
if err = m.selectWorkspace(opts.Destination); err != nil { if !workspacePresent {
if err = m.selectWorkspace(opts.Destination); err != nil {
return err
}
return nil
}
// The newly renamed current workspace is present, so we'll automatically select it for the
// user, as well as display the equivalent of 'workspace list' to show how the workspaces
// were changed (as well as the newly selected current workspace).
if err = m.SetWorkspace(newCurrentWorkspace); err != nil {
return err return err
} }
m.Ui.Output(m.Colorize().Color("[reset][bold]Migration complete! Your workspaces are as follows:[reset]"))
var out bytes.Buffer
for _, name := range workspaces {
if name == newCurrentWorkspace {
out.WriteString("* ")
} else {
out.WriteString(" ")
}
out.WriteString(name + "\n")
}
m.Ui.Output(out.String())
return nil return nil
} }