@@ -13,6 +13,15 @@ import (
13
13
"go.coder.com/flog"
14
14
)
15
15
16
+ const (
17
+ defaultOrg = "default"
18
+ defaultImgTag = "latest"
19
+ defaultCPUCores float32 = 1
20
+ defaultMemGB float32 = 1
21
+ defaultDiskGB = 10
22
+ defaultGPUs = 0
23
+ )
24
+
16
25
func envsCommand () * cobra.Command {
17
26
var outputFmt string
18
27
var user string
@@ -63,9 +72,9 @@ func envsCommand() *cobra.Command {
63
72
lsCmd .Flags ().StringVarP (& outputFmt , "output" , "o" , "human" , "human | json" )
64
73
cmd .AddCommand (lsCmd )
65
74
cmd .AddCommand (stopEnvCommand (& user ))
66
-
67
75
cmd .AddCommand (watchBuildLogCommand ())
68
76
cmd .AddCommand (rebuildEnvCommand ())
77
+ cmd .AddCommand (createEnvCommand ())
69
78
return cmd
70
79
}
71
80
@@ -117,3 +126,85 @@ coder envs --user charlie@coder.com ls -o json \
117
126
},
118
127
}
119
128
}
129
+
130
+ func createEnvCommand () * cobra.Command {
131
+ var (
132
+ org string
133
+ img string
134
+ tag string
135
+ services []string
136
+ )
137
+
138
+ cmd := & cobra.Command {
139
+ Use : "create [flags] [environment-name]" ,
140
+ Short : "create a new environment." ,
141
+ Args : cobra .ExactArgs (1 ),
142
+ Hidden : true ,
143
+ Long : "Create a new environment under the active user." ,
144
+ Example : `
145
+ # create a new environment using default resource amounts
146
+ coder envs create \
147
+ --image id-of-imported-image \
148
+ my-env-name
149
+
150
+ See Flags section for default values.
151
+
152
+ # create a new environment using custom resource amounts
153
+ coder envs create \
154
+ --cores 4 \
155
+ --disk 100 \
156
+ --memory 8 \
157
+ --image id-of-imported-image \
158
+ --org id-of-existing-organization \
159
+ my-env-name
160
+
161
+ # same command this time using short-hand flags for a more succinct command experience.
162
+ coder envs create \
163
+ -c 4 \
164
+ -d 100 \
165
+ -m 8 \
166
+ -i id-of-imported-image my-env-name \
167
+ -o id-of-existing-organization \
168
+ my-env-name
169
+ ` ,
170
+ RunE : func (cmd * cobra.Command , args []string ) error {
171
+ if img == "" {
172
+ return xerrors .New ("image id unset" )
173
+ }
174
+ // ExactArgs(1) ensures our name value can't panic on an out of bounds.
175
+ createReq := & coder.CreateEnvironmentRequest {
176
+ Name : args [0 ],
177
+ ImageID : img ,
178
+ ImageTag : tag ,
179
+ }
180
+ // We're explicitly ignoring errors for these because all of these flags
181
+ // have a non-zero-value default value set already.
182
+ createReq .CPUCores , _ = cmd .Flags ().GetFloat32 ("cores" )
183
+ createReq .MemoryGB , _ = cmd .Flags ().GetFloat32 ("memory" )
184
+ createReq .DiskGB , _ = cmd .Flags ().GetInt ("disk" )
185
+ createReq .GPUs , _ = cmd .Flags ().GetInt ("gpus" )
186
+
187
+ client , err := newClient ()
188
+ if err != nil {
189
+ return err
190
+ }
191
+
192
+ env , err := client .CreateEnvironment (cmd .Context (), org , * createReq )
193
+ if err != nil {
194
+ return xerrors .Errorf ("create environment: %w" , err )
195
+ }
196
+ flog .Success ("Successfully created environment %q" , env .Name )
197
+ return nil
198
+ },
199
+ }
200
+ cmd .Flags ().StringVarP (& org , "org" , "o" , defaultOrg , "ID of the organization the environment should be created under." )
201
+ cmd .Flags ().StringVarP (& tag , "tag" , "t" , defaultImgTag , "The particular tag of the image the environment will be based off of." )
202
+ cmd .Flags ().Float32P ("cores" , "c" , defaultCPUCores , "The number of cpu cores the environment should be provisioned with." )
203
+ cmd .Flags ().Float32P ("memory" , "m" , defaultMemGB , "GB of RAM an environment should be provisioned with." )
204
+ cmd .Flags ().IntP ("disk" , "d" , defaultDiskGB , "GB of disk storage an environment should be provisioned with." )
205
+ cmd .Flags ().IntP ("gpus" , "g" , defaultGPUs , "The number GPUs an environment should be provisioned with." )
206
+ cmd .Flags ().StringSliceP ("services" , "s" , services , "The services that the environment should be initialized with." )
207
+ cmd .Flags ().StringVarP (& img , "image" , "i" , "" , "ID of the image to base the environment off of." )
208
+ cmd .MarkFlagRequired ("image" )
209
+ return cmd
210
+ }
0 commit comments