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

Commit b92aaf0

Browse files
authored
Unide envs (create|edit) commands (#168)
1 parent 258f3e1 commit b92aaf0

File tree

8 files changed

+164
-101
lines changed

8 files changed

+164
-101
lines changed

ci/integration/envs_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ func TestEnvsCLI(t *testing.T) {
108108
// Successfully output help.
109109
c.Run(ctx, "coder envs create --help").Assert(t,
110110
tcli.Success(),
111-
tcli.StdoutMatches(regexp.QuoteMeta("Create a new environment under the active user.")),
111+
tcli.StdoutMatches(regexp.QuoteMeta("Create a new Coder environment.")),
112112
tcli.StderrEmpty(),
113113
)
114114

coder-sdk/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"net/url"
77
)
88

9-
// Me is the route param to access resources of the authenticated user.
9+
// Me is the user ID of the authenticated user.
1010
const Me = "me"
1111

1212
// Client wraps the Coder HTTP API.

coder-sdk/error.go

+2-8
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"encoding/json"
55
"fmt"
66
"net/http"
7-
"net/http/httputil"
87

98
"golang.org/x/xerrors"
109
)
@@ -34,22 +33,17 @@ type HTTPError struct {
3433
}
3534

3635
func (e *HTTPError) Error() string {
37-
dump, err := httputil.DumpResponse(e.Response, false)
38-
if err != nil {
39-
return fmt.Sprintf("dump response: %+v", err)
40-
}
41-
4236
var msg APIError
4337
// Try to decode the payload as an error, if it fails or if there is no error message,
4438
// return the response URL with the dump.
4539
if err := json.NewDecoder(e.Response.Body).Decode(&msg); err != nil || msg.Err.Msg == "" {
46-
return fmt.Sprintf("%s\n%s", e.Response.Request.URL, dump)
40+
return fmt.Sprintf("%s: %d %s", e.Request.URL, e.StatusCode, e.Status)
4741
}
4842

4943
// If the payload was a in the expected error format with a message, include it.
5044
return msg.Err.Msg
5145
}
5246

5347
func bodyError(resp *http.Response) error {
54-
return &HTTPError{resp}
48+
return &HTTPError{Response: resp}
5549
}

docs/coder_envs.md

+2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ Perform operations on the Coder environments owned by the active user.
2222
### SEE ALSO
2323

2424
* [coder](coder.md) - coder provides a CLI for working with an existing Coder Enterprise installation
25+
* [coder envs create](coder_envs_create.md) - create a new environment.
26+
* [coder envs edit](coder_envs_edit.md) - edit an existing environment and initiate a rebuild.
2527
* [coder envs ls](coder_envs_ls.md) - list all environments owned by the active user
2628
* [coder envs rebuild](coder_envs_rebuild.md) - rebuild a Coder environment
2729
* [coder envs rm](coder_envs_rm.md) - remove Coder environments by name

docs/coder_envs_create.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## coder envs create
2+
3+
create a new environment.
4+
5+
### Synopsis
6+
7+
Create a new Coder environment.
8+
9+
```
10+
coder envs create [environment_name] [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
# create a new environment using default resource amounts
17+
coder envs create my-new-env --image ubuntu
18+
coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ubuntu
19+
```
20+
21+
### Options
22+
23+
```
24+
-c, --cpu float32 number of cpu cores the environment should be provisioned with.
25+
-d, --disk int GB of disk storage an environment should be provisioned with.
26+
--follow follow buildlog after initiating rebuild
27+
-g, --gpus int number GPUs an environment should be provisioned with.
28+
-h, --help help for create
29+
-i, --image string name of the image to base the environment off of.
30+
-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.
32+
-t, --tag string tag of the image the environment will be based off of. (default "latest")
33+
```
34+
35+
### Options inherited from parent commands
36+
37+
```
38+
--user string Specify the user whose resources to target (default "me")
39+
-v, --verbose show verbose output
40+
```
41+
42+
### SEE ALSO
43+
44+
* [coder envs](coder_envs.md) - Interact with Coder environments
45+

