feat(cli): add --id parameter to templates init command (#7116)

This PR makes the following changes:

- Adds an --id parameter to coder templates init so that you can non-interactively initialize a specific example template by ID (e.g. folder name)
- Updates develop.sh and lima/coder.yaml to use this parameter to select the docker example template.
This commit is contained in:
Cian Johnston
2023-04-13 15:02:49 +01:00
committed by GitHub
parent 17f692a89a
commit 87fe16cde9
6 changed files with 113 additions and 30 deletions
+72 -25
View File
@@ -2,9 +2,15 @@ package cli
import (
"bytes"
"errors"
"fmt"
"io"
"os"
"path/filepath"
"sort"
"golang.org/x/exp/maps"
"golang.org/x/xerrors"
"github.com/coder/coder/cli/clibase"
"github.com/coder/coder/cli/cliui"
@@ -14,38 +20,60 @@ import (
)
func (*RootCmd) templateInit() *clibase.Cmd {
return &clibase.Cmd{
var templateID string
exampleList, err := examples.List()
if err != nil {
// This should not happen. If it does, something is very wrong.
panic(err)
}
var templateIDs []string
for _, ex := range exampleList {
templateIDs = append(templateIDs, ex.ID)
}
sort.Strings(templateIDs)
cmd := &clibase.Cmd{
Use: "init [directory]",
Short: "Get started with a templated template.",
Middleware: clibase.RequireRangeArgs(0, 1),
Handler: func(inv *clibase.Invocation) error {
exampleList, err := examples.List()
if err != nil {
return err
}
exampleNames := []string{}
exampleByName := map[string]codersdk.TemplateExample{}
for _, example := range exampleList {
name := fmt.Sprintf(
"%s\n%s\n%s\n",
cliui.Styles.Bold.Render(example.Name),
cliui.Styles.Wrap.Copy().PaddingLeft(6).Render(example.Description),
cliui.Styles.Keyword.Copy().PaddingLeft(6).Render(example.URL),
)
exampleNames = append(exampleNames, name)
exampleByName[name] = example
// If the user didn't specify any template, prompt them to select one.
if templateID == "" {
optsToID := map[string]string{}
for _, example := range exampleList {
name := fmt.Sprintf(
"%s\n%s\n%s\n",
cliui.Styles.Bold.Render(example.Name),
cliui.Styles.Wrap.Copy().PaddingLeft(6).Render(example.Description),
cliui.Styles.Keyword.Copy().PaddingLeft(6).Render(example.URL),
)
optsToID[name] = example.ID
}
opts := maps.Keys(optsToID)
sort.Strings(opts)
_, _ = fmt.Fprintln(inv.Stdout, cliui.Styles.Wrap.Render(
"A template defines infrastructure as code to be provisioned "+
"for individual developer workspaces. Select an example to be copied to the active directory:\n"))
selected, err := cliui.Select(inv, cliui.SelectOptions{
Options: opts,
})
if err != nil {
if errors.Is(err, io.EOF) {
return xerrors.Errorf(
"Couldn't find a matching template!\n" +
"Tip: if you're trying to automate template creation, try\n" +
"coder templates init --id <template_id> instead!",
)
}
return err
}
templateID = optsToID[selected]
}
_, _ = fmt.Fprintln(inv.Stdout, cliui.Styles.Wrap.Render(
"A template defines infrastructure as code to be provisioned "+
"for individual developer workspaces. Select an example to be copied to the active directory:\n"))
option, err := cliui.Select(inv, cliui.SelectOptions{
Options: exampleNames,
})
if err != nil {
return err
selectedTemplate, ok := templateByID(templateID, exampleList)
if !ok {
// clibase.EnumOf would normally handle this.
return xerrors.Errorf("template not found: %q", templateID)
}
selectedTemplate := exampleByName[option]
archive, err := examples.Archive(selectedTemplate.ID)
if err != nil {
return err
@@ -81,4 +109,23 @@ func (*RootCmd) templateInit() *clibase.Cmd {
return nil
},
}
cmd.Options = clibase.OptionSet{
{
Flag: "id",
Description: "Specify a given example template by ID.",
Value: clibase.EnumOf(&templateID, templateIDs...),
},
}
return cmd
}
func templateByID(templateID string, tes []codersdk.TemplateExample) (codersdk.TemplateExample, bool) {
for _, te := range tes {
if te.ID == templateID {
return te, true
}
}
return codersdk.TemplateExample{}, false
}
+23
View File
@@ -22,4 +22,27 @@ func TestTemplateInit(t *testing.T) {
require.NoError(t, err)
require.Greater(t, len(files), 0)
})
t.Run("ExtractSpecific", func(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
inv, _ := clitest.New(t, "templates", "init", "--id", "docker", tempDir)
ptytest.New(t).Attach(inv)
clitest.Run(t, inv)
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Greater(t, len(files), 0)
})
t.Run("NotFound", func(t *testing.T) {
t.Parallel()
tempDir := t.TempDir()
inv, _ := clitest.New(t, "templates", "init", "--id", "thistemplatedoesnotexist", tempDir)
ptytest.New(t).Attach(inv)
err := inv.Run()
require.ErrorContains(t, err, "invalid choice: thistemplatedoesnotexist, should be one of")
files, err := os.ReadDir(tempDir)
require.NoError(t, err)
require.Empty(t, files)
})
}
+5 -1
View File
@@ -1,6 +1,10 @@
Usage: coder templates init [directory]
Usage: coder templates init [flags] [directory]
Get started with a templated template.
Options
--id aws-ecs-container|aws-linux|aws-windows|azure-linux|do-linux|docker|docker-with-dotfiles|fly-docker-image|gcp-linux|gcp-vm-container|gcp-windows|kubernetes
Specify a given example template by ID.
---
Run `coder --help` for a list of global options.