Skip to content

Commit

Permalink
gitpod workspace get
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptronicek committed Oct 29, 2023
1 parent 173e401 commit af26300
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 13 deletions.
1 change: 0 additions & 1 deletion components/local-app/cmd/organization-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ var listOrganizationCommand = &cobra.Command{
table.SetHeaderLine(false)

for _, org := range orgs.Msg.GetTeams() {

table.Append([]string{org.Name, org.Id})
}

Expand Down
90 changes: 90 additions & 0 deletions components/local-app/cmd/workspace-get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Copyright (c) 2023 Gitpod GmbH. All rights reserved.
// Licensed under the GNU Affero General Public License (AGPL).
// See License.AGPL.txt in the project root for license information.

package cmd

import (
"context"
"log/slog"
"os"
"time"

"github.com/bufbuild/connect-go"
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
"github.com/gitpod-io/local-app/pkg/common"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
)

// stopWorkspaceCommand stops to a given workspace
var getWorkspaceCommand = &cobra.Command{
Use: "get <workspace-id>",
Short: "Retrieves metadata of a given workspace",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
workspaceID := ""
if len(args) < 1 {
workspaceID = common.SelectWorkspace(cmd.Context(), nil)
} else {
workspaceID = args[0]
}

ctx, cancel := context.WithTimeout(cmd.Context(), 30*time.Second)
defer cancel()

gitpod, err := common.GetGitpodClient(ctx)
if err != nil {
return err
}

slog.Debug("Attempting to retrieve workspace info...")
ws, err := gitpod.Workspaces.GetWorkspace(ctx, connect.NewRequest(&v1.GetWorkspaceRequest{WorkspaceId: workspaceID}))

wsInfo := ws.Msg.GetResult()
repository := getWorkspaceRepo(wsInfo)
phase := TranslatePhase(wsInfo.Status.Instance.Status.Phase.String())

data := &infoData{
WorkspaceId: wsInfo.WorkspaceId,
WorkspaceUrl: wsInfo.Status.Instance.Status.Url,
Repository: repository,
Branch: wsInfo.Status.Instance.Status.GitStatus.Branch,
WorkspacePhase: phase,
// todo: LastActive, Created, WorkspaceClass (API implementation pending), RepoUrl (API implementation also pending)
}

outputInfo(data)

return err
},
}

type infoData struct {
WorkspaceId string `json:"workspace_id"`
WorkspaceUrl string `json:"workspace_url"`
Branch string `json:"branch"`
Repository string `json:"repository"`
WorkspacePhase string `json:"workspace_phase"`
}

func outputInfo(info *infoData) {
table := tablewriter.NewWriter(os.Stdout)
table.SetColWidth(50)
table.SetBorder(false)
table.SetColumnSeparator(":")
table.Append([]string{"ID", info.WorkspaceId})
table.Append([]string{"URL", info.WorkspaceUrl})
// Class
table.Append([]string{"Status", info.WorkspacePhase})
// Repo URL
table.Append([]string{"Repo", info.Repository})
table.Append([]string{"Branch", info.Branch})
// Created (date)
// Last Active (duration)
table.Render()
}

func init() {
wsCmd.AddCommand(getWorkspaceCommand)
}
1 change: 1 addition & 0 deletions components/local-app/cmd/workspace-stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var stopNonBlocking = false
var stopWorkspaceCommand = &cobra.Command{
Use: "stop <workspace-id>",
Short: "Stop a given workspace",
Args: cobra.MaximumNArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
workspaceID := ""
if len(args) < 1 {
Expand Down
26 changes: 15 additions & 11 deletions components/local-app/cmd/workspaces-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ func TranslatePhase(phase string) string {
return strings.ToLower(phase[6:])
}

func getWorkspaceRepo(ws *v1.Workspace) string {
repository := ""
wsDetails := ws.Context.GetDetails()
switch d := wsDetails.(type) {
case *v1.WorkspaceContext_Git_:
repository = fmt.Sprintf("%s/%s", d.Git.Repository.Owner, d.Git.Repository.Name)
case *v1.WorkspaceContext_Prebuild_:
repository = fmt.Sprintf("%s/%s", d.Prebuild.OriginalContext.Repository.Owner, d.Prebuild.OriginalContext.Repository.Name)
default:
slog.Warn("event", "could not determine repository for workspace", ws.WorkspaceId)
}
return repository
}

// listWorkspaceCommand lists all available workspaces
var listWorkspaceCommand = &cobra.Command{
Use: "list",
Expand All @@ -50,17 +64,7 @@ var listWorkspaceCommand = &cobra.Command{
table.SetHeaderLine(false)

for _, workspace := range workspaces.Msg.GetResult() {
repository := "n/a"
wsDetails := workspace.Context.GetDetails()
switch d := wsDetails.(type) {
case *v1.WorkspaceContext_Git_:
repository = fmt.Sprintf("%s/%s", d.Git.Repository.Owner, d.Git.Repository.Name)
case *v1.WorkspaceContext_Prebuild_:
repository = fmt.Sprintf("%s/%s", d.Prebuild.OriginalContext.Repository.Owner, d.Prebuild.OriginalContext.Repository.Name)
default:
slog.Warn("event", "could not determine repository for workspace", workspace.WorkspaceId)
}

repository := getWorkspaceRepo(workspace)
branch := workspace.GetStatus().Instance.Status.GitStatus.Branch

table.Append([]string{repository, branch, workspace.WorkspaceId, TranslatePhase(workspace.GetStatus().Instance.Status.Phase.String())})
Expand Down
2 changes: 1 addition & 1 deletion components/local-app/cmd/workspaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
var wsCmd = &cobra.Command{
Use: "workspace",
Short: "Interact with workspaces",
Aliases: []string{"workspaces"},
Aliases: []string{"workspaces", "ws"},
}

func init() {
Expand Down

0 comments on commit af26300

Please sign in to comment.