forked from plouc/go-gitlab-client
/
prompt.go
76 lines (61 loc) · 1.38 KB
/
prompt.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
package cmd
import (
"fmt"
"strings"
"github.com/fatih/color"
"github.com/manifoldco/promptui"
"github.com/plouc/go-gitlab-client/gitlab"
)
func confirmAction(question, abortMessage string, autoConfirm bool) bool {
if !isInteractive || autoConfirm {
return true
}
prompt := promptui.Select{
Label: color.YellowString(question),
Items: []string{"yes", "no"},
}
_, answer, err := prompt.Run()
if err != nil {
fmt.Println(err.Error())
return false
}
if answer == "yes" {
return true
}
color.Red(" %s", abortMessage)
return false
}
func handlePaginatedResult(meta *gitlab.ResponseMeta, fetch func()) {
if !isInteractive {
return
}
if meta.PrevPage != 0 || meta.NextPage != 0 {
actions := []string{"quit"}
if meta.PrevPage != 0 {
actions = append(actions, fmt.Sprintf("get previous page (%d)", meta.PrevPage))
}
if meta.NextPage != 0 {
actions = append(actions, fmt.Sprintf("get next page (%d)", meta.NextPage))
}
prompt := promptui.Select{
Label: color.YellowString("There's more results available, what do you want to do?"),
Items: actions,
}
_, action, err := prompt.Run()
if err != nil {
fmt.Println(err.Error())
return
}
if action == "quit" {
return
}
if strings.Contains(action, "previous") {
page = meta.PrevPage
fetch()
}
if strings.Contains(action, "next") {
page = meta.NextPage
fetch()
}
}
}