Skip to content

Commit 59696e7

Browse files
authored
Merge pull request #2078 from afbjorklund/interface
Move all usage of survey into an internal pkg
2 parents 6f767a8 + d1f546c commit 59696e7

File tree

4 files changed

+63
-40
lines changed

4 files changed

+63
-40
lines changed

cmd/limactl/edit.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
"os"
88
"path/filepath"
99

10-
"github.com/AlecAivazis/survey/v2"
1110
"github.com/lima-vm/lima/cmd/limactl/editflags"
1211
"github.com/lima-vm/lima/pkg/editutil"
1312
"github.com/lima-vm/lima/pkg/limayaml"
1413
networks "github.com/lima-vm/lima/pkg/networks/reconcile"
1514
"github.com/lima-vm/lima/pkg/start"
1615
"github.com/lima-vm/lima/pkg/store"
1716
"github.com/lima-vm/lima/pkg/store/filenames"
17+
"github.com/lima-vm/lima/pkg/uiutil"
1818
"github.com/lima-vm/lima/pkg/yqutil"
1919
"github.com/sirupsen/logrus"
2020
"github.com/spf13/cobra"
@@ -126,15 +126,8 @@ func editAction(cmd *cobra.Command, args []string) error {
126126
}
127127

128128
func askWhetherToStart() (bool, error) {
129-
ans := true
130-
prompt := &survey.Confirm{
131-
Message: "Do you want to start the instance now? ",
132-
Default: true,
133-
}
134-
if err := survey.AskOne(prompt, &ans); err != nil {
135-
return false, err
136-
}
137-
return ans, nil
129+
message := "Do you want to start the instance now? "
130+
return uiutil.Confirm(message, true)
138131
}
139132

