-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.go
150 lines (125 loc) · 3.34 KB
/
test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package cmd
import (
"errors"
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"github.com/spf13/cobra"
"github.com/rockset/cli/config"
"github.com/rockset/cli/tui"
)
func newTestCmd() *cobra.Command {
cmd := cobra.Command{
Use: "test",
Short: "test",
Hidden: true,
}
cmd.AddCommand(newTestProgressCmd())
cmd.AddCommand(newTestInputCmd())
cmd.AddCommand(newTestSelectorCmd())
return &cmd
}
func newTestInputCmd() *cobra.Command {
cmd := cobra.Command{
Use: "input",
Short: "test input",
Long: "used for testing input fields",
Args: cobra.NoArgs,
Annotations: group("test"),
RunE: func(cmd *cobra.Command, args []string) error {
model := tui.NewInput("Enter context options", []tui.InputConfig{
{Placeholder: "name", Prompt: "Name: "},
{Placeholder: "server", Prompt: "Cluster: ", Validate: func(s string) error {
for _, c := range config.Clusters {
if strings.HasPrefix(c, s) {
return nil
}
}
return fmt.Errorf("cluster must be one of: %s", strings.Join(config.Clusters, ", "))
}},
{Placeholder: "organization", Prompt: "Organization: "},
})
p := tea.NewProgram(model)
if _, err := p.Run(); err != nil {
return err
}
if model.Err != nil {
return model.Err
}
for _, f := range model.Fields {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "%s\n", f)
}
return nil
},
}
return &cmd
}
func newTestProgressCmd() *cobra.Command {
cmd := cobra.Command{
Use: "progress",
Short: "test progress",
Long: "used for testing progress bar",
Args: cobra.NoArgs,
Annotations: group("test"),
RunE: func(cmd *cobra.Command, args []string) error {
fail, _ := cmd.Flags().GetBool("fail")
done, _ := cmd.Flags().GetDuration("done")
estimate, _ := cmd.Flags().GetDuration("estimate")
model := tui.NewTimeProgress(estimate)
p := tea.NewProgram(model)
ctx := cmd.Context()
go func() {
select {
case <-ctx.Done():
if err := ctx.Err(); err != nil {
p.Send(tui.ErrMsg{Err: err})
}
case <-time.After(done):
if fail {
p.Send(tui.ErrMsg{Err: errors.New("boom")})
} else {
p.Send(tui.DoneMsg{})
}
}
}()
if _, err := p.Run(); err != nil {
return err
}
if err := model.Error(); err == nil {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "done\n")
} else {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "err: %v\n", err)
}
return nil
},
}
cmd.Flags().Duration("estimate", 10*time.Second, "estimate how long it takes")
cmd.Flags().Duration("done", 5*time.Second, "done after")
cmd.Flags().Bool("fail", false, "should it fail")
return &cmd
}
func newTestSelectorCmd() *cobra.Command {
cmd := cobra.Command{
Use: "selector",
Short: "test selector",
Long: "used for testing the selector",
Args: cobra.NoArgs,
Annotations: group("test"),
RunE: func(cmd *cobra.Command, args []string) error {
values := []string{"a", "b", "c"}
model := tui.NewSelector(values, 2)
p := tea.NewProgram(model)
if _, err := p.Run(); err != nil {
return err
}
if model.Selected > 0 {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "selection: %s\n", values[model.Selected])
} else {
_, _ = fmt.Fprintf(cmd.OutOrStdout(), "no selection\n")
}
return nil
},
}
return &cmd
}