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

feat: add envs create-from-repo subcommand #212

Merged
merged 1 commit into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 30 additions & 10 deletions coder-sdk/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {
Expand Down
61 changes: 60 additions & 1 deletion internal/cmd/envs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func envsCmd() *cobra.Command {
watchBuildLogCommand(),
rebuildEnvCommand(),
createEnvCmd(),
createEnvFromRepoCmd(),
editEnvCmd(),
)
return cmd
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
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