From 583df7c162d1c913d6ba979325b1f1867db63956 Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Thu, 7 Jan 2021 08:42:47 +0000 Subject: [PATCH] feat: add envs create-from-repo subcommand --- coder-sdk/env.go | 40 +++++++++++++++++++++-------- internal/cmd/envs.go | 61 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 11 deletions(-) diff --git a/coder-sdk/env.go b/coder-sdk/env.go index cdaa8bab..69eb05f5 100644 --- a/coder-sdk/env.go +++ b/coder-sdk/env.go @@ -73,16 +73,17 @@ const ( // CreateEnvironmentRequest is used to configure a new environment. type CreateEnvironmentRequest struct { - Name string `json:"name"` - ImageID string `json:"image_id"` - OrgID string `json:"org_id"` - ImageTag string `json:"image_tag"` - CPUCores float32 `json:"cpu_cores"` - MemoryGB float32 `json:"memory_gb"` - DiskGB int `json:"disk_gb"` - GPUs int `json:"gpus"` - Services []string `json:"services"` - UseContainerVM bool `json:"use_container_vm"` + Name string `json:"name"` + ImageID string `json:"image_id"` + OrgID string `json:"org_id"` + ImageTag string `json:"image_tag"` + CPUCores float32 `json:"cpu_cores"` + MemoryGB float32 `json:"memory_gb"` + DiskGB int `json:"disk_gb"` + GPUs int `json:"gpus"` + Services []string `json:"services"` + UseContainerVM bool `json:"use_container_vm"` + Template *Template `json:"template"` } // CreateEnvironment sends a request to create an environment. @@ -94,6 +95,25 @@ func (c Client) CreateEnvironment(ctx context.Context, req CreateEnvironmentRequ return &env, nil } +// Template is used to configure a new environment from a repo. +// It is currently in alpha and subject to API-breaking change. +type Template struct { + RepositoryURL string `json:"repository_url"` + // Optional. The default branch will be used if not provided. + Branch string `json:"branch"` + // Optional. The template name will be used if not provided. + Name string `json:"name"` +} + +// CreateEnvironmentFromRepo sends a request to create an environment from a repository. +func (c Client) CreateEnvironmentFromRepo(ctx context.Context, orgID string, req Template) (*Environment, error) { + var env Environment + if err := c.requestBody(ctx, http.MethodPost, "/api/private/orgs/"+orgID+"/environments/from-repo", req, &env); err != nil { + return nil, err + } + return &env, nil +} + // Environments lists environments returned by the given filter. // TODO: add the filter options, explore performance issue. func (c Client) Environments(ctx context.Context) ([]Environment, error) { diff --git a/internal/cmd/envs.go b/internal/cmd/envs.go index 4290dfe5..ce517ea0 100644 --- a/internal/cmd/envs.go +++ b/internal/cmd/envs.go @@ -32,6 +32,7 @@ func envsCmd() *cobra.Command { watchBuildLogCommand(), rebuildEnvCommand(), createEnvCmd(), + createEnvFromRepoCmd(), editEnvCmd(), ) return cmd @@ -184,7 +185,6 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub if multiOrgMember && org == "" { return xerrors.New("org is required for multi-org members") } - importedImg, err := findImg(ctx, client, findImgConf{ email: coder.Me, imgName: img, @@ -252,6 +252,65 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub return cmd } +func createEnvFromRepoCmd() *cobra.Command { + var ( + branch string + name string + follow bool + ) + + cmd := &cobra.Command{ + Use: "create-from-repo [environment_name]", + Short: "create a new environment from a git repository.", + Args: xcobra.ExactArgs(1), + Long: "Create a new Coder environment from a Git repository.", + Hidden: true, + Example: `# create a new environment from git repository template +coder envs create-from-repo github.com/cdr/m +coder envs create-from-repo github.com/cdr/m --branch envs-as-code`, + RunE: func(cmd *cobra.Command, args []string) error { + ctx := cmd.Context() + + client, err := newClient(ctx) + if err != nil { + return err + } + + // ExactArgs(1) ensures our name value can't panic on an out of bounds. + createReq := &coder.Template{ + RepositoryURL: args[0], + Branch: branch, + Name: name, + } + + env, err := client.CreateEnvironment(ctx, coder.CreateEnvironmentRequest{ + Template: createReq, + }) + if err != nil { + return xerrors.Errorf("create environment: %w", err) + } + + if follow { + clog.LogSuccess("creating environment...") + if err := trailBuildLogs(ctx, client, env.ID); err != nil { + return err + } + return nil + } + + clog.LogSuccess("creating environment...", + clog.BlankLine, + clog.Tipf(`run "coder envs watch-build %s" to trail the build logs`, env.Name), + ) + return nil + }, + } + cmd.Flags().StringVarP(&branch, "branch", "b", "master", "name of the branch to create the environment from.") + cmd.Flags().StringVarP(&name, "name", "n", "coder.yaml", "name of the config file.") + cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild") + return cmd +} + func editEnvCmd() *cobra.Command { var ( org string pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy