Skip to content

Commit 2cc902f

Browse files
committed
Use embedded templates instead of serving over API
1 parent fc8c950 commit 2cc902f

31 files changed

+543
-635
lines changed

.vscode/settings.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
"tfstate",
7878
"trimprefix",
7979
"unconvert",
80+
"Untar",
8081
"webrtc",
8182
"xerrors",
8283
"yamux"

cli/cliui/prompt.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func Prompt(cmd *cobra.Command, opts PromptOptions) (string, error) {
6161
case err := <-errCh:
6262
return "", err
6363
case line := <-lineCh:
64-
if opts.IsConfirm && line != "yes" {
64+
if opts.IsConfirm && line != "yes" && line != "y" {
6565
return line, Canceled
6666
}
6767
if opts.Validate != nil {

cli/cliui/prompt_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ func TestPrompt(t *testing.T) {
1818
ptty := ptytest.New(t)
1919
msgChan := make(chan string)
2020
go func() {
21-
resp, err := prompt(ptty, cliui.PromptOptions{
21+
resp, err := newPrompt(ptty, cliui.PromptOptions{
2222
Text: "Example",
2323
})
2424
require.NoError(t, err)
@@ -34,7 +34,7 @@ func TestPrompt(t *testing.T) {
3434
ptty := ptytest.New(t)
3535
doneChan := make(chan string)
3636
go func() {
37-
resp, err := prompt(ptty, cliui.PromptOptions{
37+
resp, err := newPrompt(ptty, cliui.PromptOptions{
3838
Text: "Example",
3939
IsConfirm: true,
4040
})
@@ -47,7 +47,7 @@ func TestPrompt(t *testing.T) {
4747
})
4848
}
4949

50-
func prompt(ptty *ptytest.PTY, opts cliui.PromptOptions) (string, error) {
50+
func newPrompt(ptty *ptytest.PTY, opts cliui.PromptOptions) (string, error) {
5151
value := ""
5252
cmd := &cobra.Command{
5353
RunE: func(cmd *cobra.Command, args []string) error {

cli/cliui/select.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package cliui
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
"strings"
8+
"text/template"
9+
10+
"github.com/manifoldco/promptui"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
type SelectOptions struct {
15+
Options []string
16+
Size int
17+
}
18+
19+
// Select displays a list of user options.
20+
func Select(cmd *cobra.Command, opts SelectOptions) (string, error) {
21+
selector := promptui.Select{
22+
Label: "",
23+
Items: opts.Options,
24+
Size: opts.Size,
25+
Searcher: func(input string, index int) bool {
26+
option := opts.Options[index]
27+
name := strings.Replace(strings.ToLower(option), " ", "", -1)
28+
input = strings.Replace(strings.ToLower(input), " ", "", -1)
29+
30+
return strings.Contains(name, input)
31+
},
32+
Stdin: io.NopCloser(cmd.InOrStdin()),
33+
Stdout: &writeCloser{cmd.OutOrStdout()},
34+
Templates: &promptui.SelectTemplates{
35+
FuncMap: template.FuncMap{
36+
"faint": func(value interface{}) string {
37+
return Styles.Placeholder.Render(value.(string))
38+
},
39+
"selected": func(value interface{}) string {
40+
return defaultStyles.SelectedMenuItem.Render("▶ " + value.(string))
41+
},
42+
},
43+
Active: "{{ . | selected }}",
44+
Inactive: " {{.}}",
45+
Label: "{{.}}",
46+
Selected: "{{ \"\" }}",
47+
Help: fmt.Sprintf(`{{ "Use" | faint }} {{ .SearchKey | faint }} {{ "to toggle search" | faint }}`),
48+
},
49+
HideSelected: true,
50+
}
51+
52+
_, result, err := selector.Run()
53+
if errors.Is(err, promptui.ErrAbort) || errors.Is(err, promptui.ErrInterrupt) {
54+
return result, Canceled
55+
}
56+
if err != nil {
57+
return result, err
58+
}
59+
return result, nil
60+
}
61+
62+
type writeCloser struct {
63+
io.Writer
64+
}
65+
66+
func (w *writeCloser) Close() error {
67+
return nil
68+
}

cli/cliui/select_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package cliui_test
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/manifoldco/promptui"
8+
"github.com/spf13/cobra"
9+
"github.com/stretchr/testify/require"
10+
11+
"github.com/coder/coder/cli/cliui"
12+
"github.com/coder/coder/pty/ptytest"
13+
)
14+
15+
func TestSelect(t *testing.T) {
16+
t.Parallel()
17+
t.Run("Select", func(t *testing.T) {
18+
t.Parallel()
19+
ptty := ptytest.New(t)
20+
msgChan := make(chan string)
21+
go func() {
22+
resp, err := newSelect(ptty, cliui.SelectOptions{
23+
Options: []string{"First", "Second"},
24+
})
25+
require.NoError(t, err)
26+
msgChan <- resp
27+
}()
28+
ptty.ExpectMatch("Second")
29+
ptty.Write(promptui.KeyNext)
30+
ptty.WriteLine("")
31+
require.Equal(t, "Second", <-msgChan)
32+
})
33+
}
34+
35+
func newSelect(ptty *ptytest.PTY, opts cliui.SelectOptions) (string, error) {
36+
value := ""
37+
cmd := &cobra.Command{
38+
RunE: func(cmd *cobra.Command, args []string) error {
39+
var err error
40+
value, err = cliui.Select(cmd, opts)
41+
return err
42+
},
43+
}
44+
cmd.SetOutput(ptty.Output())
45+
cmd.SetIn(ptty.Input())
46+
return value, cmd.ExecuteContext(context.Background())
47+
}

cli/projectclone.go

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)