Skip to content

Commit af94d52

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

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
@@ -1641,29 +1641,48 @@ func (m *Meta) stateStore_C_s(c *configs.StateStore, cHash int, backendSMgr *cli
16411641
if opts.Init && b != nil {
16421642
err := m.selectWorkspace(b)
16431643
if strings.Contains(err.Error(), "No existing workspaces") {
1644+
// If there are no workspaces, Terraform either needs to create the default workspace here,
1645+
// or instruct the user to run a `terraform workspace new` command.
16441646
ws, err := m.Workspace()
16451647
if err != nil {
16461648
diags = diags.Append(fmt.Errorf("Failed to check current workspace: %w", err))
16471649
return nil, diags
16481650
}
16491651

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

16681687
// TODO: Confirm if defaulting to creation on first use (rather than error) is a good idea
16691688
// Make the default workspace. All other workspaces are user-created via the workspace commands.
@@ -1685,6 +1704,9 @@ func (m *Meta) stateStore_C_s(c *configs.StateStore, cHash int, backendSMgr *cli
16851704
diags = diags.Append(fmt.Errorf(errStateStoreWorkspaceCreate, c.Type, err))
16861705
return nil, diags
16871706
}
1707+
default:
1708+
diags = diags.Append(err)
1709+
return nil, diags
16881710
}
16891711
// TODO: handle if input is not enabled
16901712
}

0 commit comments

Comments
 (0)