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

Commit 12e20a0

Browse files
committed
Add envs create command
1 parent 282f351 commit 12e20a0

File tree

3 files changed

+64
-2
lines changed

3 files changed

+64
-2
lines changed

coder-sdk/env.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ type CreateEnvironmentRequest struct {
7878
ImageID string `json:"image_id"`
7979
ImageTag string `json:"image_tag"`
8080
CPUCores float32 `json:"cpu_cores"`
81-
MemoryGB int `json:"memory_gb"`
81+
MemoryGB float32 `json:"memory_gb"`
8282
DiskGB int `json:"disk_gb"`
8383
GPUs int `json:"gpus"`
8484
Services []string `json:"services"`

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,7 @@ golang.org/x/crypto v0.0.0-20190605123033-f99c8df09eb5/go.mod h1:yigFU9vqHzYiE8U
275275
golang.org/x/crypto v0.0.0-20191206172530-e9b2fee46413/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
276276
golang.org/x/crypto v0.0.0-20200422194213-44a606286825 h1:dSChiwOTvzwbHFTMq2l6uRardHH7/E6SqEkqccinS/o=
277277
golang.org/x/crypto v0.0.0-20200422194213-44a606286825/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
278+
golang.org/x/crypto v0.0.0-20201016220609-9e8e0b390897 h1:pLI5jrR7OSLijeIDcmRxNmw2api+jEfxLoykJVice/E=
278279
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
279280
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
280281
golang.org/x/exp v0.0.0-20190510132918-efd6b22b2522/go.mod h1:ZjyILWgesfNpC6sMxTJOJm9Kp84zZh5NQWvqDGG3Qr8=

internal/cmd/envs.go

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

33
import (
4+
"context"
45
"encoding/json"
56
"os"
67

@@ -63,9 +64,9 @@ func envsCommand() *cobra.Command {
6364
lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")
6465
cmd.AddCommand(lsCmd)
6566
cmd.AddCommand(stopEnvCommand(&user))
66-
6767
cmd.AddCommand(watchBuildLogCommand())
6868
cmd.AddCommand(rebuildEnvCommand())
69+
cmd.AddCommand(createEnvCommand())
6970
return cmd
7071
}
7172

@@ -117,3 +118,63 @@ coder envs --user charlie@coder.com ls -o json \
117118
},
118119
}
119120
}
121+
122+
func createEnvCommand() *cobra.Command {
123+
var orgID string
124+
createReq := new(coder.CreateEnvironmentRequest)
125+
126+
cmd := &cobra.Command{
127+
Use: "create",
128+
Short: "create a new environment.",
129+
Long: "Create a new environment under the active user.",
130+
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
152+
}
153+
154+
client, err := newClient()
155+
if err != nil {
156+
return err
157+
}
158+
159+
env, err := client.CreateEnvironment(ctx, orgID, *createReq)
160+
if err != nil {
161+
return xerrors.Errorf("create environment: %w", err)
162+
}
163+
flog.Success("Successfully created environment %q", env.Name)
164+
return nil
165+
},
166+
}
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")
179+
return cmd
180+
}

0 commit comments

Comments
 (0)