Skip to content

Commit 698a72f

Browse files
committed
Add handling of non-default workspace, remove input prompting for now.
1 parent afccc3f commit 698a72f

File tree

1 file changed

+39
-17
lines changed

1 file changed

+39
-17
lines changed

internal/command/meta_backend.go

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,29 +1646,48 @@ func (m *Meta) stateStore_C_s(c *configs.StateStore, cHash int, backendSMgr *cli
16461646
if opts.Init && b != nil {
16471647
err := m.selectWorkspace(b)
16481648
if strings.Contains(err.Error(), "No existing workspaces") {
1649+
// If there are no workspaces, Terraform either needs to create the default workspace here,
1650+
// or instruct the user to run a `terraform workspace new` command.
16491651
ws, err := m.Workspace()
16501652
if err != nil {
16511653
diags = diags.Append(fmt.Errorf("Failed to check current workspace: %w", err))
16521654
return nil, diags
16531655
}
16541656

1655-
if m.Input() && ws == backend.DefaultStateName {
1656-
input := m.UIInput()
1657-
desc := fmt.Sprintf("Terraform will create the %q workspace via %q.\n"+
1658-
"Only 'yes' will be accepted to approve.", backend.DefaultStateName, c.Type)
1659-
v, err := input.Input(context.Background(), &terraform.InputOpts{
1660-
Id: "approve",
1661-
Query: fmt.Sprintf("Workspace %q does not exit, would you like to create one?", backend.DefaultStateName),
1662-
Description: desc,
1663-
})
1664-
if err != nil {
1665-
diags = diags.Append(fmt.Errorf("Failed to confirm default workspace creation: %w", err))
1666-
return nil, diags
1667-
}
1668-
if v != "yes" {
1669-
diags = diags.Append(errors.New("Failed to create default workspace"))
1670-
return nil, diags
1671-
}
1657+
switch {
1658+
case ws != backend.DefaultStateName:
1659+
// User needs to run a `terraform workspace new` command.
1660+
diags = append(diags, tfdiags.Sourceless(
1661+
tfdiags.Error,
1662+
fmt.Sprintf("Workspace %q has not been created yet", ws),
1663+
fmt.Sprintf("State store %q in provider %s (%q) reports that no workspaces currently exist. To create the custom workspace %q use the command `terraform workspace new %s`.",
1664+
c.Type,
1665+
c.Provider.Name,
1666+
c.ProviderAddr,
1667+
ws,
1668+
ws,
1669+
),
1670+
))
1671+
return nil, diags
1672+
1673+
case ws == backend.DefaultStateName:
1674+
// TODO: do we want to prompt for input here (m.Input()), or create automatically unless -readonly flag present?
1675+
// input := m.UIInput()
1676+
// desc := fmt.Sprintf("Terraform will create the %q workspace via %q.\n"+
1677+
// "Only 'yes' will be accepted to approve.", backend.DefaultStateName, c.Type)
1678+
// v, err := input.Input(context.Background(), &terraform.InputOpts{
1679+
// Id: "approve",
1680+
// Query: fmt.Sprintf("Workspace %q does not exit, would you like to create one?", backend.DefaultStateName),
1681+
// Description: desc,
1682+
// })
1683+
// if err != nil {
1684+
// diags = diags.Append(fmt.Errorf("Failed to confirm default workspace creation: %w", err))
1685+
// return nil, diags
1686+
// }
1687+
// if v != "yes" {
1688+
// diags = diags.Append(errors.New("Failed to create default workspace"))
1689+
// return nil, diags
1690+
// }
16721691

16731692
// TODO: Confirm if defaulting to creation on first use (rather than error) is a good idea
16741693
// Make the default workspace. All other workspaces are user-created via the workspace commands.
@@ -1691,6 +1710,9 @@ func (m *Meta) stateStore_C_s(c *configs.StateStore, cHash int, backendSMgr *cli
16911710
diags = diags.Append(fmt.Errorf(errStateStoreWorkspaceCreate, c.Type, err))
16921711
return nil, diags
16931712
}
1713+
default:
1714+
diags = diags.Append(err)
1715+
return nil, diags
16941716
}
16951717
// TODO: handle if input is not enabled
16961718
}

0 commit comments

Comments
 (0)