Skip to content

Commit 06d2cb9

Browse files
authored
feat: add spinner to workspace start (#1072)
Signed-off-by: tarunrajput <[email protected]>
1 parent 03e9d34 commit 06d2cb9

File tree

8 files changed

+173
-97
lines changed

8 files changed

+173
-97
lines changed

pkg/cmd/target/remove.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/daytonaio/daytona/cmd/daytona/config"
1212
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
1313
"github.com/daytonaio/daytona/pkg/apiclient"
14+
workspace_cmd "github.com/daytonaio/daytona/pkg/cmd/workspace"
1415
"github.com/daytonaio/daytona/pkg/common"
1516
"github.com/daytonaio/daytona/pkg/views"
1617
"github.com/daytonaio/daytona/pkg/views/target"
@@ -120,14 +121,13 @@ func RemoveTargetWorkspaces(ctx context.Context, client *apiclient.APIClient, ta
120121
if workspace.Target != target {
121122
continue
122123
}
123-
124-
res, err := client.WorkspaceAPI.RemoveWorkspace(ctx, workspace.Id).Execute()
124+
err := workspace_cmd.RemoveWorkspace(ctx, client, &workspace, false)
125125
if err != nil {
126-
log.Errorf("Failed to delete workspace %s: %v", workspace.Name, apiclient_util.HandleErrorResponse(res, err))
126+
log.Errorf("Failed to delete workspace %s: %v", workspace.Name, err)
127127
continue
128128
}
129129

130-
views.RenderLine(fmt.Sprintf("- Workspace %s successfully deleted\n", workspace.Name))
130+
views.RenderInfoMessage(fmt.Sprintf("- Workspace '%s' successfully deleted", workspace.Name))
131131
}
132132

133133
return nil

pkg/cmd/workspace/code.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/daytonaio/daytona/pkg/ide"
2020
"github.com/daytonaio/daytona/pkg/server/workspaces"
2121
"github.com/daytonaio/daytona/pkg/telemetry"
22-
"github.com/daytonaio/daytona/pkg/views"
2322
ide_views "github.com/daytonaio/daytona/pkg/views/ide"
2423
"github.com/daytonaio/daytona/pkg/views/workspace/selection"
2524

@@ -215,12 +214,9 @@ func AutoStartWorkspace(workspaceId string, projectName string) (bool, error) {
215214
return false, err
216215
}
217216

218-
views.RenderInfoMessage(fmt.Sprintf("Project '%s' from workspace '%s' is starting", projectName, workspaceId))
219-
220-
ctx := context.Background()
221-
res, err := apiClient.WorkspaceAPI.StartProject(ctx, workspaceId, projectName).Execute()
217+
err = StartWorkspace(apiClient, workspaceId, projectName)
222218
if err != nil {
223-
return false, apiclient_util.HandleErrorResponse(res, err)
219+
return false, err
224220
}
225221

226222
return true, nil

pkg/cmd/workspace/delete.go

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
1515
"github.com/daytonaio/daytona/pkg/apiclient"
1616
"github.com/daytonaio/daytona/pkg/views"
17+
views_util "github.com/daytonaio/daytona/pkg/views/util"
1718
"github.com/daytonaio/daytona/pkg/views/workspace/selection"
1819

1920
log "github.com/sirupsen/logrus"
@@ -117,10 +118,11 @@ var DeleteCmd = &cobra.Command{
117118
fmt.Println("Operation canceled.")
118119
} else {
119120
for _, workspace := range workspaceDeleteList {
120-
err := removeWorkspace(ctx, apiClient, workspace, forceFlag)
121+
err := RemoveWorkspace(ctx, apiClient, workspace, forceFlag)
121122
if err != nil {
122123
log.Error(fmt.Sprintf("[ %s ] : %v", workspace.Name, err))
123124
}
125+
views.RenderInfoMessage(fmt.Sprintf("Workspace '%s' successfully deleted", workspace.Name))
124126
}
125127
}
126128
},
@@ -152,38 +154,43 @@ func DeleteAllWorkspaces(force bool) error {
152154
}
153155

154156
for _, workspace := range workspaceList {
155-
res, err := apiClient.WorkspaceAPI.RemoveWorkspace(ctx, workspace.Id).Force(force).Execute()
157+
err := RemoveWorkspace(ctx, apiClient, &workspace, force)
156158
if err != nil {
157-
log.Errorf("Failed to delete workspace %s: %v", workspace.Name, apiclient_util.HandleErrorResponse(res, err))
159+
log.Errorf("Failed to delete workspace %s: %v", workspace.Name, err)
158160
continue
159161
}
160-
views.RenderLine(fmt.Sprintf("- Workspace %s successfully deleted\n", workspace.Name))
162+
views.RenderInfoMessage(fmt.Sprintf("- Workspace '%s' successfully deleted", workspace.Name))
161163
}
162164
return nil
163165
}
164166

