Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add workspace status check to code and ssh commands #821 #822

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions pkg/cmd/workspace/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/daytonaio/daytona/internal/util"
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/ide"
"github.com/daytonaio/daytona/pkg/views"
"github.com/daytonaio/daytona/pkg/views/workspace/selection"
Expand Down Expand Up @@ -98,12 +99,14 @@ var CodeCmd = &cobra.Command{
}
}

views.RenderInfoMessage(fmt.Sprintf("Opening the project '%s' from workspace '%s' in %s", projectName, *workspace.Name, ideName))

providerMetadata := ""
if workspace.Info != nil {
for _, project := range workspace.Info.Projects {
if *project.Name == projectName {
if !project.GetIsRunning() {
views.RenderInfoMessage(fmt.Sprintf("Project '%s' from workspace '%s' is not in running state", projectName, *workspace.Name))
return
}
if project.ProviderMetadata == nil {
log.Fatal(errors.New("project provider metadata is missing"))
}
Expand All @@ -113,6 +116,13 @@ var CodeCmd = &cobra.Command{
}
}

if !workspace_util.IsProjectRunning(workspace, projectName) {
views.RenderInfoMessage(fmt.Sprintf("Project '%s' from workspace '%s' is not in running state", projectName, *workspace.Name))
return
}

views.RenderInfoMessage(fmt.Sprintf("Opening the project '%s' from workspace '%s' in %s", projectName, *workspace.Name, ideName))

err = openIDE(ideId, activeProfile, workspaceId, projectName, providerMetadata)
if err != nil {
log.Fatal(err)
Expand Down
27 changes: 17 additions & 10 deletions pkg/cmd/workspace/ssh.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ package workspace

import (
"context"
"fmt"

"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/ide"
"github.com/daytonaio/daytona/pkg/views"
"github.com/daytonaio/daytona/pkg/views/workspace/selection"

log "github.com/sirupsen/logrus"
Expand All @@ -33,35 +37,33 @@ var SshCmd = &cobra.Command{
}

ctx := context.Background()
var workspaceId string
var workspace *apiclient.WorkspaceDTO
var projectName string

apiClient, err := apiclient.GetApiClient(&activeProfile)
apiClient, err := apiclient_util.GetApiClient(&activeProfile)
if err != nil {
log.Fatal(err)
}

if len(args) == 0 {
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, "SSH Into")
workspace = selection.GetWorkspaceFromPrompt(workspaceList, "SSH Into")
if workspace == nil {
return
}
workspaceId = *workspace.Id
} else {
workspace, err := apiclient.GetWorkspace(args[0])
workspace, err = apiclient_util.GetWorkspace(args[0])
if err != nil {
log.Fatal(err)
}
workspaceId = *workspace.Id
}

if len(args) == 0 || len(args) == 1 {
selectedProject, err := selectWorkspaceProject(workspaceId, &activeProfile)
selectedProject, err := selectWorkspaceProject(*workspace.Id, &activeProfile)
if err != nil {
log.Fatal(err)
}
Expand All @@ -75,7 +77,12 @@ var SshCmd = &cobra.Command{
projectName = args[1]
}

err = ide.OpenTerminalSsh(activeProfile, workspaceId, projectName)
if !workspace_util.IsProjectRunning(workspace, projectName) {
views.RenderInfoMessage(fmt.Sprintf("Project '%s' from workspace '%s' is not in running state", projectName, *workspace.Name))
return
}

err = ide.OpenTerminalSsh(activeProfile, *workspace.Id, projectName)
if err != nil {
log.Fatal(err)
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/cmd/workspace/util/workspace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2024 Daytona Platforms Inc.
// SPDX-License-Identifier: Apache-2.0

package util
Tpuljak marked this conversation as resolved.
Show resolved Hide resolved

import "github.com/daytonaio/daytona/pkg/apiclient"

func IsProjectRunning(workspace *apiclient.WorkspaceDTO, projectName string) bool {
for _, project := range workspace.GetProjects() {
if project.GetName() == projectName {
uptime := project.GetState().Uptime
return uptime != nil && *uptime != 0
}
}
return false
}
Loading