|
1 | 1 | package cmd
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "context" |
4 | 5 | "encoding/json"
|
5 | 6 | "os"
|
6 | 7 |
|
@@ -63,9 +64,9 @@ func envsCommand() *cobra.Command {
|
63 | 64 | lsCmd.Flags().StringVarP(&outputFmt, "output", "o", "human", "human | json")
|
64 | 65 | cmd.AddCommand(lsCmd)
|
65 | 66 | cmd.AddCommand(stopEnvCommand(&user))
|
66 |
| - |
67 | 67 | cmd.AddCommand(watchBuildLogCommand())
|
68 | 68 | cmd.AddCommand(rebuildEnvCommand())
|
| 69 | + cmd.AddCommand(createEnvCommand()) |
69 | 70 | return cmd
|
70 | 71 | }
|
71 | 72 |
|
@@ -117,3 +118,63 @@ coder envs --user charlie@coder.com ls -o json \
|
117 | 118 | },
|
118 | 119 | }
|
119 | 120 | }
|
| 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