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

Commit 0156dd0

Browse files
authored
Cleanup (#179)
1 parent b92aaf0 commit 0156dd0

16 files changed

+115
-121
lines changed

ci/steps/build.sh

+11-4
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,13 @@ pushd() { builtin pushd "$@" >/dev/null; }
55
popd() { builtin popd >/dev/null; }
66

77
set -euo pipefail
8-
cd "$(dirname "$0")"
8+
9+
cd "$(git rev-parse --show-toplevel)/ci/steps"
910

1011
tag=$(git describe --tags)
1112

1213
build() {
13-
echo "Building coder-cli for $GOOS-$GOARCH..."
14+
echo "--- building coder-cli for $GOOS-$GOARCH"
1415

1516
tmpdir=$(mktemp -d)
1617
go build -ldflags "-X cdr.dev/coder-cli/internal/version.Version=${tag}" -o "$tmpdir/coder" ../../cmd/coder
@@ -29,9 +30,15 @@ build() {
2930
tar -czf "$artifact" coder
3031
;;
3132
"darwin")
33+
if [[ ${CI-} ]]; then
3234
artifact="coder-cli-$GOOS-$GOARCH.zip"
3335
gon -log-level debug ./gon.json
3436
mv coder.zip $artifact
37+
else
38+
artifact="coder-cli-$GOOS-$GOARCH.tar.gz"
39+
tar -czf "$artifact" coder
40+
echo "--- warning: not in ci, skipping signed release of darwin"
41+
fi
3542
;;
3643
esac
3744
popd
@@ -46,8 +53,8 @@ build() {
4653
if [[ "$(uname)" == "Darwin" ]]; then
4754
CGO_ENABLED=1 GOOS=darwin GOARCH=amd64 build
4855
else
49-
echo "Warning: Darwin builds don't work on Linux."
50-
echo "Please use an OSX machine to build Darwin tars."
56+
echo "--- warning: Darwin builds don't work on Linux."
57+
echo "--- please use an OSX machine to build Darwin tars."
5158
fi
5259

5360
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 build

ci/steps/fmt.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22

33
set -euo pipefail
44

5-
echo "Formatting..."
5+
cd "$(git rev-parse --show-toplevel)"
66

7+
echo "--- formatting"
78
go mod tidy
89
gofmt -w -s .
910
goimports -w "-local=$$(go list -m)" .

ci/steps/gendocs.sh

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
set -euo pipefail
44

5-
echo "Generating docs..."
6-
7-
cd "$(dirname "$0")"
8-
cd ../../
5+
cd "$(git rev-parse --show-toplevel)"
96

7+
echo "--- regenerating documentation"
108
rm -rf ./docs
119
mkdir ./docs
1210
go run ./cmd/coder gen-docs ./docs

ci/steps/integration.sh

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,10 @@
22

33
set -eo pipefail
44

5-
log() {
6-
echo "--- $@"
7-
}
8-
95
cd "$(git rev-parse --show-toplevel)"
106

11-
log "building integration test image"
7+
echo "--- building integration test image"
128
docker build -f ./ci/integration/Dockerfile -t coder-cli-integration:latest .
139

14-
log "starting integration tests"
10+
echo "--- starting integration tests"
1511
go test ./ci/integration -count=1

ci/steps/lint.sh

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,7 @@
22

33
set -euo pipefail
44

5-
echo "Linting..."
6-
7-
cd "$(dirname "$0")"
8-
cd ../../
5+
cd "$(git rev-parse --show-toplevel)"
96

107
echo "--- golangci-lint"
118
golangci-lint run -c .golangci.yml

ci/steps/unit_test.sh

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ set -euo pipefail
44

55
cd "$(git rev-parse --show-toplevel)"
66

7-
echo "--- go test..."
8-
7+
echo "--- running unit tests"
98
go test $(go list ./... | grep -v pkg/tcli | grep -v ci/integration | grep -v coder-sdk)

coder-sdk/client.go

+9-11
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,20 @@ type Client struct {
2525
// WARNING: If the caller sets a custom transport to set TLS settings or a custom CA, the default
2626
// pool will not be used and it might result in a new dns lookup/tls session/socket begin
2727
// established each time.
28-
func (c *Client) newHTTPClient() (*http.Client, error) {
28+
func (c Client) newHTTPClient() (*http.Client, error) {
2929
jar, err := cookiejar.New(nil)
3030
if err != nil {
3131
return nil, err
3232
}
3333

34-
jar.SetCookies(c.BaseURL, []*http.Cookie{
35-
{
36-
Name: "session_token",
37-
Value: c.Token,
38-
MaxAge: 86400,
39-
Path: "/",
40-
HttpOnly: true,
41-
Secure: c.BaseURL.Scheme == "https",
42-
},
43-
})
34+
jar.SetCookies(c.BaseURL, []*http.Cookie{{
35+
Name: "session_token",
36+
Value: c.Token,
37+
MaxAge: 86400,
38+
Path: "/",
39+
HttpOnly: true,
40+
Secure: c.BaseURL.Scheme == "https",
41+
}})
4442

4543
return &http.Client{Jar: jar}, nil
4644
}

coder-sdk/devurl.go

+14-56
Original file line numberDiff line numberDiff line change
@@ -20,75 +20,33 @@ type delDevURLRequest struct {
2020
DevURLID string `json:"url_id"`
2121
}
2222

23-
// DelDevURL deletes the specified devurl.
24-
func (c Client) DelDevURL(ctx context.Context, envID, urlID string) error {
23+
// DeleteDevURL deletes the specified devurl.
24+
func (c Client) DeleteDevURL(ctx context.Context, envID, urlID string) error {
2525
reqURL := fmt.Sprintf("/api/environments/%s/devurls/%s", envID, urlID)
2626

27-
resp, err := c.request(ctx, http.MethodDelete, reqURL, delDevURLRequest{
27+
return c.requestBody(ctx, http.MethodDelete, reqURL, delDevURLRequest{
2828
EnvID: envID,
2929
DevURLID: urlID,
30-
})
31-
if err != nil {
32-
return err
33-
}
34-
defer func() { _ = resp.Body.Close() }() // Best effort. Likely connection drop.
35-
36-
if resp.StatusCode != http.StatusOK {
37-
return bodyError(resp)
38-
}
39-
40-
return nil
30+
}, nil)
4131
}
4232

43-
type createDevURLRequest struct {
33+
// CreateDevURLReq defines the request parameters for creating a new DevURL.
34+
type CreateDevURLReq struct {
4435
EnvID string `json:"environment_id"`
4536
Port int `json:"port"`
4637
Access string `json:"access"`
4738
Name string `json:"name"`
4839
}
4940

50-
// InsertDevURL inserts a new devurl for the authenticated user.
51-
func (c Client) InsertDevURL(ctx context.Context, envID string, port int, name, access string) error {
52-
reqURL := fmt.Sprintf("/api/environments/%s/devurls", envID)
53-
54-
resp, err := c.request(ctx, http.MethodPost, reqURL, createDevURLRequest{
55-
EnvID: envID,
56-
Port: port,
57-
Access: access,
58-
Name: name,
59-
})
60-
if err != nil {
61-
return err
62-
}
63-
defer func() { _ = resp.Body.Close() }() // Best effort. Likely connection drop.
64-
65-
if resp.StatusCode != http.StatusOK {
66-
return bodyError(resp)
67-
}
68-
69-
return nil
41+
// CreateDevURL inserts a new devurl for the authenticated user.
42+
func (c Client) CreateDevURL(ctx context.Context, envID string, req CreateDevURLReq) error {
43+
return c.requestBody(ctx, http.MethodPost, "/api/environments/"+envID+"/devurls", req, nil)
7044
}
7145

72-
type updateDevURLRequest createDevURLRequest
73-
74-
// UpdateDevURL updates an existing devurl for the authenticated user.
75-
func (c Client) UpdateDevURL(ctx context.Context, envID, urlID string, port int, name, access string) error {
76-
reqURL := fmt.Sprintf("/api/environments/%s/devurls/%s", envID, urlID)
77-
78-
resp, err := c.request(ctx, http.MethodPut, reqURL, updateDevURLRequest{
79-
EnvID: envID,
80-
Port: port,
81-
Access: access,
82-
Name: name,
83-
})
84-
if err != nil {
85-
return err
86-
}
87-
defer func() { _ = resp.Body.Close() }() // Best effort. Likefly connection drop.
88-
89-
if resp.StatusCode != http.StatusOK {
90-
return bodyError(resp)
91-
}
46+
// PutDevURLReq defines the request parameters for overwriting a DevURL.
47+
type PutDevURLReq CreateDevURLReq
9248

93-
return nil
49+
// PutDevURL updates an existing devurl for the authenticated user.
50+
func (c Client) PutDevURL(ctx context.Context, envID, urlID string, req PutDevURLReq) error {
51+
return c.requestBody(ctx, http.MethodPut, "/api/environments/"+envID+"/devurls/"+urlID, req, nil)
9452
}

coder-sdk/error.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ var ErrPermissions = xerrors.New("insufficient permissions")
1717
// ErrAuthentication describes the error case in which the requester has invalid authentication.
1818
var ErrAuthentication = xerrors.New("invalid authentication")
1919

20-
// APIError is the expected payload format for our errors.
21-
type APIError struct {
22-
Err APIErrorMsg `json:"error"`
20+
// apiError is the expected payload format for our errors.
21+
type apiError struct {
22+
Err apiErrorMsg `json:"error"`
2323
}
2424

25-
// APIErrorMsg contains the rich error information returned by API errors.
26-
type APIErrorMsg struct {
25+
// apiErrorMsg contains the rich error information returned by API errors.
26+
type apiErrorMsg struct {
2727
Msg string `json:"msg"`
2828
}
2929

@@ -33,9 +33,9 @@ type HTTPError struct {
3333
}
3434

3535
func (e *HTTPError) Error() string {
36-
var msg APIError
36+
var msg apiError
3737
// Try to decode the payload as an error, if it fails or if there is no error message,
38-
// return the response URL with the dump.
38+
// return the response URL with the status.
3939
if err := json.NewDecoder(e.Response.Body).Decode(&msg); err != nil || msg.Err.Msg == "" {
4040
return fmt.Sprintf("%s: %d %s", e.Request.URL, e.StatusCode, e.Status)
4141
}

coder-sdk/org.go

+12-3
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,18 @@ import (
88

99
// Organization describes an Organization in Coder.
1010
type Organization struct {
11-
ID string `json:"id"`
12-
Name string `json:"name"`
13-
Members []OrganizationUser `json:"members"`
11+
ID string `json:"id"`
12+
Name string `json:"name"`
13+
Description string `json:"description"`
14+
Default bool `json:"default"`
15+
Members []OrganizationUser `json:"members"`
16+
EnvironmentCount int `json:"environment_count"`
17+
ResourceNamespace string `json:"resource_namespace"`
18+
CreatedAt time.Time `json:"created_at"`
19+
UpdatedAt time.Time `json:"updated_at"`
20+
AutoOffThreshold Duration `json:"auto_off_threshold"`
21+
CPUProvisioningRate float32 `json:"cpu_provisioning_rate"`
22+
MemoryProvisioningRate float32 `json:"memory_provisioning_rate"`
1423
}
1524

1625
// OrganizationUser user wraps the basic User type and adds data specific to the user's membership of an organization.

coder-sdk/secrets.go

+23-7
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ import (
77
)
88

99
// Secret describes a Coder secret.
10+
//
11+
// Deprecated: Coder Secrets will be removed from Coder Enterprise in a future release.
1012
type Secret struct {
1113
ID string `json:"id" table:"-"`
1214
Name string `json:"name" table:"Name"`
@@ -17,7 +19,9 @@ type Secret struct {
1719
}
1820

1921
// Secrets gets all secrets for the given user.
20-
func (c *Client) Secrets(ctx context.Context, userID string) ([]Secret, error) {
22+
//
23+
// Deprecated: Coder Secrets will be removed from Coder Enterprise in a future release.
24+
func (c Client) Secrets(ctx context.Context, userID string) ([]Secret, error) {
2125
var secrets []Secret
2226
if err := c.requestBody(ctx, http.MethodGet, "/api/users/"+userID+"/secrets", nil, &secrets); err != nil {
2327
return nil, err
@@ -26,7 +30,9 @@ func (c *Client) Secrets(ctx context.Context, userID string) ([]Secret, error) {
2630
}
2731

2832
// SecretWithValueByName gets the Coder secret with its value by its name.
29-
func (c *Client) SecretWithValueByName(ctx context.Context, name, userID string) (*Secret, error) {
33+
//
34+
// Deprecated: Coder Secrets will be removed from Coder Enterprise in a future release.
35+
func (c Client) SecretWithValueByName(ctx context.Context, name, userID string) (*Secret, error) {
3036
// Lookup the secret from the name.
3137
s, err := c.SecretByName(ctx, name, userID)
3238
if err != nil {
@@ -43,7 +49,9 @@ func (c *Client) SecretWithValueByName(ctx context.Context, name, userID string)
4349
}
4450

4551
// SecretWithValueByID gets the Coder secret with its value by the secret_id.
46-
func (c *Client) SecretWithValueByID(ctx context.Context, id, userID string) (*Secret, error) {
52+
//
53+
// Deprecated: Coder Secrets will be removed from Coder Enterprise in a future release.
54+
func (c Client) SecretWithValueByID(ctx context.Context, id, userID string) (*Secret, error) {
4755
var secret Secret
4856
if err := c.requestBody(ctx, http.MethodGet, "/api/users/"+userID+"/secrets/"+id, nil, &secret); err != nil {
4957
return nil, err
@@ -52,7 +60,9 @@ func (c *Client) SecretWithValueByID(ctx context.Context, id, userID string) (*S
5260
}
5361

5462
// SecretByName gets a secret object by name.
55-
func (c *Client) SecretByName(ctx context.Context, name, userID string) (*Secret, error) {
63+
//
64+
// Deprecated: Coder Secrets will be removed from Coder Enterprise in a future release.
65+
func (c Client) SecretByName(ctx context.Context, name, userID string) (*Secret, error) {
5666
secrets, err := c.Secrets(ctx, userID)
5767
if err != nil {
5868
return nil, err
@@ -66,19 +76,25 @@ func (c *Client) SecretByName(ctx context.Context, name, userID string) (*Secret
6676
}
6777

6878
// InsertSecretReq describes the request body for creating a new secret.
79+
//
80+
// Deprecated: Coder Secrets will be removed from Coder Enterprise in a future release.
6981
type InsertSecretReq struct {
7082
Name string `json:"name"`
7183
Value string `json:"value"`
7284
Description string `json:"description"`
7385
}
7486

7587
// InsertSecret adds a new secret for the authed user.
76-
func (c *Client) InsertSecret(ctx context.Context, user *User, req InsertSecretReq) error {
77-
return c.requestBody(ctx, http.MethodPost, "/api/users/"+user.ID+"/secrets", req, nil)
88+
//
89+
// Deprecated: Coder Secrets will be removed from Coder Enterprise in a future release.
90+
func (c Client) InsertSecret(ctx context.Context, userID string, req InsertSecretReq) error {
91+
return c.requestBody(ctx, http.MethodPost, "/api/users/"+userID+"/secrets", req, nil)
7892
}
7993

8094
// DeleteSecretByName deletes the authenticated users secret with the given name.
81-
func (c *Client) DeleteSecretByName(ctx context.Context, name, userID string) error {
95+
//
96+
// Deprecated: Coder Secrets will be removed from Coder Enterprise in a future release.
97+
func (c Client) DeleteSecretByName(ctx context.Context, name, userID string) error {
8298
// Lookup the secret by name to get the ID.
8399
secret, err := c.SecretByName(ctx, name, userID)
84100
if err != nil {

docs/coder_envs_create.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
2828
-h, --help help for create
2929
-i, --image string name of the image to base the environment off of.
3030
-m, --memory float32 GB of RAM an environment should be provisioned with.
31-
-o, --org string ID of the organization the environment should be created under.
31+
-o, --org string name of the organization the environment should be created under.
3232
-t, --tag string tag of the image the environment will be based off of. (default "latest")
3333
```
3434

internal/cmd/configssh.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func configSSHCmd() *cobra.Command {
3737
}
3838

3939
func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []string) error {
40-
startToken := "# ------------START-CODER-ENTERPRISE-----------"
40+
const startToken = "# ------------START-CODER-ENTERPRISE-----------"
4141
startMessage := `# The following has been auto-generated by "coder config-ssh"
4242
# to make accessing your Coder Enterprise environments easier.
4343
#
@@ -46,7 +46,7 @@ func configSSH(configpath *string, remove *bool) func(cmd *cobra.Command, _ []st
4646
# coder config-ssh --remove
4747
#
4848
# You should not hand-edit this section, unless you are deleting it.`
49-
endToken := "# ------------END-CODER-ENTERPRISE------------"
49+
const endToken = "# ------------END-CODER-ENTERPRISE------------"
5050

5151
return func(cmd *cobra.Command, _ []string) error {
5252
ctx := cmd.Context()

internal/cmd/envs.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
230230
return nil
231231
},
232232
}
233-
cmd.Flags().StringVarP(&org, "org", "o", "", "ID of the organization the environment should be created under.")
233+
cmd.Flags().StringVarP(&org, "org", "o", "", "name of the organization the environment should be created under.")
234234
cmd.Flags().StringVarP(&tag, "tag", "t", defaultImgTag, "tag of the image the environment will be based off of.")
235235
cmd.Flags().Float32VarP(&cpu, "cpu", "c", 0, "number of cpu cores the environment should be provisioned with.")
236236
cmd.Flags().Float32VarP(&memory, "memory", "m", 0, "GB of RAM an environment should be provisioned with.")

0 commit comments

Comments
 (0)