Skip to content

Commit 62f7d70

Browse files
committed
fix: handle signals in organization list
1 parent 3819c6d commit 62f7d70

File tree

4 files changed

+21
-40
lines changed

4 files changed

+21
-40
lines changed

cmd/orgs.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package cmd
22

33
import (
4+
"os"
5+
"os/signal"
6+
47
"github.com/spf13/afero"
58
"github.com/spf13/cobra"
69
"github.com/supabase/cli/internal/orgs/list"
@@ -18,7 +21,8 @@ var (
1821
Short: "List all organizations",
1922
Long: "List all organizations the logged-in user belongs.",
2023
RunE: func(cmd *cobra.Command, args []string) error {
21-
return list.Run(afero.NewOsFs())
24+
ctx, _ := signal.NotifyContext(cmd.Context(), os.Interrupt)
25+
return list.Run(ctx, afero.NewOsFs())
2226
},
2327
}
2428
)

cmd/projects.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package cmd
22

33
import (
4+
"errors"
45
"fmt"
56
"os"
67
"os/signal"
@@ -103,6 +104,9 @@ func PromptCreateFlags(cmd *cobra.Command) error {
103104
if err != nil {
104105
return err
105106
}
107+
if resp.JSON200 == nil {
108+
return errors.New("Unexpected error retrieving organizations: " + string(resp.Body))
109+
}
106110
items := make([]utils.PromptItem, len(*resp.JSON200))
107111
for i, org := range *resp.JSON200 {
108112
items[i] = utils.PromptItem{Summary: org.Name, Details: org.Id}

internal/orgs/list/list.go

Lines changed: 6 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package list
22

33
import (
4-
"encoding/json"
4+
"context"
55
"errors"
66
"fmt"
7-
"io"
8-
"net/http"
97
"strings"
108

119
"github.com/charmbracelet/glamour"
@@ -18,46 +16,20 @@ type Organization struct {
1816
Name string `json:"name"`
1917
}
2018

21-
func Run(fsys afero.Fs) error {
22-
accessToken, err := utils.LoadAccessTokenFS(fsys)
19+
func Run(ctx context.Context, fsys afero.Fs) error {
20+
resp, err := utils.GetSupabase().GetOrganizationsWithResponse(ctx)
2321
if err != nil {
2422
return err
2523
}
2624

27-
req, err := http.NewRequest("GET", utils.GetSupabaseAPIHost()+"/v1/organizations", nil)
28-
if err != nil {
29-
return err
30-
}
31-
req.Header.Add("Authorization", "Bearer "+string(accessToken))
32-
resp, err := http.DefaultClient.Do(req)
33-
if err != nil {
34-
return err
35-
}
36-
defer resp.Body.Close()
37-
38-
if resp.StatusCode != http.StatusOK {
39-
body, err := io.ReadAll(resp.Body)
40-
if err != nil {
41-
return fmt.Errorf("Unexpected error retrieving organizations: %w", err)
42-
}
43-
44-
return errors.New("Unexpected error retrieving organizations: " + string(body))
45-
}
46-
47-
body, err := io.ReadAll(resp.Body)
48-
if err != nil {
49-
return err
50-
}
51-
52-
var orgs []Organization
53-
if err := json.Unmarshal(body, &orgs); err != nil {
54-
return err
25+
if resp.JSON200 == nil {
26+
return errors.New("Unexpected error retrieving organizations: " + string(resp.Body))
5527
}
5628

5729
table := `|ID|NAME|
5830
|-|-|
5931
`
60-
for _, org := range orgs {
32+
for _, org := range *resp.JSON200 {
6133
table += fmt.Sprintf("|`%s`|`%s`|\n", org.Id, strings.ReplaceAll(org.Name, "|", "\\|"))
6234
}
6335

internal/orgs/list/list_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package list
22

33
import (
4+
"context"
45
"errors"
56
"testing"
67

@@ -29,13 +30,13 @@ func TestOrganizationListCommand(t *testing.T) {
2930
},
3031
})
3132
// Run test
32-
assert.NoError(t, Run(fsys))
33+
assert.NoError(t, Run(context.Background(), fsys))
3334
// Validate api
3435
assert.Empty(t, apitest.ListUnmatchedRequests())
3536
})
3637

3738
t.Run("throws error on failure to load token", func(t *testing.T) {
38-
assert.Error(t, Run(afero.NewMemMapFs()))
39+
assert.Error(t, Run(context.Background(), afero.NewMemMapFs()))
3940
})
4041

4142
t.Run("throws error on network error", func(t *testing.T) {
@@ -50,7 +51,7 @@ func TestOrganizationListCommand(t *testing.T) {
5051
Get("/v1/organizations").
5152
ReplyError(errors.New("network error"))
5253
// Run test
53-
assert.Error(t, Run(fsys))
54+
assert.Error(t, Run(context.Background(), fsys))
5455
// Validate api
5556
assert.Empty(t, apitest.ListUnmatchedRequests())
5657
})
@@ -68,7 +69,7 @@ func TestOrganizationListCommand(t *testing.T) {
6869
Reply(500).
6970
JSON(map[string]string{"message": "unavailable"})
7071
// Run test
71-
assert.Error(t, Run(fsys))
72+
assert.Error(t, Run(context.Background(), fsys))
7273
// Validate api
7374
assert.Empty(t, apitest.ListUnmatchedRequests())
7475
})
@@ -86,7 +87,7 @@ func TestOrganizationListCommand(t *testing.T) {
8687
Reply(200).
8788
JSON(map[string]string{})
8889
// Run test
89-
assert.Error(t, Run(fsys))
90+
assert.Error(t, Run(context.Background(), fsys))
9091
// Validate api
9192
assert.Empty(t, apitest.ListUnmatchedRequests())
9293
})

0 commit comments

Comments
 (0)