-
Notifications
You must be signed in to change notification settings - Fork 232
/
ui.go
113 lines (90 loc) · 3.24 KB
/
ui.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
package ui
import (
"fmt"
"strings"
"github.com/manifoldco/promptui"
)
// UserInterface is created to allow us to pass a mock user interface for testing.
// Since we can't properly run integration tests on code that calls PromptUI.
// This is because the first call to PromptUI reads from stdin correctly,
// but subsequent calls return EOF.
type UserInterface interface {
ReadSecretStringFromUser(message string) (string, error)
ReadStringFromUser(message string, defaultValue string) string
AskUserToConfirm(message string) bool
}
// InteractiveUI implements the UserInterface used by the real program, not in tests.
type InteractiveUI struct {
}
// ReadSecretStringFromUser can be used to read a value from the user by masking their input.
// It's useful for token input in our case.
func (InteractiveUI) ReadSecretStringFromUser(message string) (string, error) {
prompt := promptui.Prompt{
Label: message,
Mask: '*',
}
token, err := prompt.Run()
if err != nil {
return "", err
}
return token, nil
}
// ReadStringFromUser can be used to read any value from the user or the defaultValue when provided.
func (InteractiveUI) ReadStringFromUser(message string, defaultValue string) string {
prompt := promptui.Prompt{
Label: message,
}
if defaultValue != "" {
prompt.Default = defaultValue
}
token, err := prompt.Run()
if err != nil {
panic(err)
}
return token
}
// AskUserToConfirm will prompt the user to confirm with the provided message.
func (InteractiveUI) AskUserToConfirm(message string) bool {
prompt := promptui.Prompt{
Label: message,
IsConfirm: true,
}
result, err := prompt.Run()
return err == nil && strings.ToLower(result) == "y"
}
// TestingUI implements the UserInterface for our testing purposes.
type TestingUI struct {
Input string
Confirm bool
}
// ReadSecretStringFromUser implements the TestingUI interface for asking a user's input.
// It works by simply printing the message to standard output and passing the input through.
func (ui TestingUI) ReadSecretStringFromUser(message string) (string, error) {
fmt.Println(message)
return ui.Input, nil
}
// ReadStringFromUser implements the TestingUI interface for asking a user's input.
// It works by simply printing the message to standard output and passing the input through.
func (ui TestingUI) ReadStringFromUser(message string, defaultValue string) string {
fmt.Println(message)
return ui.Input
}
// AskUserToConfirm works by printing the provided message to standard out and returning a Confirm dialogue up the chain.
func (ui TestingUI) AskUserToConfirm(message string) bool {
fmt.Println(message)
return ui.Confirm
}
// ShouldAskForToken wraps an AskUserToConfirm dialogue only if their token is empty or blank.
func ShouldAskForToken(token string, ui UserInterface) bool {
if token == "" {
return true
}
return ui.AskUserToConfirm("A CircleCI token is already set. Do you want to change it")
}
// ShouldAskForEndpoint wraps an AskUserToConfirm dialogue only if their endpoint has changed from the default value.
func ShouldAskForEndpoint(endpoint string, ui UserInterface, defaultValue string) bool {
if endpoint == defaultValue {
return true
}
return ui.AskUserToConfirm(fmt.Sprintf("Do you want to reset the endpoint? (default: %s)", defaultValue))
}