Skip to content

Commit

Permalink
feat: add spinner to workspace start (#1072)
Browse files Browse the repository at this point in the history
Signed-off-by: tarunrajput <[email protected]>
  • Loading branch information
tarunrajput authored Sep 12, 2024
1 parent 03e9d34 commit 06d2cb9
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 97 deletions.
8 changes: 4 additions & 4 deletions pkg/cmd/target/remove.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/daytonaio/daytona/cmd/daytona/config"
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
"github.com/daytonaio/daytona/pkg/apiclient"
workspace_cmd "github.com/daytonaio/daytona/pkg/cmd/workspace"
"github.com/daytonaio/daytona/pkg/common"
"github.com/daytonaio/daytona/pkg/views"
"github.com/daytonaio/daytona/pkg/views/target"
Expand Down Expand Up @@ -120,14 +121,13 @@ func RemoveTargetWorkspaces(ctx context.Context, client *apiclient.APIClient, ta
if workspace.Target != target {
continue
}

res, err := client.WorkspaceAPI.RemoveWorkspace(ctx, workspace.Id).Execute()
err := workspace_cmd.RemoveWorkspace(ctx, client, &workspace, false)
if err != nil {
log.Errorf("Failed to delete workspace %s: %v", workspace.Name, apiclient_util.HandleErrorResponse(res, err))
log.Errorf("Failed to delete workspace %s: %v", workspace.Name, err)
continue
}

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

return nil
Expand Down
8 changes: 2 additions & 6 deletions pkg/cmd/workspace/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import (
"github.com/daytonaio/daytona/pkg/ide"
"github.com/daytonaio/daytona/pkg/server/workspaces"
"github.com/daytonaio/daytona/pkg/telemetry"
"github.com/daytonaio/daytona/pkg/views"
ide_views "github.com/daytonaio/daytona/pkg/views/ide"
"github.com/daytonaio/daytona/pkg/views/workspace/selection"

Expand Down Expand Up @@ -215,12 +214,9 @@ func AutoStartWorkspace(workspaceId string, projectName string) (bool, error) {
return false, err
}

views.RenderInfoMessage(fmt.Sprintf("Project '%s' from workspace '%s' is starting", projectName, workspaceId))

ctx := context.Background()
res, err := apiClient.WorkspaceAPI.StartProject(ctx, workspaceId, projectName).Execute()
err = StartWorkspace(apiClient, workspaceId, projectName)
if err != nil {
return false, apiclient_util.HandleErrorResponse(res, err)
return false, err
}

return true, nil
Expand Down
47 changes: 27 additions & 20 deletions pkg/cmd/workspace/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
"github.com/daytonaio/daytona/pkg/apiclient"
"github.com/daytonaio/daytona/pkg/views"
views_util "github.com/daytonaio/daytona/pkg/views/util"
"github.com/daytonaio/daytona/pkg/views/workspace/selection"

log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -117,10 +118,11 @@ var DeleteCmd = &cobra.Command{
fmt.Println("Operation canceled.")
} else {
for _, workspace := range workspaceDeleteList {
err := removeWorkspace(ctx, apiClient, workspace, forceFlag)
err := RemoveWorkspace(ctx, apiClient, workspace, forceFlag)
if err != nil {
log.Error(fmt.Sprintf("[ %s ] : %v", workspace.Name, err))
}
views.RenderInfoMessage(fmt.Sprintf("Workspace '%s' successfully deleted", workspace.Name))
}
}
},
Expand Down Expand Up @@ -152,38 +154,43 @@ func DeleteAllWorkspaces(force bool) error {
}

for _, workspace := range workspaceList {
res, err := apiClient.WorkspaceAPI.RemoveWorkspace(ctx, workspace.Id).Force(force).Execute()
err := RemoveWorkspace(ctx, apiClient, &workspace, force)
if err != nil {
log.Errorf("Failed to delete workspace %s: %v", workspace.Name, apiclient_util.HandleErrorResponse(res, err))
log.Errorf("Failed to delete workspace %s: %v", workspace.Name, err)
continue
}
views.RenderLine(fmt.Sprintf("- Workspace %s successfully deleted\n", workspace.Name))
views.RenderInfoMessage(fmt.Sprintf("- Workspace '%s' successfully deleted", workspace.Name))
}
return nil
}

func removeWorkspace(ctx context.Context, apiClient *apiclient.APIClient, workspace *apiclient.WorkspaceDTO, force bool) error {
res, err := apiClient.WorkspaceAPI.RemoveWorkspace(ctx, workspace.Id).Force(force).Execute()

if err != nil {
return apiclient_util.HandleErrorResponse(res, err)
}
func RemoveWorkspace(ctx context.Context, apiClient *apiclient.APIClient, workspace *apiclient.WorkspaceDTO, force bool) error {
message := fmt.Sprintf("Deleting workspace %s", workspace.Name)
err := views_util.WithInlineSpinner(message, func() error {
res, err := apiClient.WorkspaceAPI.RemoveWorkspace(ctx, workspace.Id).Force(force).Execute()
if err != nil {
return apiclient_util.HandleErrorResponse(res, err)
}
c, err := config.GetConfig()
if err != nil {
return err
}

c, err := config.GetConfig()
if err != nil {
return err
}
activeProfile, err := c.GetActiveProfile()
if err != nil {
return err
}

activeProfile, err := c.GetActiveProfile()
if err != nil {
return err
}
err = config.RemoveWorkspaceSshEntries(activeProfile.Id, workspace.Id)
if err != nil {
return err
}
return nil
})

err = config.RemoveWorkspaceSshEntries(activeProfile.Id, workspace.Id)
if err != nil {
return err
}

views.RenderInfoMessage(fmt.Sprintf("Workspace %s successfully deleted", workspace.Name))
return nil
}
82 changes: 55 additions & 27 deletions pkg/cmd/workspace/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import (

"github.com/daytonaio/daytona/cmd/daytona/config"
"github.com/daytonaio/daytona/internal/util"
"github.com/daytonaio/daytona/internal/util/apiclient"
apiclient_util "github.com/daytonaio/daytona/internal/util/apiclient"
"github.com/daytonaio/daytona/pkg/apiclient"
workspace_util "github.com/daytonaio/daytona/pkg/cmd/workspace/util"
"github.com/daytonaio/daytona/pkg/views"
ide_views "github.com/daytonaio/daytona/pkg/views/ide"
views_util "github.com/daytonaio/daytona/pkg/views/util"
"github.com/daytonaio/daytona/pkg/views/workspace/selection"

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

ctx := context.Background()

apiClient, err := apiclient.GetApiClient(nil)
apiClient, err := apiclient_util.GetApiClient(nil)
if err != nil {
log.Fatal(err)
}
Expand All @@ -68,7 +69,7 @@ var StartCmd = &cobra.Command{
}
workspaceList, res, err := apiClient.WorkspaceAPI.ListWorkspaces(ctx).Execute()
if err != nil {
log.Fatal(apiclient.HandleErrorResponse(res, err))
log.Fatal(apiclient_util.HandleErrorResponse(res, err))
}

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

wsInfo, res, err := apiClient.WorkspaceAPI.GetWorkspace(ctx, workspaceIdOrName).Execute()
if err != nil {
log.Fatal(apiclient.HandleErrorResponse(res, err))
log.Fatal(apiclient_util.HandleErrorResponse(res, err))
}
workspaceId = wsInfo.Id
if startProjectFlag == "" {
Expand All @@ -110,22 +111,15 @@ var StartCmd = &cobra.Command{
}
}

if startProjectFlag == "" {
message = fmt.Sprintf("Workspace '%s' is starting", workspaceIdOrName)
res, err := apiClient.WorkspaceAPI.StartWorkspace(ctx, workspaceIdOrName).Execute()
if err != nil {
log.Fatal(apiclient.HandleErrorResponse(res, err))
}
err = StartWorkspace(apiClient, workspaceIdOrName, startProjectFlag)
if err != nil {
log.Fatal(err)
}

views.RenderInfoMessage(message)
if startProjectFlag == "" {
views.RenderInfoMessage(fmt.Sprintf("Workspace '%s' started successfully", workspaceIdOrName))
} else {
message = fmt.Sprintf("Project '%s' from workspace '%s' is starting", startProjectFlag, workspaceIdOrName)
res, err := apiClient.WorkspaceAPI.StartProject(ctx, workspaceIdOrName, startProjectFlag).Execute()
if err != nil {
log.Fatal(apiclient.HandleErrorResponse(res, err))
}

views.RenderInfoMessage(message)
views.RenderInfoMessage(fmt.Sprintf("Project '%s' from workspace '%s' started successfully", startProjectFlag, workspaceIdOrName))

if codeFlag {
ide_views.RenderIdeOpeningMessage(workspaceIdOrName, startProjectFlag, ideId, ideList)
Expand Down Expand Up @@ -159,30 +153,31 @@ func init() {

func startAllWorkspaces() error {
ctx := context.Background()
apiClient, err := apiclient.GetApiClient(nil)
apiClient, err := apiclient_util.GetApiClient(nil)
if err != nil {
return err
}

workspaceList, res, err := apiClient.WorkspaceAPI.ListWorkspaces(ctx).Execute()
if err != nil {
return apiclient.HandleErrorResponse(res, err)
return apiclient_util.HandleErrorResponse(res, err)
}

for _, workspace := range workspaceList {
res, err := apiClient.WorkspaceAPI.StartWorkspace(ctx, workspace.Id).Execute()
err := StartWorkspace(apiClient, workspace.Name, "")
if err != nil {
log.Errorf("Failed to start workspace %s: %v", workspace.Name, apiclient.HandleErrorResponse(res, err))
log.Errorf("Failed to start workspace %s: %v\n\n", workspace.Name, err)
continue
}
fmt.Printf("Workspace '%s' is starting\n", workspace.Name)

views.RenderInfoMessage(fmt.Sprintf("- Workspace '%s' started successfully", workspace.Name))
}
return nil
}

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

func getWorkspaceNameCompletions() ([]string, cobra.ShellCompDirective) {
ctx := context.Background()
apiClient, err := apiclient.GetApiClient(nil)
apiClient, err := apiclient_util.GetApiClient(nil)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
Expand All @@ -222,7 +217,7 @@ func getWorkspaceNameCompletions() ([]string, cobra.ShellCompDirective) {

func getAllWorkspacesByState(state WorkspaceState) ([]string, cobra.ShellCompDirective) {
ctx := context.Background()
apiClient, err := apiclient.GetApiClient(nil)
apiClient, err := apiclient_util.GetApiClient(nil)
if err != nil {
return nil, cobra.ShellCompDirectiveNoFileComp
}
Expand All @@ -248,3 +243,36 @@ func getAllWorkspacesByState(state WorkspaceState) ([]string, cobra.ShellCompDir

return choices, cobra.ShellCompDirectiveNoFileComp
}

func StartWorkspace(apiClient *apiclient.APIClient, workspaceId, projectName string) error {
ctx := context.Background()
var message string
var startFunc func() error

if projectName == "" {
message = fmt.Sprintf("Workspace '%s' is starting", workspaceId)
startFunc = func() error {
res, err := apiClient.WorkspaceAPI.StartWorkspace(ctx, workspaceId).Execute()
if err != nil {
return apiclient_util.HandleErrorResponse(res, err)
}
return nil
}
} else {
message = fmt.Sprintf("Project '%s' from workspace '%s' is starting", projectName, workspaceId)
startFunc = func() error {
res, err := apiClient.WorkspaceAPI.StartProject(ctx, workspaceId, projectName).Execute()
if err != nil {
return apiclient_util.HandleErrorResponse(res, err)
}
return nil
}
}

err := views_util.WithInlineSpinner(message, startFunc)
if err != nil {
return err
}

return nil
}
Loading

0 comments on commit 06d2cb9

Please sign in to comment.