-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.go
111 lines (87 loc) · 2.46 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
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
package credentials
import (
"context"
"errors"
"github.com/AlecAivazis/survey/v2"
"github.com/AlecAivazis/survey/v2/terminal"
"github.com/bool64/ctxd"
"github.com/nhatthm/moneyloverapi"
"go.nhat.io/surveyexpect/options"
"go.nhat.io/surveyexpect/options/cobra"
)
var _ moneyloverapi.CredentialsProvider = (*Prompt)(nil)
// Option configures Prompt.
type Option func(p *Prompt)
// Prompt provides the credentials using cli prompt.
type Prompt struct {
options []survey.AskOpt
logger ctxd.Logger
username string
password string
}
func (p *Prompt) prompt(message string, response interface{}) error {
return survey.AskOne(&survey.Password{Message: message}, response, p.options...)
}
// Username provides a username via cli prompt.
func (p *Prompt) Username() string {
if p.username != "" {
return p.username
}
message := "Enter email (input is hidden) >"
if err := p.prompt(message, &p.username); err != nil {
if !errors.Is(err, terminal.InterruptErr) {
p.logger.Error(context.Background(), "could not read email", "error", err)
}
return ""
}
return p.username
}
// Password provides a password via cli prompt.
func (p *Prompt) Password() string {
if p.password != "" {
return p.password
}
message := "Enter password (input is hidden) >"
if err := p.prompt(message, &p.password); err != nil {
if !errors.Is(err, terminal.InterruptErr) {
p.logger.Error(context.Background(), "could not read password", "error", err)
}
return ""
}
return p.password
}
// New initiates a new CredentialsProvider.
func New(options ...Option) *Prompt {
p := &Prompt{
options: []survey.AskOpt{
survey.WithValidator(survey.Required),
},
logger: ctxd.NoOpLogger{},
}
for _, o := range options {
o(p)
}
return p
}
// WithStdio sets stdio for the prompts.
func WithStdio(stdio terminal.Stdio) Option {
return func(p *Prompt) {
p.options = append(p.options, options.WithStdio(stdio))
}
}
// WithStdioProvider sets stdio for the prompts.
func WithStdioProvider(stdio cobra.StdioProvider) Option {
return func(p *Prompt) {
p.options = append(p.options, cobra.WithStdioProvider(stdio))
}
}
// WithLogger sets logger for Prompt.
func WithLogger(logger ctxd.Logger) Option {
return func(p *Prompt) {
p.logger = logger
}
}
// WithCredentialsAtLast sets CredentialsProvider as a moneyloverapi.CredentialsProvider.
func WithCredentialsAtLast(options ...Option) moneyloverapi.Option {
return moneyloverapi.WithCredentialsProviderAtLast(New(options...))
}