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

Commit 806e190

Browse files
committed
Style cleanup
1 parent 258f3e1 commit 806e190

File tree

2 files changed

+66
-89
lines changed

2 files changed

+66
-89
lines changed

internal/cmd/ceapi.go

+10-15
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,
100+
imgs, err := getImgs(ctx, client, getImgsConf{
104101
email: conf.email,
105102
orgName: conf.orgName,
106-
},
107-
)
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
}

internal/cmd/envs.go

+56-74
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,10 @@ coder envs --user charlie@coder.com ls -o json \
141141
func createEnvCmd(user *string) *cobra.Command {
142142
var (
143143
org string
144+
cpu float32
145+
memory float32
146+
disk int
147+
gpus int
144148
img string
145149
tag string
146150
follow bool
@@ -150,14 +154,10 @@ func createEnvCmd(user *string) *cobra.Command {
150154
Use: "create [environment_name]",
151155
Short: "create a new environment.",
152156
Args: cobra.ExactArgs(1),
153-
// Don't unhide this command until we can pass image names instead of image id's.
154-
Hidden: true,
155-
Long: "Create a new environment under the active user.",
157+
Long: "Create a new Coder environment.",
156158
Example: `# create a new environment using default resource amounts
157-
coder envs create --image 5f443b16-30652892427b955601330fa5 my-env-name
158-
159-
# create a new environment using custom resource amounts
160-
coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955601330fa5 my-env-name`,
159+
coder envs create my-new-env --image ubuntu
160+
coder envs create my-new-powerfull-env --cpu 12 --disk 100 --memory 16 --image ubuntu`,
161161
RunE: func(cmd *cobra.Command, args []string) error {
162162
ctx := cmd.Context()
163163
if img == "" {
@@ -178,14 +178,11 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
178178
return xerrors.New("org is required for multi-org members")
179179
}
180180

181-
importedImg, err := findImg(ctx,
182-
findImgConf{
183-
client: client,
184-
email: *user,
185-
imgName: img,
186-
orgName: org,
187-
},
188-
)
181+
importedImg, err := findImg(ctx, client, findImgConf{
182+
email: *user,
183+
imgName: img,
184+
orgName: org,
185+
})
189186
if err != nil {
190187
return err
191188
}
@@ -195,13 +192,11 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
195192
Name: args[0],
196193
ImageID: importedImg.ID,
197194
ImageTag: tag,
195+
CPUCores: cpu,
196+
MemoryGB: memory,
197+
DiskGB: disk,
198+
GPUs: gpus,
198199
}
199-
// We're explicitly ignoring errors for these because all we
200-
// need to now is if the numeric type is 0 or not.
201-
createReq.CPUCores, _ = cmd.Flags().GetFloat32("cpu")
202-
createReq.MemoryGB, _ = cmd.Flags().GetFloat32("memory")
203-
createReq.DiskGB, _ = cmd.Flags().GetInt("disk")
204-
createReq.GPUs, _ = cmd.Flags().GetInt("gpus")
205200

206201
// if any of these defaulted to their zero value we provision
207202
// the create request with the imported image defaults instead.
@@ -230,17 +225,17 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
230225

231226
clog.LogSuccess("creating environment...",
232227
clog.BlankLine,
233-
clog.Tipf(`run "coder envs watch-build %q" to trail the build logs`, env.Name),
228+
clog.Tipf(`run "coder envs watch-build %s" to trail the build logs`, env.Name),
234229
)
235230
return nil
236231
},
237232
}
238233
cmd.Flags().StringVarP(&org, "org", "o", "", "ID of the organization the environment should be created under.")
239234
cmd.Flags().StringVarP(&tag, "tag", "t", defaultImgTag, "tag of the image the environment will be based off of.")
240-
cmd.Flags().Float32P("cpu", "c", 0, "number of cpu cores the environment should be provisioned with.")
241-
cmd.Flags().Float32P("memory", "m", 0, "GB of RAM an environment should be provisioned with.")
242-
cmd.Flags().IntP("disk", "d", 0, "GB of disk storage an environment should be provisioned with.")
243-
cmd.Flags().IntP("gpus", "g", 0, "number GPUs an environment should be provisioned with.")
235+
cmd.Flags().Float32VarP(&cpu, "cpu", "c", 0, "number of cpu cores the environment should be provisioned with.")
236+
cmd.Flags().Float32VarP(&memory, "memory", "m", 0, "GB of RAM an environment should be provisioned with.")
237+
cmd.Flags().IntVarP(&disk, "disk", "d", 0, "GB of disk storage an environment should be provisioned with.")
238+
cmd.Flags().IntVarP(&gpus, "gpus", "g", 0, "number GPUs an environment should be provisioned with.")
244239
cmd.Flags().StringVarP(&img, "image", "i", "", "name of the image to base the environment off of.")
245240
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild")
246241
_ = cmd.MarkFlagRequired("image")
@@ -249,22 +244,21 @@ coder envs create --cpu 4 --disk 100 --memory 8 --image 5f443b16-30652892427b955
249244

250245
func editEnvCmd(user *string) *cobra.Command {
251246
var (
252-
org string
253-
img string
254-
tag string
255-
cpuCores float32
256-
memGB float32
257-
diskGB int
258-
gpus int
259-
follow bool
247+
org string
248+
img string
249+
tag string
250+
cpu float32
251+
memory float32
252+
disk int
253+
gpus int
254+
follow bool
260255
)
261256

262257
cmd := &cobra.Command{
263-
Use: "edit",
264-
Short: "edit an existing environment owned by the active user.",
265-
Args: cobra.ExactArgs(1),
266-
Hidden: true,
267-
Long: "Edit an existing environment owned by the active user.",
258+
Use: "edit",
259+
Short: "edit an existing environment and initiate a rebuild.",
260+
Args: cobra.ExactArgs(1),
261+
Long: "Edit an existing environment and initate a rebuild.",
268262
Example: `coder envs edit back-end-env --cpu 4
269263
270264
coder envs edit back-end-env --disk 20`,
@@ -292,25 +286,17 @@ coder envs edit back-end-env --disk 20`,
292286
return xerrors.New("org is required for multi-org members")
293287
}
294288

295-
cpuCores, _ = cmd.Flags().GetFloat32("cpu")
296-
memGB, _ = cmd.Flags().GetFloat32("memory")
297-
diskGB, _ = cmd.Flags().GetInt("disk")
298-
gpus, _ = cmd.Flags().GetInt("gpus")
299-
300-
req, err := buildUpdateReq(ctx,
301-
updateConf{
302-
cpu: cpuCores,
303-
memGB: memGB,
304-
diskGB: diskGB,
305-
gpus: gpus,
306-
client: client,
307-
environment: env,
308-
user: user,
309-
image: img,
310-
imageTag: tag,
311-
orgName: org,
312-
},
313-
)
289+
req, err := buildUpdateReq(ctx, client, updateConf{
290+
cpu: cpu,
291+
memGB: memory,
292+
diskGB: disk,
293+
gpus: gpus,
294+
environment: env,
295+
user: user,
296+
image: img,
297+
imageTag: tag,
298+
orgName: org,
299+
})
314300
if err != nil {
315301
return err
316302
}
@@ -329,18 +315,18 @@ coder envs edit back-end-env --disk 20`,
329315

330316
clog.LogSuccess("applied changes to the environment, rebuilding...",
331317
clog.BlankLine,
332-
clog.Tipf(`run "coder envs watch-build %q" to trail the build logs`, envName),
318+
clog.Tipf(`run "coder envs watch-build %s" to trail the build logs`, envName),
333319
)
334320
return nil
335321
},
336322
}
337323
cmd.Flags().StringVarP(&org, "org", "o", "", "name of the organization the environment should be created under.")
338324
cmd.Flags().StringVarP(&img, "image", "i", "", "name of the image you want the environment to be based off of.")
339325
cmd.Flags().StringVarP(&tag, "tag", "t", "latest", "image tag of the image you want to base the environment off of.")
340-
cmd.Flags().Float32P("cpu", "c", cpuCores, "The number of cpu cores the environment should be provisioned with.")
341-
cmd.Flags().Float32P("memory", "m", memGB, "The amount of RAM an environment should be provisioned with.")
342-
cmd.Flags().IntP("disk", "d", diskGB, "The amount of disk storage an environment should be provisioned with.")
343-
cmd.Flags().IntP("gpu", "g", gpus, "The amount of disk storage to provision the environment with.")
326+
cmd.Flags().Float32VarP(&cpu, "cpu", "c", 0, "The number of cpu cores the environment should be provisioned with.")
327+
cmd.Flags().Float32VarP(&memory, "memory", "m", 0, "The amount of RAM an environment should be provisioned with.")
328+
cmd.Flags().IntVarP(&disk, "disk", "d", 0, "The amount of disk storage an environment should be provisioned with.")
329+
cmd.Flags().IntVarP(&gpus, "gpu", "g", 0, "The amount of disk storage to provision the environment with.")
344330
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild")
345331
return cmd
346332
}
@@ -364,7 +350,7 @@ func rmEnvsCmd(user *string) *cobra.Command {
364350
}
365351
if _, err := confirm.Run(); err != nil {
366352
return clog.Fatal(
367-
"failed to confirm prompt", clog.BlankLine,
353+
"failed to confirm deletion", clog.BlankLine,
368354
clog.Tipf(`use "--force" to rebuild without a confirmation prompt`),
369355
)
370356
}
@@ -400,15 +386,14 @@ type updateConf struct {
400386
memGB float32
401387
diskGB int
402388
gpus int
403-
client *coder.Client
404389
environment *coder.Environment
405390
user *string
406391
image string
407392
imageTag string
408393
orgName string
409394
}
410395

411-
func buildUpdateReq(ctx context.Context, conf updateConf) (*coder.UpdateEnvironmentReq, error) {
396+
func buildUpdateReq(ctx context.Context, client *coder.Client, conf updateConf) (*coder.UpdateEnvironmentReq, error) {
412397
var (
413398
updateReq coder.UpdateEnvironmentReq
414399
defaultCPUCores float32
@@ -418,14 +403,11 @@ func buildUpdateReq(ctx context.Context, conf updateConf) (*coder.UpdateEnvironm
418403

419404
// If this is not empty it means the user is requesting to change the environment image.
420405
if conf.image != "" {
421-
importedImg, err := findImg(ctx,
422-
findImgConf{
423-
client: conf.client,
424-
email: *conf.user,
425-
imgName: conf.image,
426-
orgName: conf.orgName,
427-
},
428-
)
406+
importedImg, err := findImg(ctx, client, findImgConf{
407+
email: *conf.user,
408+
imgName: conf.image,
409+
orgName: conf.orgName,
410+
})
429411
if err != nil {
430412
return nil, err
431413
}

0 commit comments

Comments
 (0)