Skip to content

Commit

Permalink
feat: add project status check when opening (daytonaio#822)
Browse files Browse the repository at this point in the history
Signed-off-by: titanventura <[email protected]>
Signed-off-by: johnwick <[email protected]>
  • Loading branch information
titanventura authored and the-johnwick committed Aug 13, 2024
1 parent 8597452 commit c96b22a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
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

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
}

0 comments on commit c96b22a

Please sign in to comment.