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

Commit 9fc7f8a

Browse files
committed
clean up
1 parent 12e20a0 commit 9fc7f8a

File tree

1 file changed

+64
-37
lines changed

1 file changed

+64
-37
lines changed

internal/cmd/envs.go

Lines changed: 64 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package cmd
22

33
import (
4-
"context"
54
"encoding/json"
65
"os"
76

@@ -14,6 +13,15 @@ import (
1413
"go.coder.com/flog"
1514
)
1615

16+
const (
17+
defaultOrg = "default"
18+
defaultImgTag = "latest"
19+
defaultCPUCores float32 = 1
20+
defaultMemGB float32 = 1
21+
defaultDiskGB = 10
22+
defaultGPUs = 1
23+
)
24+
1725
func envsCommand() *cobra.Command {
1826
var outputFmt string
1927
var user string
@@ -120,61 +128,80 @@ coder envs --user charlie@coder.com ls -o json \
120128
}
121129

122130
func createEnvCommand() *cobra.Command {
123-
var orgID string
124-
createReq := new(coder.CreateEnvironmentRequest)
131+
var (
132+
org string
133+
img string
134+
tag string
135+
services []string
136+
)
125137

126138
cmd := &cobra.Command{
127139
Use: "create",
128140
Short: "create a new environment.",
141+
Args: cobra.ExactArgs(1),
129142
Long: "Create a new environment under the active user.",
143+
Example: `
144+
# create a new environment using default resource amounts
145+
coder envs create \
146+
--image id-of-imported-image \
147+
my-env-name
148+
149+
See Flags section for default values.
150+
151+
# create a new environment using custom resource amounts
152+
coder envs create \
153+
--cores 4 \
154+
--disk 100 \
155+
--memory 8 \
156+
--image id-of-imported-image \
157+
--org id-of-existing-organization \
158+
my-env-name
159+
160+
# same command this time using short-hand flags for a more succinct command experience.
161+
coder envs create \
162+
-c 4 \
163+
-d 100 \
164+
-m 8 \
165+
-i id-of-imported-image my-env-name \
166+
-o id-of-existing-organization \
167+
my-env-name
168+
`,
130169
RunE: func(cmd *cobra.Command, args []string) error {
131-
ctx, cancel := context.WithCancel(context.Background())
132-
defer cancel()
133-
134-
if createReq.ImageTag == "" {
135-
createReq.ImageTag = "latest"
136-
}
137-
138-
if createReq.CPUCores == 0 {
139-
createReq.CPUCores = 1
140-
}
141-
142-
if createReq.MemoryGB == 0 {
143-
createReq.MemoryGB = 1
144-
}
145-
146-
if createReq.DiskGB == 0 {
147-
createReq.DiskGB = 10
148-
}
149-
150-
if createReq.GPUs == 0 {
151-
createReq.GPUs = 1
170+
// We don't need an empty-ness check on img because its value is propogated by a required flag.
171+
// cobra.ExactArgs(1) ensures our name value can't panic on an out of bounds.
172+
createReq := &coder.CreateEnvironmentRequest{
173+
Name: args[0],
174+
ImageID: img,
175+
ImageTag: tag,
152176
}
177+
// We're explicitly ignoring errors for these because all of these flags
178+
// have a non-zero-value default value set already.
179+
createReq.CPUCores, _ = cmd.Flags().GetFloat32("cores")
180+
createReq.MemoryGB, _ = cmd.Flags().GetFloat32("memory")
181+
createReq.DiskGB, _ = cmd.Flags().GetInt("disk")
182+
createReq.GPUs, _ = cmd.Flags().GetInt("gpus")
153183

154184
client, err := newClient()
155185
if err != nil {
156186
return err
157187
}
158188

159-
env, err := client.CreateEnvironment(ctx, orgID, *createReq)
189+
env, err := client.CreateEnvironment(cmd.Context(), org, *createReq)
160190
if err != nil {
161191
return xerrors.Errorf("create environment: %w", err)
162192
}
163193
flog.Success("Successfully created environment %q", env.Name)
164194
return nil
165195
},
166196
}
167-
cmd.Flags().StringVarP(&orgID, "org", "o", "", "ID of the organization the environment should be created under. (optional: false)")
168-
cmd.Flags().StringVarP(&createReq.Name, "name", "n", "", "The name to assign to the environment. (optional: false)")
169-
cmd.Flags().StringVarP(&createReq.ImageID, "img", "i", "", "ID of the image to base the environment off of. (optional: false)")
170-
cmd.Flags().StringVarP(&createReq.ImageTag, "tag", "t", "", "The particular tag of the image the environment will be based off of. (default: latest, optional: true)")
171-
cmd.Flags().Float32P("cores", "c", createReq.CPUCores, "The number of cpu cores the environment should be provisioned with. (default: 1, optional: true, supports fractional amounts)")
172-
cmd.Flags().Float32P("memory", "m", createReq.MemoryGB, "The amount of RAM an environment should be provisioned with. (default: 1, optional: true, supports fractional amounts)")
173-
cmd.Flags().IntP("disk", "d", createReq.DiskGB, "The amount of disk storage an environment should be provisioned with. (default:10, optional: true, supports whole numbers only)")
174-
cmd.Flags().IntP("gpu", "g", createReq.GPUs, "The amount of disk storage to provision the environment with. (default:1, optional: true, supports whole numbers only)")
175-
cmd.Flags().StringSliceP("services", "s", createReq.Services, "The services that the environment should be initialized with. (optional: true)")
176-
cmd.MarkFlagRequired("org")
177-
cmd.MarkFlagRequired("name")
178-
cmd.MarkFlagRequired("img")
197+
cmd.Flags().StringVarP(&org, "org", "o", defaultOrg, "ID of the organization the environment should be created under.")
198+
cmd.Flags().StringVarP(&tag, "tag", "t", defaultImgTag, "The particular tag of the image the environment will be based off of.")
199+
cmd.Flags().Float32P("cores", "c", defaultCPUCores, "The number of cpu cores the environment should be provisioned with.")
200+
cmd.Flags().Float32P("memory", "m", defaultMemGB, "GB of RAM an environment should be provisioned with.")
201+
cmd.Flags().IntP("disk", "d", defaultDiskGB, "GB of disk storage an environment should be provisioned with.")
202+
cmd.Flags().IntP("gpu", "g", defaultGPUs, "The number GPUs an environment should be provisioned with.")
203+
cmd.Flags().StringSliceP("services", "s", services, "The services that the environment should be initialized with.")
204+
cmd.Flags().StringVarP(&img, "image", "i", "", "ID of the image to base the environment off of.")
205+
cmd.MarkFlagRequired("image")
179206
return cmd
180207
}

0 commit comments

Comments
 (0)