165-
func removeWorkspace(ctx context.Context, apiClient *apiclient.APIClient, workspace *apiclient.WorkspaceDTO, force bool) error {
166-
res, err := apiClient.WorkspaceAPI.RemoveWorkspace(ctx, workspace.Id).Force(force).Execute()
167-
168-
if err != nil {
169-
return apiclient_util.HandleErrorResponse(res, err)
170-
}
167+
func RemoveWorkspace(ctx context.Context, apiClient *apiclient.APIClient, workspace *apiclient.WorkspaceDTO, force bool) error {
168+
message := fmt.Sprintf("Deleting workspace %s", workspace.Name)
169+
err := views_util.WithInlineSpinner(message, func() error {
170+
res, err := apiClient.WorkspaceAPI.RemoveWorkspace(ctx, workspace.Id).Force(force).Execute()
171+
if err != nil {
172+
return apiclient_util.HandleErrorResponse(res, err)
173+
}
174+
c, err := config.GetConfig()
175+
if err != nil {
176+
return err
177+
}
171178

172-
c, err := config.GetConfig()
173-
if err != nil {
174-
return err
175-
}
179+
activeProfile, err := c.GetActiveProfile()
180+
if err != nil {
181+
return err
182+
}
176183

177-
activeProfile, err := c.GetActiveProfile()
178-
if err != nil {
179-
return err
180-
}
184+
err = config.RemoveWorkspaceSshEntries(activeProfile.Id, workspace.Id)
185+
if err != nil {
186+
return err
187+
}
188+
return nil
189+
})
181190

182-
err = config.RemoveWorkspaceSshEntries(activeProfile.Id, workspace.Id)
183191
if err != nil {
184192
return err
185193
}
186194

187-
views.RenderInfoMessage(fmt.Sprintf("Workspace %s successfully deleted", workspace.Name))
188195
return nil
189196
}

pkg/cmd/workspace/start.go

Lines changed: 55 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ import (
99

1010
"github.com/daytonaio/daytona/cmd/daytona/config"
1111
"github.com/daytonaio/daytona/internal/util"
12-
"github.com/daytonaio/daytona/internal/util/apiclient"
12+
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
13+
"github.com/daytonaio/daytona/pkg/apiclient"
1314
workspace_util "github.com/daytonaio/daytona/pkg/cmd/workspace/util"
1415
"github.com/daytonaio/daytona/pkg/views"
1516
ide_views "github.com/daytonaio/daytona/pkg/views/ide"
17+
views_util "github.com/daytonaio/daytona/pkg/views/util"
1618
"github.com/daytonaio/daytona/pkg/views/workspace/selection"
1719

1820
log "github.com/sirupsen/logrus"
@@ -36,7 +38,6 @@ var StartCmd = &cobra.Command{
3638
GroupID: util.WORKSPACE_GROUP,
3739
Run: func(cmd *cobra.Command, args []string) {
3840
var workspaceIdOrName string
39-
var message string
4041
var activeProfile config.Profile
4142
var ideId string
4243
var workspaceId string
@@ -53,7 +54,7 @@ var StartCmd = &cobra.Command{
5354

5455
ctx := context.Background()
5556

56-
apiClient, err := apiclient.GetApiClient(nil)
57+
apiClient, err := apiclient_util.GetApiClient(nil)
5758
if err != nil {
5859
log.Fatal(err)
5960
}
@@ -68,7 +69,7 @@ var StartCmd = &cobra.Command{
6869
}
6970
workspaceList, res, err := apiClient.WorkspaceAPI.ListWorkspaces(ctx).Execute()
7071
if err != nil {
71-
log.Fatal(apiclient.HandleErrorResponse(res, err))
72+
log.Fatal(apiclient_util.HandleErrorResponse(res, err))
7273
}
7374

7475
workspace := selection.GetWorkspaceFromPrompt(workspaceList, "Start")
@@ -96,7 +97,7 @@ var StartCmd = &cobra.Command{
9697

9798
wsInfo, res, err := apiClient.WorkspaceAPI.GetWorkspace(ctx, workspaceIdOrName).Execute()
9899
if err != nil {
99-
log.Fatal(apiclient.HandleErrorResponse(res, err))
100+
log.Fatal(apiclient_util.HandleErrorResponse(res, err))
100101
}
101102
workspaceId = wsInfo.Id
102103
if startProjectFlag == "" {
@@ -110,22 +111,15 @@ var StartCmd = &cobra.Command{
110111
}
111112
}
112113

113-
if startProjectFlag == "" {
114-
message = fmt.Sprintf("Workspace '%s' is starting", workspaceIdOrName)
115-
res, err := apiClient.WorkspaceAPI.StartWorkspace(ctx, workspaceIdOrName).Execute()
116-
if err != nil {
117-
log.Fatal(apiclient.HandleErrorResponse(res, err))
118-
}
114+
err = StartWorkspace(apiClient, workspaceIdOrName, startProjectFlag)
115+
if err != nil {
116+
log.Fatal(err)
117+
}
119118

120-
views.RenderInfoMessage(message)
119+
if startProjectFlag == "" {
120+
views.RenderInfoMessage(fmt.Sprintf("Workspace '%s' started successfully", workspaceIdOrName))
121121
} else {
122-
message = fmt.Sprintf("Project '%s' from workspace '%s' is starting", startProjectFlag, workspaceIdOrName)
123-
res, err := apiClient.WorkspaceAPI.StartProject(ctx, workspaceIdOrName, startProjectFlag).Execute()
124-
if err != nil {
125-
log.Fatal(apiclient.HandleErrorResponse(res, err))
126-
}
127-
128-
views.RenderInfoMessage(message)
122+
views.RenderInfoMessage(fmt.Sprintf("Project '%s' from workspace '%s' started successfully", startProjectFlag, workspaceIdOrName))
129123

130124
if codeFlag {
131125
ide_views.RenderIdeOpeningMessage(workspaceIdOrName, startProjectFlag, ideId, ideList)
@@ -159,30 +153,31 @@ func init() {
159153

160154
func startAllWorkspaces() error {
161155
ctx := context.Background()
162-
apiClient, err := apiclient.GetApiClient(nil)
156+
apiClient, err := apiclient_util.GetApiClient(nil)
163157
if err != nil {
164158
return err
165159
}
166160

167161
workspaceList, res, err := apiClient.WorkspaceAPI.ListWorkspaces(ctx).Execute()
168162
if err != nil {
169-
return apiclient.HandleErrorResponse(res, err)
163+
return apiclient_util.HandleErrorResponse(res, err)
170164
}
171165

172166
for _, workspace := range workspaceList {
173-
res, err := apiClient.WorkspaceAPI.StartWorkspace(ctx, workspace.Id).Execute()
167+
err := StartWorkspace(apiClient, workspace.Name, "")
174168
if err != nil {
175-
log.Errorf("Failed to start workspace %s: %v", workspace.Name, apiclient.HandleErrorResponse(res, err))
169+
log.Errorf("Failed to start workspace %s: %v\n\n", workspace.Name, err)
176170
continue
177171
}
178-
fmt.Printf("Workspace '%s' is starting\n", workspace.Name)
172+
173+
views.RenderInfoMessage(fmt.Sprintf("- Workspace '%s' started successfully", workspace.Name))
179174
}
180175
return nil
181176
}
182177

183178
func getProjectNameCompletions(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
184179
ctx := context.Background()
185-
apiClient, err := apiclient.GetApiClient(nil)
180+
apiClient, err := apiclient_util.GetApiClient(nil)
186181
if err != nil {
187182
return nil, cobra.ShellCompDirectiveDefault
188183
}
@@ -202,7 +197,7 @@ func getProjectNameCompletions(cmd *cobra.Command, args []string, toComplete str
202197

203198
func getWorkspaceNameCompletions() ([]string, cobra.ShellCompDirective) {
204199
ctx := context.Background()
205-
apiClient, err := apiclient.GetApiClient(nil)
200+
apiClient, err := apiclient_util.GetApiClient(nil)
206201
if err != nil {
207202
return nil, cobra.ShellCompDirectiveNoFileComp
208203
}
@@ -222,7 +217,7 @@ func getWorkspaceNameCompletions() ([]string, cobra.ShellCompDirective) {
222217

223218
func getAllWorkspacesByState(state WorkspaceState) ([]string, cobra.ShellCompDirective) {
224219
ctx := context.Background()
225-
apiClient, err := apiclient.GetApiClient(nil)
220+
apiClient, err := apiclient_util.GetApiClient(nil)
226221
if err != nil {
227222
return nil, cobra.ShellCompDirectiveNoFileComp
228223
}
@@ -248,3 +243,36 @@ func getAllWorkspacesByState(state WorkspaceState) ([]string, cobra.ShellCompDir
248243

249244
return choices, cobra.ShellCompDirectiveNoFileComp
250245
}
246+
247+
func StartWorkspace(apiClient *apiclient.APIClient, workspaceId, projectName string) error {
248+
ctx := context.Background()
249+
var message string
250+
var startFunc func() error
251+
252+
if projectName == "" {
253+
message = fmt.Sprintf("Workspace '%s' is starting", workspaceId)
254+
startFunc = func() error {
255+
res, err := apiClient.WorkspaceAPI.StartWorkspace(ctx, workspaceId).Execute()
256+
if err != nil {
257+
return apiclient_util.HandleErrorResponse(res, err)
258+
}
259+
return nil
260+
}
261+
} else {
262+
message = fmt.Sprintf("Project '%s' from workspace '%s' is starting", projectName, workspaceId)
263+
startFunc = func() error {
264+
res, err := apiClient.WorkspaceAPI.StartProject(ctx, workspaceId, projectName).Execute()
265+
if err != nil {
266+
return apiclient_util.HandleErrorResponse(res, err)
267+
}
268+
return nil
269+
}
270+
}
271+
272+
err := views_util.WithInlineSpinner(message, startFunc)
273+
if err != nil {
274+
return err
275+
}
276+
277+
return nil
278+
}

0 commit comments

Comments
 (0)