generated from brevdev/seed
-
Notifications
You must be signed in to change notification settings - Fork 16
/
reset.go
124 lines (106 loc) · 4.08 KB
/
reset.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package reset
import (
"fmt"
"github.com/brevdev/brev-cli/pkg/cmd/completions"
"github.com/brevdev/brev-cli/pkg/entity"
"github.com/brevdev/brev-cli/pkg/store"
"github.com/brevdev/brev-cli/pkg/terminal"
breverrors "github.com/brevdev/brev-cli/pkg/errors"
"github.com/spf13/cobra"
)
var (
startLong = "Reset your machine if it's acting up. This deletes the machine and gets you a fresh one."
startExample = "brev reset <ws_name>"
)
type ResetStore interface {
completions.CompletionStore
ResetWorkspace(workspaceID string) (*entity.Workspace, error)
GetAllWorkspaces(options *store.GetWorkspacesOptions) ([]entity.Workspace, error)
GetWorkspaces(organizationID string, options *store.GetWorkspacesOptions) ([]entity.Workspace, error)
GetActiveOrganizationOrDefault() (*entity.Organization, error)
GetCurrentUser() (*entity.User, error)
GetWorkspace(id string) (*entity.Workspace, error)
GetWorkspaceMetaData(workspaceID string) (*entity.WorkspaceMetaData, error)
}
func NewCmdReset(t *terminal.Terminal, loginResetStore ResetStore, noLoginResetStore ResetStore) *cobra.Command {
cmd := &cobra.Command{
Annotations: map[string]string{"workspace": ""},
Use: "reset",
DisableFlagsInUseLine: true,
Short: "Reset a workspace if it's in a weird state.",
Long: startLong,
Example: startExample,
Args: cobra.ExactArgs(1),
ValidArgsFunction: completions.GetAllWorkspaceNameCompletionHandler(noLoginResetStore, t),
Run: func(cmd *cobra.Command, args []string) {
err := resetWorkspace(args[0], t, loginResetStore)
if err != nil {
t.Vprint(t.Red(err.Error()))
}
},
}
return cmd
}
func resetWorkspace(workspaceName string, t *terminal.Terminal, resetStore ResetStore) error {
workspace, err := getWorkspaceFromNameOrID(workspaceName, resetStore)
if err != nil {
return breverrors.WrapAndTrace(err)
}
startedWorkspace, err := resetStore.ResetWorkspace(workspace.ID)
if err != nil {
return breverrors.WrapAndTrace(err)
}
t.Vprintf("Workspace %s is resetting. \n Note: this can take a few seconds. Run 'brev ls' to check status\n", startedWorkspace.Name)
return nil
}
// NOTE: this function is copy/pasted in many places. If you modify it, modify it elsewhere.
// Reasoning: there wasn't a utils file so I didn't know where to put it
// + not sure how to pass a generic "store" object
func getWorkspaceFromNameOrID(nameOrID string, sstore ResetStore) (*entity.WorkspaceWithMeta, error) {
// Get Active Org
org, err := sstore.GetActiveOrganizationOrDefault()
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
if org == nil {
return nil, fmt.Errorf("no orgs exist")
}
// Get Current User
currentUser, err := sstore.GetCurrentUser()
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
// Get Workspaces for User
var workspace *entity.Workspace // this will be the returned workspace
workspaces, err := sstore.GetWorkspaces(org.ID, &store.GetWorkspacesOptions{Name: nameOrID, UserID: currentUser.ID})
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
switch len(workspaces) {
case 0:
// In this case, check workspace by ID
wsbyid, othererr := sstore.GetWorkspace(nameOrID) // Note: workspaceName is ID in this case
if othererr != nil {
return nil, fmt.Errorf("no workspaces found with name or id %s", nameOrID)
}
if wsbyid != nil {
workspace = wsbyid
} else {
// Can this case happen?
return nil, fmt.Errorf("no workspaces found with name or id %s", nameOrID)
}
case 1:
workspace = &workspaces[0]
default:
return nil, fmt.Errorf("multiple workspaces found with name %s\n\nTry running the command by id instead of name:\n\tbrev command <id>", nameOrID)
}
if workspace == nil {
return nil, fmt.Errorf("no workspaces found with name or id %s", nameOrID)
}
// Get WorkspaceMetaData
workspaceMetaData, err := sstore.GetWorkspaceMetaData(workspace.ID)
if err != nil {
return nil, breverrors.WrapAndTrace(err)
}
return &entity.WorkspaceWithMeta{WorkspaceMetaData: *workspaceMetaData, Workspace: *workspace}, nil
}