Skip to content

Commit 9078242

Browse files
chore: use tea.WithContext(inv.Context()) for tea program
1 parent 579c73b commit 9078242

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

cli/cliui/select.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -92,18 +92,25 @@ func Select(inv *serpent.Invocation, opts SelectOptions) (string, error) {
9292

9393
m, err := tea.NewProgram(
9494
initialModel,
95+
tea.WithContext(inv.Context()),
9596
tea.WithInput(inv.Stdin),
9697
tea.WithOutput(inv.Stdout),
9798
).Run()
9899

99-
var value string
100-
if m, ok := m.(selectModel); ok {
101-
if m.canceled {
102-
return value, Canceled
103-
}
104-
value = m.selected
100+
if err != nil {
101+
return "", err
102+
}
103+
104+
model, ok := m.(selectModel)
105+
if !ok {
106+
return "", xerrors.New(fmt.Sprintf("unknown model found %T (%+v)", m, m))
105107
}
106-
return value, err
108+
109+
if model.canceled {
110+
return "", Canceled
111+
}
112+
113+
return model.selected, err
107114
}
108115

109116
type selectModel struct {
@@ -286,19 +293,25 @@ func MultiSelect(inv *serpent.Invocation, opts MultiSelectOptions) ([]string, er
286293

287294
m, err := tea.NewProgram(
288295
initialModel,
296+
tea.WithContext(inv.Context()),
289297
tea.WithInput(inv.Stdin),
290298
tea.WithOutput(inv.Stdout),
291299
).Run()
292300

293-
values := []string{}
294-
if m, ok := m.(multiSelectModel); ok {
295-
if m.canceled {
296-
return values, Canceled
297-
}
301+
if err != nil {
302+
return nil, err
303+
}
298304

299-
values = m.selectedOptions()
305+
model, ok := m.(multiSelectModel)
306+
if !ok {
307+
return nil, xerrors.New(fmt.Sprintf("unknown model found %T (%+v)", m, m))
300308
}
301-
return values, err
309+
310+
if model.canceled {
311+
return nil, Canceled
312+
}
313+
314+
return model.selectedOptions(), err
302315
}
303316

304317
type multiSelectOption struct {

0 commit comments

Comments
 (0)