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