140133
func editBashComplete(cmd *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {

cmd/limactl/start.go

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"path/filepath"
1111
"strings"
1212

13-
"github.com/AlecAivazis/survey/v2"
14-
"github.com/AlecAivazis/survey/v2/terminal"
1513
"github.com/containerd/containerd/identifiers"
1614
"github.com/lima-vm/lima/cmd/limactl/editflags"
1715
"github.com/lima-vm/lima/cmd/limactl/guessarg"
@@ -24,6 +22,7 @@ import (
2422
"github.com/lima-vm/lima/pkg/store"
2523
"github.com/lima-vm/lima/pkg/store/filenames"
2624
"github.com/lima-vm/lima/pkg/templatestore"
25+
"github.com/lima-vm/lima/pkg/uiutil"
2726
"github.com/lima-vm/lima/pkg/yqutil"
2827
"github.com/sirupsen/logrus"
2928
"github.com/spf13/cobra"
@@ -366,27 +365,25 @@ func chooseNextCreatorState(st *creatorState, yq string) (*creatorState, error)
366365
logrus.WithError(err).Warn("Failed to evaluate yq expression")
367366
return st, err
368367
}
369-
var ans string
370-
prompt := &survey.Select{
371-
Message: fmt.Sprintf("Creating an instance %q", st.instName),
372-
Options: []string{
373-
"Proceed with the current configuration",
374-
"Open an editor to review or modify the current configuration",
375-
"Choose another template (docker, podman, archlinux, fedora, ...)",
376-
"Exit",
377-
},
368+
message := fmt.Sprintf("Creating an instance %q", st.instName)
369+
options := []string{
370+
"Proceed with the current configuration",
371+
"Open an editor to review or modify the current configuration",
372+
"Choose another template (docker, podman, archlinux, fedora, ...)",
373+
"Exit",
378374
}
379-
if err := survey.AskOne(prompt, &ans); err != nil {
380-
if err == terminal.InterruptErr {
375+
ans, err := uiutil.Select(message, options)
376+
if err != nil {
377+
if err == uiutil.InterruptErr {
381378
logrus.Fatal("Interrupted by user")
382379
}
383380
logrus.WithError(err).Warn("Failed to open TUI")
384381
return st, nil
385382
}
386383
switch ans {
387-
case prompt.Options[0]: // "Proceed with the current configuration"
384+
case 0: // "Proceed with the current configuration"
388385
return st, nil
389-
case prompt.Options[1]: // "Open an editor ..."
386+
case 1: // "Open an editor ..."
390387
hdr := fmt.Sprintf("# Review and modify the following configuration for Lima instance %q.\n", st.instName)
391388
if st.instName == DefaultInstanceName {
392389
hdr += "# - In most cases, you do not need to modify this file.\n"
@@ -405,20 +402,18 @@ func chooseNextCreatorState(st *creatorState, yq string) (*creatorState, error)
405402
return st, errors.New("should not reach here")
406403
}
407404
return st, nil
408-
case prompt.Options[2]: // "Choose another template..."
405+
case 2: // "Choose another template..."
409406
templates, err := templatestore.Templates()
410407
if err != nil {
411408
return st, err
412409
}
413-
var ansEx int
414-
promptEx := &survey.Select{
415-
Message: "Choose a template",
416-
Options: make([]string, len(templates)),
417-
}
410+
message := "Choose a template"
411+
options := make([]string, len(templates))
418412
for i := range templates {
419-
promptEx.Options[i] = templates[i].Name
413+
options[i] = templates[i].Name
420414
}
421-
if err := survey.AskOne(promptEx, &ansEx); err != nil {
415+
ansEx, err := uiutil.Select(message, options)
416+
if err != nil {
422417
return st, err
423418
}
424419
if ansEx > len(templates)-1 {
@@ -436,7 +431,7 @@ func chooseNextCreatorState(st *creatorState, yq string) (*creatorState, error)
436431
return nil, err
437432
}
438433
continue
439-
case prompt.Options[3]: // "Exit"
434+
case 3: // "Exit"
440435
os.Exit(0)
441436
return st, errors.New("should not reach here")
442437
default:

pkg/qemu/entitlementutil/entitlementutil.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
"os/exec"
88
"strings"
99

10-
"github.com/AlecAivazis/survey/v2"
10+
"github.com/lima-vm/lima/pkg/uiutil"
11+
1112
"github.com/mattn/go-isatty"
1213
"github.com/sirupsen/logrus"
1314
)
@@ -85,11 +86,10 @@ func AskToSignIfNotSignedProperly(qExe string) {
8586
}
8687
var ans bool
8788
if isatty.IsTerminal(os.Stdout.Fd()) || isatty.IsCygwinTerminal(os.Stdout.Fd()) {
88-
prompt := &survey.Confirm{
89-
Message: fmt.Sprintf("Try to sign %q with the \"com.apple.security.hypervisor\" entitlement?", qExe),
90-
Default: true,
91-
}
92-
if askErr := survey.AskOne(prompt, &ans); askErr != nil {
89+
message := fmt.Sprintf("Try to sign %q with the \"com.apple.security.hypervisor\" entitlement?", qExe)
90+
var askErr error
91+
ans, askErr = uiutil.Confirm(message, true)
92+
if askErr != nil {
9393
logrus.WithError(askErr).Warn("No answer was given")
9494
}
9595
}

pkg/uiutil/uiutil.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package uiutil
2+
3+
import (
4+
"github.com/AlecAivazis/survey/v2"
5+
"github.com/AlecAivazis/survey/v2/terminal"
6+
)
7+
8+
var InterruptErr = terminal.InterruptErr
9+
10+
// Confirm is a regular text input that accept yes/no answers.
11+
func Confirm(message string, defaultParam bool) (bool, error) {
12+
var ans bool
13+
prompt := &survey.Confirm{
14+
Message: message,
15+
Default: defaultParam,
16+
}
17+
if err := survey.AskOne(prompt, &ans); err != nil {
18+
return false, err
19+
}
20+
return ans, nil
21+
}
22+
23+
// Select is a prompt that presents a list of various options
24+
// to the user for them to select using the arrow keys and enter.
25+
func Select(message string, options []string) (int, error) {
26+
var ans int
27+
prompt := &survey.Select{
28+
Message: message,
29+
Options: options,
30+
}
31+
if err := survey.AskOne(prompt, &ans); err != nil {
32+
return -1, err
33+
}
34+
return ans, nil
35+
}

0 commit comments

Comments
 (0)