-
Notifications
You must be signed in to change notification settings - Fork 1.3k
feat(gitpod-cli): Create gp info
command
#13537
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) 2022 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" | ||
"encoding/json" | ||
"fmt" | ||
"github.com/gitpod-io/gitpod/common-go/log" | ||
supervisor_helper "github.com/gitpod-io/gitpod/gitpod-cli/pkg/supervisor-helper" | ||
supervisor "github.com/gitpod-io/gitpod/supervisor/api" | ||
"github.com/olekukonko/tablewriter" | ||
"github.com/spf13/cobra" | ||
"google.golang.org/grpc" | ||
"os" | ||
"time" | ||
) | ||
|
||
var infoCmdOpts struct { | ||
// Json configures whether the command output is printed as JSON, to make it machine-readable. | ||
Json bool | ||
} | ||
|
||
// infoCmd represents the info command. | ||
var infoCmd = &cobra.Command{ | ||
Use: "info", | ||
Short: "Display workspace info, such as its ID, class, etc.", | ||
Run: func(cmd *cobra.Command, args []string) { | ||
ctx, cancel := context.WithTimeout(cmd.Context(), 5*time.Second) | ||
defer cancel() | ||
|
||
conn, err := supervisor_helper.Dial(ctx) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
defer conn.Close() | ||
|
||
data, err := fetchInfo(ctx, conn) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
|
||
if infoCmdOpts.Json { | ||
content, _ := json.Marshal(data) | ||
fmt.Println(string(content)) | ||
return | ||
} | ||
|
||
outputInfo(data) | ||
}, | ||
} | ||
|
||
func fetchInfo(ctx context.Context, conn *grpc.ClientConn) (*infoData, error) { | ||
wsInfo, err := supervisor.NewInfoServiceClient(conn).WorkspaceInfo(ctx, &supervisor.WorkspaceInfoRequest{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to retrieve workspace info: %w", err) | ||
} | ||
|
||
return &infoData{ | ||
WorkspaceId: wsInfo.WorkspaceId, | ||
InstanceId: wsInfo.InstanceId, | ||
WorkspaceClass: wsInfo.WorkspaceClass, | ||
WorkspaceUrl: wsInfo.WorkspaceUrl, | ||
ClusterHost: wsInfo.WorkspaceClusterHost, | ||
}, nil | ||
} | ||
|
||
func outputInfo(info *infoData) { | ||
table := tablewriter.NewWriter(os.Stdout) | ||
table.SetColWidth(50) | ||
table.SetBorder(false) | ||
table.SetColumnSeparator(":") | ||
table.Append([]string{"Workspace ID", info.WorkspaceId}) | ||
table.Append([]string{"Instance ID", info.InstanceId}) | ||
table.Append([]string{"Workspace class", fmt.Sprintf("%s: %s", info.WorkspaceClass.DisplayName, info.WorkspaceClass.Description)}) | ||
table.Append([]string{"Workspace URL", info.WorkspaceUrl}) | ||
table.Append([]string{"Cluster host", info.ClusterHost}) | ||
table.Render() | ||
} | ||
|
||
type infoData struct { | ||
WorkspaceId string `json:"workspace_id"` | ||
InstanceId string `json:"instance_id"` | ||
WorkspaceClass *supervisor.WorkspaceInfoResponse_WorkspaceClass `json:"workspace_class"` | ||
WorkspaceUrl string `json:"workspace_url"` | ||
ClusterHost string `json:"cluster_host"` | ||
} | ||
|
||
func init() { | ||
infoCmd.Flags().BoolVarP(&infoCmdOpts.Json, "json", "j", false, "Output in JSON format") | ||
rootCmd.AddCommand(infoCmd) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I liked this formatting style. It looks neater compared to a regular table, I think we should consider it for
gp top
as well!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
gave it a go, what do you think of this:

Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when we extended the
top
cmd to include the workspace class, I wasn't convinced by the layout of mixing table + plain text, it would be nice to make class feel more part of the output. However, now we also havegp info
so maybe we don't need class ingp top
? I think if you open a draft PR we can collect feedback in there :)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good idea of moving the class to the table, raised a draft PR here: #13607