@@ -2,15 +2,10 @@ package reset
22
33import (
44 _ "embed"
5- "strings"
6- "time"
75
86 "github.com/brevdev/brev-cli/pkg/cmd/completions"
97 "github.com/brevdev/brev-cli/pkg/cmd/util"
10- "github.com/brevdev/brev-cli/pkg/config"
118 "github.com/brevdev/brev-cli/pkg/entity"
12- "github.com/brevdev/brev-cli/pkg/featureflag"
13- "github.com/brevdev/brev-cli/pkg/store"
149 "github.com/brevdev/brev-cli/pkg/terminal"
1510
1611 breverrors "github.com/brevdev/brev-cli/pkg/errors"
@@ -28,16 +23,9 @@ type ResetStore interface {
2823 completions.CompletionStore
2924 util.GetWorkspaceByNameOrIDErrStore
3025 ResetWorkspace (workspaceID string ) (* entity.Workspace , error )
31- GetActiveOrganizationOrDefault () (* entity.Organization , error )
32- GetCurrentUser () (* entity.User , error )
33- CreateWorkspace (organizationID string , options * store.CreateWorkspacesOptions ) (* entity.Workspace , error )
34- GetWorkspace (id string ) (* entity.Workspace , error )
35- DeleteWorkspace (workspaceID string ) (* entity.Workspace , error )
3626}
3727
3828func NewCmdReset (t * terminal.Terminal , loginResetStore ResetStore , noLoginResetStore ResetStore ) * cobra.Command {
39- var hardreset bool
40-
4129 cmd := & cobra.Command {
4230 Annotations : map [string ]string {"provider-dependent" : "" },
4331 Use : "reset" ,
@@ -48,192 +36,17 @@ func NewCmdReset(t *terminal.Terminal, loginResetStore ResetStore, noLoginResetS
4836 ValidArgsFunction : completions .GetAllWorkspaceNameCompletionHandler (noLoginResetStore , t ),
4937 RunE : func (cmd * cobra.Command , args []string ) error {
5038 for _ , arg := range args {
51- if hardreset {
52- err := hardResetProcess (arg , t , loginResetStore )
53- if err != nil {
54- return breverrors .WrapAndTrace (err )
55- }
56- } else {
57- err := resetWorkspace (arg , t , loginResetStore )
58- if err != nil {
59- return breverrors .WrapAndTrace (err )
60- }
39+ err := resetWorkspace (arg , t , loginResetStore )
40+ if err != nil {
41+ return breverrors .WrapAndTrace (err )
6142 }
6243 }
6344 return nil
6445 },
6546 }
66-
67- cmd .Flags ().BoolVarP (& hardreset , "hard" , "" , false , "DEPRECATED: use brev recreate" )
6847 return cmd
6948}
7049
71- // hardResetProcess deletes an existing workspace and creates a new one
72- func hardResetProcess (workspaceName string , t * terminal.Terminal , resetStore ResetStore ) error {
73- t .Vprint (t .Green ("Starting hard reset 🤙 " + t .Yellow ("This can take a couple of minutes.\n " )))
74- workspace , err := util .GetUserWorkspaceByNameOrIDErr (resetStore , workspaceName )
75- if err != nil {
76- return breverrors .WrapAndTrace (err )
77- }
78-
79- deletedWorkspace , err := resetStore .DeleteWorkspace (workspace .ID )
80- if err != nil {
81- return breverrors .WrapAndTrace (err )
82- }
83-
84- t .Vprint (t .Yellow ("Deleting instance - %s." , deletedWorkspace .Name ))
85- time .Sleep (10 * time .Second )
86-
87- if len (deletedWorkspace .GitRepo ) != 0 {
88- err := hardResetCreateWorkspaceFromRepo (t , resetStore , deletedWorkspace )
89- if err != nil {
90- return breverrors .WrapAndTrace (err )
91- }
92- } else {
93- err := hardResetCreateEmptyWorkspace (t , resetStore , deletedWorkspace )
94- if err != nil {
95- return breverrors .WrapAndTrace (err )
96- }
97- }
98- t .Vprint (t .Red ("NOTE: THIS COMMAND IS DEPRECATED" ))
99- t .Vprint (t .Red ("It still worked, but use brev recreate " + workspaceName + " next time" ))
100- return nil
101- }
102-
103- // hardResetCreateWorkspaceFromRepo clone a GIT repository, triggeres from the --hardreset flag
104- func hardResetCreateWorkspaceFromRepo (t * terminal.Terminal , resetStore ResetStore , workspace * entity.Workspace ) error {
105- t .Vprint (t .Green ("Instance is starting. " ) + t .Yellow ("This can take up to 2 minutes the first time." ))
106- var orgID string
107- activeorg , err := resetStore .GetActiveOrganizationOrDefault ()
108- if err != nil {
109- return breverrors .WrapAndTrace (err )
110- }
111- if activeorg == nil {
112- return breverrors .NewValidationError ("no org exist" )
113- }
114- orgID = activeorg .ID
115- clusterID := config .GlobalConfig .GetDefaultClusterID ()
116- options := store .NewCreateWorkspacesOptions (clusterID , workspace .Name ).WithGitRepo (workspace .GitRepo )
117-
118- user , err := resetStore .GetCurrentUser ()
119- if err != nil {
120- return breverrors .WrapAndTrace (err )
121- }
122-
123- options = resolveWorkspaceUserOptions (options , user )
124-
125- options .StartupScriptPath = workspace .StartupScriptPath
126- options .Execs = workspace .ExecsV0
127- options .Repos = workspace .ReposV0
128- options .IDEConfig = & workspace .IDEConfig
129-
130- w , err := resetStore .CreateWorkspace (orgID , options )
131- if err != nil {
132- return breverrors .WrapAndTrace (err )
133- }
134-
135- err = pollUntil (t , w .ID , entity .Running , resetStore , true )
136- if err != nil {
137- return breverrors .WrapAndTrace (err )
138- }
139-
140- t .Vprint (t .Green ("\n Your instance is ready!" ))
141- t .Vprintf ("%s" , t .Green ("\n SSH into your machine:\n \t ssh %s\n " , w .GetLocalIdentifier ()))
142- return nil
143- }
144-
145- // hardResetCreateEmptyWorkspace creates a new empty worksapce, triggered from the --hardreset flag
146- func hardResetCreateEmptyWorkspace (t * terminal.Terminal , resetStore ResetStore , workspace * entity.Workspace ) error {
147- t .Vprint (t .Green ("Instance is starting. " ) + t .Yellow ("This can take up to 2 minutes the first time.\n " ))
148-
149- // ensure name
150- if len (workspace .Name ) == 0 {
151- return breverrors .NewValidationError ("name field is required for empty instances" )
152- }
153-
154- // ensure org
155- var orgID string
156- activeorg , err := resetStore .GetActiveOrganizationOrDefault ()
157- if err != nil {
158- return breverrors .WrapAndTrace (err )
159- }
160- if activeorg == nil {
161- return breverrors .NewValidationError ("no org exist" )
162- }
163- orgID = activeorg .ID
164- clusterID := config .GlobalConfig .GetDefaultClusterID ()
165- options := store .NewCreateWorkspacesOptions (clusterID , workspace .Name )
166-
167- user , err := resetStore .GetCurrentUser ()
168- if err != nil {
169- return breverrors .WrapAndTrace (err )
170- }
171-
172- options = resolveWorkspaceUserOptions (options , user )
173-
174- options .StartupScriptPath = workspace .StartupScriptPath
175- options .Execs = workspace .ExecsV0
176- options .Repos = workspace .ReposV0
177- options .IDEConfig = & workspace .IDEConfig
178-
179- w , err := resetStore .CreateWorkspace (orgID , options )
180- if err != nil {
181- return breverrors .WrapAndTrace (err )
182- }
183-
184- err = pollUntil (t , w .ID , entity .Running , resetStore , true )
185- if err != nil {
186- return breverrors .WrapAndTrace (err )
187- }
188-
189- t .Vprint (t .Green ("\n Your instance is ready!" ))
190- t .Vprintf ("%s" , t .Green ("\n SSH into your machine:\n \t ssh %s\n " , w .GetLocalIdentifier ()))
191-
192- return nil
193- }
194-
195- func pollUntil (t * terminal.Terminal , wsid string , state string , resetStore ResetStore , canSafelyExit bool ) error {
196- s := t .NewSpinner ()
197- isReady := false
198- if canSafelyExit {
199- t .Vprintf ("You can safely ctrl+c to exit\n " )
200- }
201- s .Suffix = " hang tight 🤙"
202- s .Start ()
203- for ! isReady {
204- time .Sleep (5 * time .Second )
205- ws , err := resetStore .GetWorkspace (wsid )
206- if err != nil {
207- return breverrors .WrapAndTrace (err )
208- }
209- s .Suffix = " instance is " + strings .ToLower (ws .Status )
210- if ws .Status == state {
211- s .Suffix = "Instance is ready!"
212- s .Stop ()
213- isReady = true
214- }
215- }
216- return nil
217- }
218-
219- func resolveWorkspaceUserOptions (options * store.CreateWorkspacesOptions , user * entity.User ) * store.CreateWorkspacesOptions {
220- if options .WorkspaceTemplateID == "" {
221- if featureflag .IsAdmin (user .GlobalUserType ) {
222- options .WorkspaceTemplateID = store .DevWorkspaceTemplateID
223- } else {
224- options .WorkspaceTemplateID = store .UserWorkspaceTemplateID
225- }
226- }
227- if options .WorkspaceClassID == "" {
228- if featureflag .IsAdmin (user .GlobalUserType ) {
229- options .WorkspaceClassID = store .DevWorkspaceClassID
230- } else {
231- options .WorkspaceClassID = store .UserWorkspaceClassID
232- }
233- }
234- return options
235- }
236-
23750func resetWorkspace (workspaceName string , t * terminal.Terminal , resetStore ResetStore ) error {
23851 workspace , err := util .GetUserWorkspaceByNameOrIDErr (resetStore , workspaceName )
23952 if err != nil {
0 commit comments