Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Aug 30, 2024. It is now read-only.
/coder-v1-cliPublic archive

Commit2d52bb3

Browse files
authored
Merge pull request#95 from cdr/cleanup-entclient
Cleanup and export entclient
2 parents057ccbc +9974e45 commit2d52bb3

File tree

23 files changed

+241
-271
lines changed

23 files changed

+241
-271
lines changed

‎ci/integration/users_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
"testing"
66

77
"cdr.dev/coder-cli/ci/tcli"
8-
"cdr.dev/coder-cli/internal/entclient"
8+
"cdr.dev/coder-cli/coder-sdk"
99
"cdr.dev/slog/sloggers/slogtest/assert"
1010
)
1111

@@ -20,7 +20,7 @@ func TestUsers(t *testing.T) {
2020

2121
headlessLogin(ctx,t,c)
2222

23-
varuserentclient.User
23+
varusercoder.User
2424
c.Run(ctx,`coder users ls --output json | jq -c '.[] | select( .username == "charlie")'`).Assert(t,
2525
tcli.Success(),
2626
tcli.StdoutJSONUnmarshal(&user),
Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
packageentclient
1+
packagecoder
22

33
import (
44
"context"
55
"net/http"
66
)
77

8-
// PushActivity pushes CLI activity to Coder
8+
// PushActivity pushes CLI activity to Coder.
99
func (cClient)PushActivity(ctx context.Context,sourcestring,envIDstring)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
ifres.StatusCode!=http.StatusOK {
1919
returnbodyError(res)
2020
}
21-
2221
returnnil
2322
}

‎internal/entclient/client.gorenamed to‎coder-sdk/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packageentclient
1+
packagecoder
22

33
import (
44
"net/http"

‎internal/entclient/devurl.gorenamed to‎coder-sdk/devurl.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packageentclient
1+
packagecoder
22

33
import (
44
"context"
@@ -24,7 +24,7 @@ type delDevURLRequest struct {
2424
func (cClient)DelDevURL(ctx context.Context,envID,urlIDstring)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 (cClient)InsertDevURL(ctx context.Context,envIDstring,portint,name,accessstring)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 (cClient)UpdateDevURL(ctx context.Context,envID,urlIDstring,portint,name,accessstring)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.gorenamed to‎coder-sdk/env.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
packageentclient
1+
packagecoder
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 theauthenticated user
37-
func (cClient)Envs(ctx context.Context,user*User,orgOrg) ([]Environment,error) {
37+
//EnvironmentsByOrganization gets the list of environments owned by thegiven user.
38+
func (cClient)EnvironmentsByOrganization(ctx context.Context,userID,orgIDstring) ([]Environment,error) {
3839
varenvs []Environment
3940
err:=c.requestBody(
4041
ctx,
41-
"GET","/api/orgs/"+org.ID+"/members/"+user.ID+"/environments",
42+
http.MethodGet,"/api/orgs/"+orgID+"/members/"+userID+"/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 (cClient)DialWsep(ctx context.Context,envEnvironment) (*websocket.Conn,error) {
51+
func (cClient)DialWsep(ctx context.Context,env*Environment) (*websocket.Conn,error) {
5152
u:=c.copyURL()
5253
ifc.BaseURL.Scheme=="https" {
5354
u.Scheme="wss"

‎internal/entclient/error.gorenamed to‎coder-sdk/error.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packageentclient
1+
packagecoder
22

33
import (
44
"encoding/json"

‎internal/entclient/org.gorenamed to‎coder-sdk/org.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
packageentclient
1+
packagecoder
22

3-
import"context"
3+
import (
4+
"context"
5+
"net/http"
6+
)
47

58
// Org describes an Organization in Coder
69
typeOrgstruct {
@@ -12,6 +15,6 @@ type Org struct {
1215
// Orgs gets all Organizations
1316
func (cClient)Orgs(ctx context.Context) ([]Org,error) {
1417
varos []Org
15-
err:=c.requestBody(ctx,"GET","/api/orgs",nil,&os)
18+
err:=c.requestBody(ctx,http.MethodGet,"/api/orgs",nil,&os)
1619
returnos,err
1720
}

‎internal/entclient/request.gorenamed to‎coder-sdk/request.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
packageentclient
1+
packagecoder
22

33
import (
44
"bytes"

‎coder-sdk/secrets.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package coder
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"time"
7+
)
8+
9+
// Secret describes a Coder secret
10+
typeSecretstruct {
11+
IDstring`json:"id" tab:"-"`
12+
Namestring`json:"name"`
13+
Valuestring`json:"value,omitempty"`
14+
Descriptionstring`json:"description"`
15+
CreatedAt time.Time`json:"created_at"`
16+
UpdatedAt time.Time`json:"updated_at" tab:"-"`
17+
}
18+
19+
// Secrets gets all secrets for the given user
20+
func (c*Client)Secrets(ctx context.Context,userIDstring) ([]Secret,error) {
21+
varsecrets []Secret
22+
err:=c.requestBody(ctx,http.MethodGet,"/api/users/"+userID+"/secrets",nil,&secrets)
23+
returnsecrets,err
24+
}
25+
26+
// SecretWithValueByName gets the Coder secret with its value by its name.
27+
func (c*Client)SecretWithValueByName(ctx context.Context,name,userIDstring) (*Secret,error) {
28+
s,err:=c.SecretByName(ctx,name,userID)
29+
iferr!=nil {
30+
returnnil,err
31+
}
32+
varsecretSecret
33+
err=c.requestBody(ctx,http.MethodGet,"/api/users/"+userID+"/secrets/"+s.ID,nil,&secret)
34+
return&secret,err
35+
}
36+
37+
// SecretWithValueByID gets the Coder secret with its value by the secret_id.
38+
func (c*Client)SecretWithValueByID(ctx context.Context,id,userIDstring) (*Secret,error) {
39+
varsecretSecret
40+
err:=c.requestBody(ctx,http.MethodGet,"/api/users/"+userID+"/secrets/"+id,nil,&secret)
41+
return&secret,err
42+
}
43+
44+
// SecretByName gets a secret object by name
45+
func (c*Client)SecretByName(ctx context.Context,name,userIDstring) (*Secret,error) {
46+
secrets,err:=c.Secrets(ctx,userID)
47+
iferr!=nil {
48+
returnnil,err
49+
}
50+
for_,s:=rangesecrets {
51+
ifs.Name==name {
52+
return&s,nil
53+
}
54+
}
55+
returnnil,ErrNotFound
56+
}
57+
58+
// InsertSecretReq describes the request body for creating a new secret
59+
typeInsertSecretReqstruct {
60+
Namestring`json:"name"`
61+
Valuestring`json:"value"`
62+
Descriptionstring`json:"description"`
63+
}
64+
65+
// InsertSecret adds a new secret for the authed user
66+
func (c*Client)InsertSecret(ctx context.Context,user*User,reqInsertSecretReq)error {
67+
varrespinterface{}
68+
returnc.requestBody(ctx,http.MethodPost,"/api/users/"+user.ID+"/secrets",req,&resp)
69+
}
70+
71+
// DeleteSecretByName deletes the authenticated users secret with the given name
72+
func (c*Client)DeleteSecretByName(ctx context.Context,name,userIDstring)error {
73+
secret,err:=c.SecretByName(ctx,name,userID)
74+
iferr!=nil {
75+
returnerr
76+
}
77+
_,err=c.request(ctx,http.MethodDelete,"/api/users/"+userID+"/secrets/"+secret.ID,nil)
78+
returnerr
79+
}

‎coder-sdk/users.go

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package coder
2+
3+
import (
4+
"context"
5+
"net/http"
6+
"time"
7+
)
8+
9+
// User describes a Coder user account.
10+
typeUserstruct {
11+
IDstring`json:"id" tab:"-"`
12+
Emailstring`json:"email"`
13+
Usernamestring`json:"username"`
14+
Namestring`json:"name"`
15+
CreatedAt time.Time`json:"created_at"`
16+
UpdatedAt time.Time`json:"updated_at" tab:"-"`
17+
}
18+
19+
// Me gets the details of the authenticated user.
20+
func (cClient)Me(ctx context.Context) (*User,error) {
21+
returnc.UserByID(ctx,Me)
22+
}
23+
24+
// UserByID get the details of a user by their id.
25+
func (cClient)UserByID(ctx context.Context,idstring) (*User,error) {
26+
varuUser
27+
err:=c.requestBody(ctx,http.MethodGet,"/api/users/"+id,nil,&u)
28+
iferr!=nil {
29+
returnnil,err
30+
}
31+
return&u,nil
32+
}
33+
34+
// SSHKey describes an SSH keypair
35+
typeSSHKeystruct {
36+
PublicKeystring`json:"public_key"`
37+
PrivateKeystring`json:"private_key"`
38+
}
39+
40+
// SSHKey gets the current SSH kepair of the authenticated user.
41+
func (cClient)SSHKey(ctx context.Context) (*SSHKey,error) {
42+
varkeySSHKey
43+
err:=c.requestBody(ctx,http.MethodGet,"/api/users/me/sshkey",nil,&key)
44+
iferr!=nil {
45+
returnnil,err
46+
}
47+
return&key,nil
48+
}
49+
50+
// Users gets the list of user accounts.
51+
func (cClient)Users(ctx context.Context) ([]User,error) {
52+
varu []User
53+
err:=c.requestBody(ctx,http.MethodGet,"/api/users",nil,&u)
54+
iferr!=nil {
55+
returnnil,err
56+
}
57+
returnu,nil
58+
}
59+
60+
// UserByEmail gets a user by email.
61+
func (cClient)UserByEmail(ctx context.Context,emailstring) (*User,error) {
62+
ifemail==Me {
63+
returnc.Me(ctx)
64+
}
65+
users,err:=c.Users(ctx)
66+
iferr!=nil {
67+
returnnil,err
68+
}
69+
for_,u:=rangeusers {
70+
ifu.Email==email {
71+
return&u,nil
72+
}
73+
}
74+
returnnil,ErrNotFound
75+
}

‎internal/activity/pusher.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"context"
55
"time"
66

7-
"cdr.dev/coder-cli/internal/entclient"
7+
"cdr.dev/coder-cli/coder-sdk"
88
"golang.org/x/time/rate"
99

1010
"go.coder.com/flog"
@@ -18,12 +18,12 @@ type Pusher struct {
1818
envIDstring
1919
sourcestring
2020

21-
client*entclient.Client
21+
client*coder.Client
2222
rate*rate.Limiter
2323
}
2424

2525
// NewPusher instantiates a new instance of Pusher
26-
funcNewPusher(c*entclient.Client,envID,sourcestring)*Pusher {
26+
funcNewPusher(c*coder.Client,envID,sourcestring)*Pusher {
2727
return&Pusher{
2828
envID:envID,
2929
source:source,

‎internal/cmd/auth.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@ package cmd
33
import (
44
"net/url"
55

6+
"cdr.dev/coder-cli/coder-sdk"
67
"cdr.dev/coder-cli/internal/config"
7-
"cdr.dev/coder-cli/internal/entclient"
88
"golang.org/x/xerrors"
99

1010
"go.coder.com/flog"
1111
)
1212

1313
// requireAuth exits the process with a nonzero exit code if the user is not authenticated to make requests
14-
funcrequireAuth()*entclient.Client {
14+
funcrequireAuth()*coder.Client {
1515
client,err:=newClient()
1616
iferr!=nil {
1717
flog.Fatal("%v",err)
1818
}
1919
returnclient
2020
}
2121

22-
funcnewClient() (*entclient.Client,error) {
22+
funcnewClient() (*coder.Client,error) {
2323
sessionToken,err:=config.Session.Read()
2424
iferr!=nil {
2525
returnnil,xerrors.Errorf("read session: %v (did you run coder login?)",err)
@@ -35,7 +35,7 @@ func newClient() (*entclient.Client, error) {
3535
returnnil,xerrors.Errorf("url misformatted: %v (try runing coder login)",err)
3636
}
3737

38-
client:=&entclient.Client{
38+
client:=&coder.Client{
3939
BaseURL:u,
4040
Token:sessionToken,
4141
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp