Skip to content

Commit 29c8cf2

Browse files
authored
fix: handle CLI default organization when none exists in <v2.9.0 coderd (coder#12594)
* chore: 'coder org set' help message was incorrect * fix: handler coder cli against older versions of Coder
1 parent f78b5c1 commit 29c8cf2

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

cli/organization_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package cli_test
22

33
import (
4+
"encoding/json"
5+
"net/http"
6+
"net/http/httptest"
7+
"net/url"
48
"testing"
9+
"time"
510

11+
"github.com/google/uuid"
612
"github.com/stretchr/testify/require"
713

814
"github.com/coder/coder/v2/cli/clitest"
@@ -16,6 +22,38 @@ import (
1622
func TestCurrentOrganization(t *testing.T) {
1723
t.Parallel()
1824

25+
// This test emulates 2 cases:
26+
// 1. The user is not a part of the default organization, but only belongs to one.
27+
// 2. The user is connecting to an older Coder instance.
28+
t.Run("no-default", func(t *testing.T) {
29+
t.Parallel()
30+
31+
orgID := uuid.New()
32+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
33+
json.NewEncoder(w).Encode([]codersdk.Organization{
34+
{
35+
ID: orgID,
36+
Name: "not-default",
37+
CreatedAt: time.Now(),
38+
UpdatedAt: time.Now(),
39+
IsDefault: false,
40+
},
41+
})
42+
}))
43+
defer srv.Close()
44+
45+
client := codersdk.New(must(url.Parse(srv.URL)))
46+
inv, root := clitest.New(t, "organizations", "show", "current")
47+
clitest.SetupConfig(t, client, root)
48+
pty := ptytest.New(t).Attach(inv)
49+
errC := make(chan error)
50+
go func() {
51+
errC <- inv.Run()
52+
}()
53+
require.NoError(t, <-errC)
54+
pty.ExpectMatch(orgID.String())
55+
})
56+
1957
t.Run("OnlyID", func(t *testing.T) {
2058
t.Parallel()
2159
ownerClient := coderdtest.New(t, nil)
@@ -108,3 +146,10 @@ func TestOrganizationSwitch(t *testing.T) {
108146
pty.ExpectMatch(exp.ID.String())
109147
})
110148
}
149+
150+
func must[V any](v V, err error) V {
151+
if err != nil {
152+
panic(err)
153+
}
154+
return v
155+
}

cli/root.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -749,7 +749,14 @@ func CurrentOrganization(r *RootCmd, inv *clibase.Invocation, client *codersdk.C
749749
return org.IsDefault
750750
})
751751
if index < 0 {
752-
return codersdk.Organization{}, xerrors.Errorf("unable to determine current organization. Use 'coder set <org>' to select an organization to use")
752+
if len(orgs) == 1 {
753+
// If there is no "isDefault", but only 1 org is present. We can just
754+
// assume the single organization is correct. This is mainly a helper
755+
// for cli hitting an old instance, or a user that belongs to a single
756+
// org that is not the default.
757+
return orgs[0], nil
758+
}
759+
return codersdk.Organization{}, xerrors.Errorf("unable to determine current organization. Use 'coder org set <org>' to select an organization to use")
753760
}
754761

755762
return orgs[index], nil

0 commit comments

Comments
 (0)