docs/coder_envs_edit.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## coder envs edit
2+
3+
edit an existing environment and initiate a rebuild.
4+
5+
### Synopsis
6+
7+
Edit an existing environment and initate a rebuild.
8+
9+
```
10+
coder envs edit [flags]
11+
```
12+
13+
### Examples
14+
15+
```
16+
coder envs edit back-end-env --cpu 4
17+
18+
coder envs edit back-end-env --disk 20
19+
```
20+
21+
### Options
22+
23+
```
24+
-c, --cpu float32 The number of cpu cores the environment should be provisioned with.
25+
-d, --disk int The amount of disk storage an environment should be provisioned with.
26+
--follow follow buildlog after initiating rebuild
27+
-g, --gpu int The amount of disk storage to provision the environment with.
28+
-h, --help help for edit
29+
-i, --image string name of the image you want the environment to be based off of.
30+
-m, --memory float32 The amount of RAM an environment should be provisioned with.
31+
-o, --org string name of the organization the environment should be created under.
32+
-t, --tag string image tag of the image you want to base the environment off of. (default "latest")
33+
```
34+
35+
### Options inherited from parent commands
36+
37+
```
38+
--user string Specify the user whose resources to target (default "me")
39+
-v, --verbose show verbose output
40+
```
41+
42+
### SEE ALSO
43+
44+
* [coder envs](coder_envs.md) - Interact with Coder environments
45+

internal/cmd/ceapi.go

+12-17
Original file line numberDiff line numberDiff line change
@@ -84,27 +84,23 @@ func findEnv(ctx context.Context, client *coder.Client, envName, userEmail strin
8484
}
8585

8686
type findImgConf struct {
87-
client *coder.Client
8887
email string
8988
imgName string
9089
orgName string
9190
}
9291

93-
func findImg(ctx context.Context, conf findImgConf) (*coder.Image, error) {
92+
func findImg(ctx context.Context, client *coder.Client, conf findImgConf) (*coder.Image, error) {
9493
switch {
9594
case conf.email == "":
9695
return nil, xerrors.New("user email unset")
9796
case conf.imgName == "":
9897
return nil, xerrors.New("image name unset")
9998
}
10099

101-
imgs, err := getImgs(ctx,
102-
getImgsConf{
103-
client: conf.client,
104-
email: conf.email,
105-
orgName: conf.orgName,
106-
},
107-
)
100+
imgs, err := getImgs(ctx, client, getImgsConf{
101+
email: conf.email,
102+
orgName: conf.orgName,
103+
})
108104
if err != nil {
109105
return nil, err
110106
}
@@ -129,38 +125,37 @@ func findImg(ctx context.Context, conf findImgConf) (*coder.Image, error) {
129125
return nil, xerrors.New("image not found - did you forget to import this image?")
130126
}
131127

132-
lines := []string{clog.Tipf("Did you mean?")}
128+
lines := []string{clog.Hintf("Did you mean?")}
133129

134130
for _, img := range possibleMatches {
135-
lines = append(lines, img.Repository)
131+
lines = append(lines, fmt.Sprintf(" %s", img.Repository))
136132
}
137133
return nil, clog.Fatal(
138-
fmt.Sprintf("Found %d possible matches for %q.", len(possibleMatches), conf.imgName),
134+
fmt.Sprintf("image %s not found", conf.imgName),
139135
lines...,
140136
)
141137
}
142138

143139
type getImgsConf struct {
144-
client *coder.Client
145140
email string
146141
orgName string
147142
}
148143

149-
func getImgs(ctx context.Context, conf getImgsConf) ([]coder.Image, error) {
150-
u, err := conf.client.UserByEmail(ctx, conf.email)
144+
func getImgs(ctx context.Context, client *coder.Client, conf getImgsConf) ([]coder.Image, error) {
145+
u, err := client.UserByEmail(ctx, conf.email)
151146
if err != nil {
152147
return nil, err
153148
}
154149

155-
orgs, err := conf.client.Organizations(ctx)
150+
orgs, err := client.Organizations(ctx)
156151
if err != nil {
157152
return nil, err
158153
}
159154

160155
orgs = lookupUserOrgs(u, orgs)
161156

162157
for _, org := range orgs {
163-
imgs, err := conf.client.OrganizationImages(ctx, org.ID)
158+
imgs, err := client.OrganizationImages(ctx, org.ID)
164159
if err != nil {
165160
return nil, err
166161
}

0 commit comments

Comments
 (0)