Skip to content

Commit

Permalink
Infer orgs if applicable
Browse files Browse the repository at this point in the history
  • Loading branch information
filiptronicek committed Nov 2, 2023
1 parent 8ae8d00 commit 7be1671
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 53 deletions.
7 changes: 5 additions & 2 deletions components/local-app/cmd/organization-get.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,16 @@ var getOrganizationCommand = &cobra.Command{
Use: "get <organization-id>",
Short: "gets an organization's details",
RunE: func(cmd *cobra.Command, args []string) error {
orgId := ""
if len(args) < 1 {
orgId = common.SelectOrganization(cmd.Context())
orgId = getOrganizationId()
} else {
orgId = args[0]
}

if len(orgId) == 0 {
return cmd.Help()
}

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

Expand Down
27 changes: 27 additions & 0 deletions components/local-app/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@ import (
"log/slog"
"os"

"github.com/gitpod-io/local-app/pkg/common"
"github.com/gitpod-io/local-app/pkg/config"
"github.com/spf13/cobra"
)

var orgId string

var rootCmd = &cobra.Command{
Use: "gitpod",
Short: "A CLI for interacting with Gitpod",
Expand Down Expand Up @@ -42,4 +46,27 @@ func init() {
slog.Debug("Configured configuration and environment variables")

rootCmd.PersistentFlags().BoolP("verbose", "v", false, "Display verbose output for more detailed logging")
rootCmd.PersistentFlags().StringVarP(&orgId, "org-id", "o", "", "The organization to use")
}

func getOrganizationId() string {
if orgId != "" {
return orgId
}

orgFromConfig := config.GetString("org_id")
if orgFromConfig != "" {
return orgFromConfig
}

inferred, err := common.InferOrgId()
if err != nil {
slog.Warn("Could not get or infer an organization ID.", "error", err)
}

if inferred != "" {
slog.Debug("Inferred organization ID", "orgId", inferred)
}

return inferred
}
3 changes: 1 addition & 2 deletions components/local-app/cmd/workspace-create.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"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/gitpod-io/local-app/pkg/config"
"github.com/spf13/cobra"
)

Expand All @@ -34,7 +33,7 @@ var createWorkspaceCommand = &cobra.Command{
ctx, cancel := context.WithTimeout(cmd.Context(), 30*time.Second)
defer cancel()

orgId := config.GetOrganizationId()
orgId := getOrganizationId()
if len(orgId) == 0 {
return fmt.Errorf("No org specified. Specify an organization ID using the GITPOD_ORG_ID environment variable")
}
Expand Down
5 changes: 4 additions & 1 deletion components/local-app/cmd/workspace-list.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ var listWorkspaceCommand = &cobra.Command{
return err
}

workspaces, err := gitpod.Workspaces.ListWorkspaces(ctx, connect.NewRequest(&v1.ListWorkspacesRequest{}))
orgId := getOrganizationId()
workspaces, err := gitpod.Workspaces.ListWorkspaces(ctx, connect.NewRequest(&v1.ListWorkspacesRequest{
OrganizationId: orgId,
}))
if err != nil {
return err
}
Expand Down
44 changes: 0 additions & 44 deletions components/local-app/pkg/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,47 +156,3 @@ func SelectWorkspace(ctx context.Context, filter WorkspaceFilter) string {

return result
}

func SelectOrganization(ctx context.Context) string {
gitpod, err := GetGitpodClient(ctx)

if err != nil {
slog.ErrorContext(ctx, "Failed to setup Gitpod API client", err)
}

orgsList, err := gitpod.Teams.ListTeams(ctx, connect.NewRequest(&v1.ListTeamsRequest{}))

if err != nil {
slog.ErrorContext(ctx, "Failed to list organizations", err)
}

orgIds := []string{}

for _, org := range orgsList.Msg.GetTeams() {
orgIds = append(orgIds, org.Id)
}

if len(orgIds) == 0 {
slog.Error("No organization found")
return ""
}

if len(orgIds) == 1 {
slog.Debug("Only one organization found, automatically selecting it")
return orgIds[0]
}

prompt := promptui.Select{
Label: "Select a Workspace",
Items: orgIds,
}

_, result, err := prompt.Run()

if err != nil {
slog.ErrorContext(ctx, "Prompt failed", err)
return ""
}

return result
}
45 changes: 45 additions & 0 deletions components/local-app/pkg/common/orgs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 common

import (
"context"
"log/slog"

"github.com/bufbuild/connect-go"
v1 "github.com/gitpod-io/gitpod/components/public-api/go/experimental/v1"
)

// InferOrgId returns the organization ID to use if there is only one org available.
func InferOrgId() (string, error) {
ctx := context.Background()
gitpod, err := GetGitpodClient(ctx)
if err != nil {
slog.ErrorContext(ctx, "Failed to setup Gitpod API client", err)
}

orgsList, err := gitpod.Teams.ListTeams(ctx, connect.NewRequest(&v1.ListTeamsRequest{}))

if err != nil {
slog.ErrorContext(ctx, "Failed to list organizations", err)
}

orgIds := []string{}
for _, org := range orgsList.Msg.GetTeams() {
orgIds = append(orgIds, org.Id)
}

if len(orgIds) == 0 {
slog.Error("No organization found")
return "", nil
}

if len(orgIds) == 1 {
slog.Debug("Only one organization found, automatically selecting it")
return orgIds[0], nil
}

return "", nil
}
4 changes: 0 additions & 4 deletions components/local-app/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,6 @@ func GetGitpodUrl() string {
return fmt.Sprintf("https://%s", host)
}

func GetOrganizationId() string {
return GetString("org_id")
}

func Set(key string, value interface{}) error {
viper.Set(key, value)
return viper.WriteConfig()
Expand Down

0 comments on commit 7be1671

Please sign in to comment.