diff --git a/components/local-app/cmd/organization-get.go b/components/local-app/cmd/organization-get.go index 0af54bcd2870a9..ddd80961760c50 100644 --- a/components/local-app/cmd/organization-get.go +++ b/components/local-app/cmd/organization-get.go @@ -38,13 +38,16 @@ var getOrganizationCommand = &cobra.Command{ Use: "get ", 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() diff --git a/components/local-app/cmd/root.go b/components/local-app/cmd/root.go index f84b06c163e430..0d4696d951544c 100644 --- a/components/local-app/cmd/root.go +++ b/components/local-app/cmd/root.go @@ -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", @@ -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 } diff --git a/components/local-app/cmd/workspace-create.go b/components/local-app/cmd/workspace-create.go index 872feb2ab89047..25d3c234ec40dd 100644 --- a/components/local-app/cmd/workspace-create.go +++ b/components/local-app/cmd/workspace-create.go @@ -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" ) @@ -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") } diff --git a/components/local-app/cmd/workspace-list.go b/components/local-app/cmd/workspace-list.go index f5c7a15580d007..5e101a64d69c5b 100644 --- a/components/local-app/cmd/workspace-list.go +++ b/components/local-app/cmd/workspace-list.go @@ -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 } diff --git a/components/local-app/pkg/common/common.go b/components/local-app/pkg/common/common.go index 8b5c1908c4e231..ba01dfcb7b560f 100644 --- a/components/local-app/pkg/common/common.go +++ b/components/local-app/pkg/common/common.go @@ -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 -} diff --git a/components/local-app/pkg/common/orgs.go b/components/local-app/pkg/common/orgs.go new file mode 100644 index 00000000000000..56f1bb5ebb9c3d --- /dev/null +++ b/components/local-app/pkg/common/orgs.go @@ -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 +} diff --git a/components/local-app/pkg/config/config.go b/components/local-app/pkg/config/config.go index 3eb3460dfe1bb3..bd1e2b109ce79a 100644 --- a/components/local-app/pkg/config/config.go +++ b/components/local-app/pkg/config/config.go @@ -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()