Skip to content
This repository was archived by the owner on Aug 30, 2024. It is now read-only.

Commit ff28df3

Browse files
committed
Cleanup entclient api
1 parent f21d224 commit ff28df3

File tree

10 files changed

+102
-138
lines changed

10 files changed

+102
-138
lines changed

internal/cmd/ceapi.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ func getEnvs(ctx context.Context, client *entclient.Client, email string) ([]ent
4545
var allEnvs []entclient.Environment
4646

4747
for _, org := range orgs {
48-
envs, err := client.Envs(ctx, user, org)
48+
envs, err := client.EnvironmentsInOrganization(ctx, user, &org)
4949
if err != nil {
5050
return nil, xerrors.Errorf("get envs for %v: %+v", org.Name, err)
5151
}

internal/cmd/secrets.go

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func makeSecretsCmd() *cobra.Command {
4747
return cmd
4848
}
4949

50-
func makeCreateSecret(user *string) *cobra.Command {
50+
func makeCreateSecret(userEmail *string) *cobra.Command {
5151
var (
5252
fromFile string
5353
fromLiteral string
@@ -109,12 +109,14 @@ coder secrets create aws-credentials --from-file ./credentials.json`,
109109
}
110110
}
111111

112-
err = client.InsertSecret(cmd.Context(), entclient.InsertSecretReq{
112+
user, err := client.UserByEmail(cmd.Context(), *userEmail)
113+
if err != nil {
114+
return xerrors.Errorf("get user %q by email: %w", *userEmail, err)
115+
}
116+
err = client.InsertSecret(cmd.Context(), user, entclient.InsertSecretReq{
113117
Name: name,
114118
Value: value,
115119
Description: description,
116-
}, &entclient.ReqOptions{
117-
User: *user,
118120
})
119121
if err != nil {
120122
return xerrors.Errorf("insert secret: %w", err)
@@ -131,13 +133,15 @@ coder secrets create aws-credentials --from-file ./credentials.json`,
131133
return cmd
132134
}
133135

134-
func listSecrets(user *string) func(cmd *cobra.Command, _ []string) error {
136+
func listSecrets(userEmail *string) func(cmd *cobra.Command, _ []string) error {
135137
return func(cmd *cobra.Command, _ []string) error {
136138
client := requireAuth()
139+
user, err := client.UserByEmail(cmd.Context(), *userEmail)
140+
if err != nil {
141+
return xerrors.Errorf("get user %q by email: %w", *userEmail, err)
142+
}
137143

138-
secrets, err := client.Secrets(cmd.Context(), &entclient.ReqOptions{
139-
User: *user,
140-
})
144+
secrets, err := client.Secrets(cmd.Context(), user)
141145
if err != nil {
142146
return xerrors.Errorf("get secrets: %w", err)
143147
}
@@ -159,19 +163,18 @@ func listSecrets(user *string) func(cmd *cobra.Command, _ []string) error {
159163
}
160164
}
161165

162-
func makeViewSecret(user *string) func(cmd *cobra.Command, args []string) error {
166+
func makeViewSecret(userEmail *string) func(cmd *cobra.Command, args []string) error {
163167
return func(cmd *cobra.Command, args []string) error {
164168
var (
165169
client = requireAuth()
166170
name = args[0]
167171
)
168-
if name == "" {
169-
return xerrors.New("[name] is a required argument")
172+
user, err := client.UserByEmail(cmd.Context(), *userEmail)
173+
if err != nil {
174+
return xerrors.Errorf("get user %q by email: %w", *userEmail, err)
170175
}
171176

172-
secret, err := client.SecretByName(cmd.Context(), name, &entclient.ReqOptions{
173-
User: *user,
174-
})
177+
secret, err := client.SecretByName(cmd.Context(), name, user)
175178
if err != nil {
176179
return xerrors.Errorf("get secret by name: %w", err)
177180
}
@@ -184,20 +187,19 @@ func makeViewSecret(user *string) func(cmd *cobra.Command, args []string) error
184187
}
185188
}
186189

187-
func makeRemoveSecrets(user *string) func(c *cobra.Command, args []string) error {
190+
func makeRemoveSecrets(userEmail *string) func(c *cobra.Command, args []string) error {
188191
return func(cmd *cobra.Command, args []string) error {
189192
var (
190193
client = requireAuth()
191194
)
192-
if len(args) < 1 {
193-
return xerrors.New("[...secret_name] is a required argument")
195+
user, err := client.UserByEmail(cmd.Context(), *userEmail)
196+
if err != nil {
197+
return xerrors.Errorf("get user %q by email: %w", *userEmail, err)
194198
}
195199

196200
errorSeen := false
197201
for _, n := range args {
198-
err := client.DeleteSecretByName(cmd.Context(), n, &entclient.ReqOptions{
199-
User: *user,
200-
})
202+
err := client.DeleteSecretByName(cmd.Context(), n, user)
201203
if err != nil {
202204
flog.Error("failed to delete secret %q: %v", n, err)
203205
errorSeen = true

internal/cmd/shell.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ func runCommand(ctx context.Context, envName string, command string, args []stri
118118
ctx, cancel := context.WithCancel(ctx)
119119
defer cancel()
120120

121-
conn, err := entClient.DialWsep(ctx, *env)
121+
conn, err := entClient.DialWsep(ctx, env)
122122
if err != nil {
123123
return err
124124
}

internal/entclient/activity.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"net/http"
66
)
77

8-
// PushActivity pushes CLI activity to Coder
8+
// PushActivity pushes CLI activity to Coder.
99
func (c Client) PushActivity(ctx context.Context, source string, envID string) error {
10-
res, err := c.request(ctx, "POST", "/api/metrics/usage/push", map[string]string{
10+
res, err := c.request(ctx, http.MethodPost, "/api/metrics/usage/push", map[string]string{
1111
"source": source,
1212
"environment_id": envID,
1313
})
@@ -18,6 +18,5 @@ func (c Client) PushActivity(ctx context.Context, source string, envID string) e
1818
if res.StatusCode != http.StatusOK {
1919
return bodyError(res)
2020
}
21-
2221
return nil
2322
}

internal/entclient/devurl.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type delDevURLRequest struct {
2424
func (c Client) DelDevURL(ctx context.Context, envID, urlID string) error {
2525
reqURL := fmt.Sprintf("/api/environments/%s/devurls/%s", envID, urlID)
2626

27-
res, err := c.request(ctx, "DELETE", reqURL, delDevURLRequest{
27+
res, err := c.request(ctx, http.MethodDelete, reqURL, delDevURLRequest{
2828
EnvID: envID,
2929
DevURLID: urlID,
3030
})
@@ -51,7 +51,7 @@ type createDevURLRequest struct {
5151
func (c Client) InsertDevURL(ctx context.Context, envID string, port int, name, access string) error {
5252
reqURL := fmt.Sprintf("/api/environments/%s/devurls", envID)
5353

54-
res, err := c.request(ctx, "POST", reqURL, createDevURLRequest{
54+
res, err := c.request(ctx, http.MethodPost, reqURL, createDevURLRequest{
5555
EnvID: envID,
5656
Port: port,
5757
Access: access,
@@ -75,7 +75,7 @@ type updateDevURLRequest createDevURLRequest
7575
func (c Client) UpdateDevURL(ctx context.Context, envID, urlID string, port int, name, access string) error {
7676
reqURL := fmt.Sprintf("/api/environments/%s/devurls/%s", envID, urlID)
7777

78-
res, err := c.request(ctx, "PUT", reqURL, updateDevURLRequest{
78+
res, err := c.request(ctx, http.MethodPut, reqURL, updateDevURLRequest{
7979
EnvID: envID,
8080
Port: port,
8181
Access: access,

internal/entclient/env.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package entclient
22

33
import (
44
"context"
5+
"net/http"
56
"time"
67

78
"cdr.dev/coder-cli/internal/x/xjson"
@@ -33,12 +34,12 @@ type Environment struct {
3334
AutoOffThreshold xjson.Duration `json:"auto_off_threshold" tab:"-"`
3435
}
3536

36-
// Envs gets the list of environments owned by the authenticated user
37-
func (c Client) Envs(ctx context.Context, user *User, org Org) ([]Environment, error) {
37+
// EnvironmentsInOrganization gets the list of environments owned by the authenticated user
38+
func (c Client) EnvironmentsInOrganization(ctx context.Context, user *User, org *Org) ([]Environment, error) {
3839
var envs []Environment
3940
err := c.requestBody(
4041
ctx,
41-
"GET", "/api/orgs/"+org.ID+"/members/"+user.ID+"/environments",
42+
http.MethodGet, "/api/orgs/"+org.ID+"/members/"+user.ID+"/environments",
4243
nil,
4344
&envs,
4445
)
@@ -47,7 +48,7 @@ func (c Client) Envs(ctx context.Context, user *User, org Org) ([]Environment, e
4748

4849
// DialWsep dials an environments command execution interface
4950
// See github.com/cdr/wsep for details
50-
func (c Client) DialWsep(ctx context.Context, env Environment) (*websocket.Conn, error) {
51+
func (c Client) DialWsep(ctx context.Context, env *Environment) (*websocket.Conn, error) {
5152
u := c.copyURL()
5253
if c.BaseURL.Scheme == "https" {
5354
u.Scheme = "wss"

internal/entclient/me.go

Lines changed: 0 additions & 42 deletions
This file was deleted.

internal/entclient/secrets.go

Lines changed: 14 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -16,47 +16,21 @@ type Secret struct {
1616
UpdatedAt time.Time `json:"updated_at" tab:"-"`
1717
}
1818

19-
// ReqOptions define api request configuration options
20-
type ReqOptions struct {
21-
// User defines the users whose resources should be targeted
22-
User string
23-
}
24-
25-
// DefaultReqOptions define reasonable defaults for an api request
26-
var DefaultReqOptions = &ReqOptions{
27-
User: Me,
28-
}
29-
30-
// Secrets gets all secrets owned by the authed user
31-
func (c *Client) Secrets(ctx context.Context, opts *ReqOptions) ([]Secret, error) {
32-
if opts == nil {
33-
opts = DefaultReqOptions
34-
}
35-
user, err := c.UserByEmail(ctx, opts.User)
36-
if err != nil {
37-
return nil, err
38-
}
19+
// Secrets gets all secrets for the given user
20+
func (c *Client) Secrets(ctx context.Context, user *User) ([]Secret, error) {
3921
var secrets []Secret
40-
err = c.requestBody(ctx, http.MethodGet, "/api/users/"+user.ID+"/secrets", nil, &secrets)
22+
err := c.requestBody(ctx, http.MethodGet, "/api/users/"+user.ID+"/secrets", nil, &secrets)
4123
return secrets, err
4224
}
4325

44-
func (c *Client) secretByID(ctx context.Context, id string, opts *ReqOptions) (*Secret, error) {
45-
if opts == nil {
46-
opts = DefaultReqOptions
47-
}
48-
user, err := c.UserByEmail(ctx, opts.User)
49-
if err != nil {
50-
return nil, err
51-
}
52-
26+
func (c *Client) secretByID(ctx context.Context, id string, user *User) (*Secret, error) {
5327
var secret Secret
54-
err = c.requestBody(ctx, http.MethodGet, "/api/users/"+user.ID+"/secrets/"+id, nil, &secret)
28+
err := c.requestBody(ctx, http.MethodGet, "/api/users/"+user.ID+"/secrets/"+id, nil, &secret)
5529
return &secret, err
5630
}
5731

58-
func (c *Client) secretNameToID(ctx context.Context, name string, opts *ReqOptions) (id string, _ error) {
59-
secrets, err := c.Secrets(ctx, opts)
32+
func (c *Client) secretNameToID(ctx context.Context, name string, user *User) (id string, _ error) {
33+
secrets, err := c.Secrets(ctx, user)
6034
if err != nil {
6135
return "", err
6236
}
@@ -69,12 +43,12 @@ func (c *Client) secretNameToID(ctx context.Context, name string, opts *ReqOptio
6943
}
7044

7145
// SecretByName gets a secret object by name
72-
func (c *Client) SecretByName(ctx context.Context, name string, opts *ReqOptions) (*Secret, error) {
73-
id, err := c.secretNameToID(ctx, name, opts)
46+
func (c *Client) SecretByName(ctx context.Context, name string, user *User) (*Secret, error) {
47+
id, err := c.secretNameToID(ctx, name, user)
7448
if err != nil {
7549
return nil, err
7650
}
77-
return c.secretByID(ctx, id, opts)
51+
return c.secretByID(ctx, id, user)
7852
}
7953

8054
// InsertSecretReq describes the request body for creating a new secret
@@ -85,29 +59,14 @@ type InsertSecretReq struct {
8559
}
8660

8761
// InsertSecret adds a new secret for the authed user
88-
func (c *Client) InsertSecret(ctx context.Context, req InsertSecretReq, opts *ReqOptions) error {
89-
if opts == nil {
90-
opts = DefaultReqOptions
91-
}
92-
user, err := c.UserByEmail(ctx, opts.User)
93-
if err != nil {
94-
return err
95-
}
62+
func (c *Client) InsertSecret(ctx context.Context, user *User, req InsertSecretReq) error {
9663
var resp interface{}
97-
err = c.requestBody(ctx, http.MethodPost, "/api/users/"+user.ID+"/secrets", req, &resp)
98-
return err
64+
return c.requestBody(ctx, http.MethodPost, "/api/users/"+user.ID+"/secrets", req, &resp)
9965
}
10066

10167
// DeleteSecretByName deletes the authenticated users secret with the given name
102-
func (c *Client) DeleteSecretByName(ctx context.Context, name string, opts *ReqOptions) error {
103-
if opts == nil {
104-
opts = DefaultReqOptions
105-
}
106-
user, err := c.UserByEmail(ctx, opts.User)
107-
if err != nil {
108-
return err
109-
}
110-
id, err := c.secretNameToID(ctx, name, opts)
68+
func (c *Client) DeleteSecretByName(ctx context.Context, name string, user *User) error {
69+
id, err := c.secretNameToID(ctx, name, user)
11170
if err != nil {
11271
return err
11372
}

0 commit comments

Comments
 (0)