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

Commit 921d24d

Browse files
committed
feat: add envs create-from-repo subcommand
1 parent ed09747 commit 921d24d

File tree

2 files changed

+98
-1
lines changed

2 files changed

+98
-1
lines changed

coder-sdk/env.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,24 @@ func (c Client) CreateEnvironment(ctx context.Context, orgID string, req CreateE
9292
return &env, nil
9393
}
9494

95+
// CreateEnvironmentFromRepoRequest is used to configure a new environment from a repo.
96+
type CreateEnvironmentFromRepoRequest struct {
97+
RepositoryURL string `json:"repository_url"`
98+
// Optional. The default branch will be used if not provided.
99+
Branch string `json:"branch"`
100+
// Optional. The template name will be used if not provided.
101+
Name string `json:"name"`
102+
}
103+
104+
// CreateEnvironmentFromRepo sends a request to create an environment from a repository.
105+
func (c Client) CreateEnvironmentFromRepo(ctx context.Context, orgID string, req CreateEnvironmentFromRepoRequest) (*Environment, error) {
106+
var env Environment
107+
if err := c.requestBody(ctx, http.MethodPost, "/api/private/orgs/"+orgID+"/environments/from-repo", req, &env); err != nil {
108+
return nil, err
109+
}
110+
return &env, nil
111+
}
112+
95113
// Environments lists environments returned by the given filter.
96114
// TODO: add the filter options, explore performance issue.
97115
func (c Client) Environments(ctx context.Context) ([]Environment, error) {

internal/cmd/envs.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func envsCmd() *cobra.Command {
3131
watchBuildLogCommand(),
3232
rebuildEnvCommand(),
3333
createEnvCmd(),
34+
createEnvFromRepoCmd(),
3435
editEnvCmd(),
3536
)
3637
return cmd
@@ -183,7 +184,6 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
183184
if multiOrgMember && org == "" {
184185
return xerrors.New("org is required for multi-org members")
185186
}
186-
187187
importedImg, err := findImg(ctx, client, findImgConf{
188188
email: coder.Me,
189189
imgName: img,
@@ -250,6 +250,85 @@ coder envs create my-new-powerful-env --cpu 12 --disk 100 --memory 16 --image ub
250250
return cmd
251251
}
252252

253+
func createEnvFromRepoCmd() *cobra.Command {
254+
var (
255+
org string
256+
branch string
257+
name string
258+
follow bool
259+
)
260+
261+
cmd := &cobra.Command{
262+
Use: "create-from-repo [environment_name]",
263+
Short: "create a new environment from a git repository.",
264+
Args: cobra.ExactArgs(1),
265+
Long: "Create a new Coder environment from a Git repository.",
266+
Hidden: true,
267+
Example: `# create a new environment from git repository template
268+
coder envs create-from-repo github.com/cdr/m
269+
coder envs create-from-repo github.com/cdr/m --branch envs-as-code`,
270+
RunE: func(cmd *cobra.Command, args []string) error {
271+
ctx := cmd.Context()
272+
273+
client, err := newClient(ctx)
274+
if err != nil {
275+
return err
276+
}
277+
278+
multiOrgMember, err := isMultiOrgMember(ctx, client, coder.Me)
279+
if err != nil {
280+
return err
281+
}
282+
283+
if multiOrgMember && org == "" {
284+
return xerrors.New("org is required for multi-org members")
285+
}
286+
287+
if org == "" {
288+
// Definitely janky... but since we aren't importing
289+
// an image we need to get the org name somehow.
290+
orgs, err := client.Organizations(ctx)
291+
if err != nil {
292+
return err
293+
}
294+
org = orgs[0].Name
295+
}
296+
297+
// ExactArgs(1) ensures our name value can't panic on an out of bounds.
298+
createReq := &coder.CreateEnvironmentFromRepoRequest{
299+
RepositoryURL: args[0],
300+
Name: name,
301+
Branch: branch,
302+
}
303+
304+
env, err := client.CreateEnvironmentFromRepo(ctx, org, *createReq)
305+
if err != nil {
306+
return xerrors.Errorf("create environment: %w", err)
307+
}
308+
309+
if follow {
310+
clog.LogSuccess("creating environment...")
311+
if err := trailBuildLogs(ctx, client, env.ID); err != nil {
312+
return err
313+
}
314+
return nil
315+
}
316+
317+
clog.LogSuccess("creating environment...",
318+
clog.BlankLine,
319+
clog.Tipf(`run "coder envs watch-build %s" to trail the build logs`, env.Name),
320+
)
321+
return nil
322+
},
323+
}
324+
cmd.Flags().StringVarP(&org, "org", "o", "", "name of the organization the environment should be created under.")
325+
cmd.Flags().StringVarP(&name, "name", "n", "", "name of the environment to create.")
326+
cmd.Flags().StringVarP(&branch, "branch", "b", "", "name of the branch to create the environment from.")
327+
cmd.Flags().BoolVar(&follow, "follow", false, "follow buildlog after initiating rebuild")
328+
_ = cmd.MarkFlagRequired("image")
329+
return cmd
330+
}
331+
253332
func editEnvCmd() *cobra.Command {
254333
var (
255334
org string

0 commit comments

Comments
 (0)
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