From af2630030c33239a4508eb20e29569366518c983 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Tron=C3=AD=C4=8Dek?= Date: Sun, 29 Oct 2023 21:04:31 +0000 Subject: [PATCH] `gitpod workspace get` --- components/local-app/cmd/organization-list.go | 1 - components/local-app/cmd/workspace-get.go | 90 +++++++++++++++++++ components/local-app/cmd/workspace-stop.go | 1 + components/local-app/cmd/workspaces-list.go | 26 +++--- components/local-app/cmd/workspaces.go | 2 +- 5 files changed, 107 insertions(+), 13 deletions(-) create mode 100644 components/local-app/cmd/workspace-get.go diff --git a/components/local-app/cmd/organization-list.go b/components/local-app/cmd/organization-list.go index f7c47d7e04d73a..d102aedec2e57f 100644 --- a/components/local-app/cmd/organization-list.go +++ b/components/local-app/cmd/organization-list.go @@ -43,7 +43,6 @@ var listOrganizationCommand = &cobra.Command{ table.SetHeaderLine(false) for _, org := range orgs.Msg.GetTeams() { - table.Append([]string{org.Name, org.Id}) } diff --git a/components/local-app/cmd/workspace-get.go b/components/local-app/cmd/workspace-get.go new file mode 100644 index 00000000000000..d72acad2329973 --- /dev/null +++ b/components/local-app/cmd/workspace-get.go @@ -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 ", + 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) +} diff --git a/components/local-app/cmd/workspace-stop.go b/components/local-app/cmd/workspace-stop.go index 147b08614c33b6..4d8ae404819399 100644 --- a/components/local-app/cmd/workspace-stop.go +++ b/components/local-app/cmd/workspace-stop.go @@ -22,6 +22,7 @@ var stopNonBlocking = false var stopWorkspaceCommand = &cobra.Command{ Use: "stop ", Short: "Stop a given workspace", + Args: cobra.MaximumNArgs(1), RunE: func(cmd *cobra.Command, args []string) error { workspaceID := "" if len(args) < 1 { diff --git a/components/local-app/cmd/workspaces-list.go b/components/local-app/cmd/workspaces-list.go index feb59a6806ed18..d24849f949c156 100644 --- a/components/local-app/cmd/workspaces-list.go +++ b/components/local-app/cmd/workspaces-list.go @@ -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", @@ -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())}) diff --git a/components/local-app/cmd/workspaces.go b/components/local-app/cmd/workspaces.go index b5e745361baf8c..3872eed9927c99 100644 --- a/components/local-app/cmd/workspaces.go +++ b/components/local-app/cmd/workspaces.go @@ -11,7 +11,7 @@ import ( var wsCmd = &cobra.Command{ Use: "workspace", Short: "Interact with workspaces", - Aliases: []string{"workspaces"}, + Aliases: []string{"workspaces", "ws"}, } func init() {