From 22012041071ec68e6c897b7bd8031c92117128f0 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 14 Mar 2024 09:13:37 -0500 Subject: [PATCH 1/3] chore: 'coder org set' help message was incorrect --- cli/root.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/root.go b/cli/root.go index 4c6d5f213d846..8d04b1f3c8888 100644 --- a/cli/root.go +++ b/cli/root.go @@ -749,7 +749,7 @@ func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.C return org.IsDefault }) if index < 0 { - return codersdk.Organization{}, xerrors.Errorf("unable to determine current organization. Use 'coder set ' to select an organization to use") + return codersdk.Organization{}, xerrors.Errorf("unable to determine current organization. Use 'coder org set ' to select an organization to use") } return orgs[index], nil From e9195517d5483b01f3589bc232c4780b09d2a441 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 14 Mar 2024 09:24:00 -0500 Subject: [PATCH 2/3] fix: handler coder cli against older versions of Coder --- cli/organization_test.go | 43 ++++++++++++++++++++++++++++++++++++++++ cli/root.go | 7 +++++++ 2 files changed, 50 insertions(+) diff --git a/cli/organization_test.go b/cli/organization_test.go index 8258c847bdbd1..1375446ab0e1d 100644 --- a/cli/organization_test.go +++ b/cli/organization_test.go @@ -1,8 +1,14 @@ package cli_test import ( + "encoding/json" + "net/http" + "net/http/httptest" + "net/url" "testing" + "time" + "github.com/google/uuid" "github.com/stretchr/testify/require" "github.com/coder/coder/v2/cli/clitest" @@ -16,6 +22,36 @@ import ( func TestCurrentOrganization(t *testing.T) { t.Parallel() + // This test emulates 2 cases: + // 1. The user is not a part of the default organization, but only belongs to one. + // 2. The user is connecting to an older Coder instance. + t.Run("no-default", func(t *testing.T) { + orgID := uuid.New() + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + json.NewEncoder(w).Encode([]codersdk.Organization{ + { + ID: orgID, + Name: "not-default", + CreatedAt: time.Now(), + UpdatedAt: time.Now(), + IsDefault: false, + }, + }) + })) + defer srv.Close() + + client := codersdk.New(must(url.Parse(srv.URL))) + inv, root := clitest.New(t, "organizations", "show", "current") + clitest.SetupConfig(t, client, root) + pty := ptytest.New(t).Attach(inv) + errC := make(chan error) + go func() { + errC <- inv.Run() + }() + require.NoError(t, <-errC) + pty.ExpectMatch(orgID.String()) + }) + t.Run("OnlyID", func(t *testing.T) { t.Parallel() ownerClient := coderdtest.New(t, nil) @@ -108,3 +144,10 @@ func TestOrganizationSwitch(t *testing.T) { pty.ExpectMatch(exp.ID.String()) }) } + +func must[V any](v V, err error) V { + if err != nil { + panic(err) + } + return v +} diff --git a/cli/root.go b/cli/root.go index 8d04b1f3c8888..9696604217298 100644 --- a/cli/root.go +++ b/cli/root.go @@ -749,6 +749,13 @@ func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.C return org.IsDefault }) if index < 0 { + if len(orgs) == 1 { + // If there is no "isDefault", but only 1 org is present. We can just + // assume the single organization is correct. This is mainly a helper + // for cli hitting an old instance, or a user that belongs to a single + // org that is not the default. + return orgs[0], nil + } return codersdk.Organization{}, xerrors.Errorf("unable to determine current organization. Use 'coder org set ' to select an organization to use") } From 2fe650f6675205009b90b5371bd9f01b09644da5 Mon Sep 17 00:00:00 2001 From: Steven Masley Date: Thu, 14 Mar 2024 11:07:33 -0500 Subject: [PATCH 3/3] make test parallel --- cli/organization_test.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cli/organization_test.go b/cli/organization_test.go index 1375446ab0e1d..d5a9eeb057bfb 100644 --- a/cli/organization_test.go +++ b/cli/organization_test.go @@ -26,6 +26,8 @@ func TestCurrentOrganization(t *testing.T) { // 1. The user is not a part of the default organization, but only belongs to one. // 2. The user is connecting to an older Coder instance. t.Run("no-default", func(t *testing.T) { + t.Parallel() + orgID := uuid.New() srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode([]codersdk.Organization{