Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(prints): Add a Secret method to support secure input. #100

Merged
merged 1 commit into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/redis/go-redis/v9 v9.4.0
github.com/robfig/cron/v3 v3.0.1
github.com/stretchr/testify v1.8.4
golang.org/x/term v0.17.0
golang.org/x/text v0.14.0
gorm.io/gorm v1.25.7
)
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.17.0 h1:mkTF7LCd6WGJNL3K1Ad7kwxNfYAW6a8a8QqtMblp/4U=
golang.org/x/term v0.17.0/go.mod h1:lLRBjIVuehSbZlaOtGMbcMncT+aqLLLmKrsjNrUguwk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
4 changes: 4 additions & 0 deletions prints/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ func main() {
r2, _ := prints.Ask("What is your name?")
prints.Info(r2)

// secret
r3, _ := prints.Secret("What is your password?", "123456")
prints.Info("The password is " + r3)

// progress bar
bar := prints.NewProgressBar(100, prints.WithTemplate(prints.Full))
for i := 1; i <= 100; i++ {
Expand Down
77 changes: 63 additions & 14 deletions prints/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"bufio"
"os"
"strings"

"golang.org/x/term"
)

type Prompt struct {
question string

defaultAnswer string
secret bool // TODO: support secret
secret bool
trimSpace bool
}

type PromptOption func(*Prompt)
Expand All @@ -27,9 +30,16 @@
}
}

func WithTrimSpace(flag bool) PromptOption {
return func(p *Prompt) {
p.trimSpace = flag
}

Check warning on line 36 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L33-L36

Added lines #L33 - L36 were not covered by tests
}

func NewPrompt(question string, opts ...PromptOption) *Prompt {
p := &Prompt{
question: question,
question: question,
trimSpace: true,

Check warning on line 42 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L41-L42

Added lines #L41 - L42 were not covered by tests
}

for _, opt := range opts {
Expand All @@ -40,30 +50,59 @@
}

func (p *Prompt) Ask() (string, error) {
if _, err := Infof("%s ", p.question); err != nil {
return "", err
}
if p.defaultAnswer != "" {
if _, err := Warnf("[%s] ", p.defaultAnswer); err != nil {
return "", err
}
}
if _, err := Linef("\n> "); err != nil {
if err := p.output(); err != nil {

Check warning on line 53 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L53

Added line #L53 was not covered by tests
return "", err
}

reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
input, err := p.input()

Check warning on line 57 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L57

Added line #L57 was not covered by tests
if err != nil {
return "", err
}
input = strings.TrimSpace(input)

if p.trimSpace {
input = strings.TrimSpace(input)
}

Check warning on line 64 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L62-L64

Added lines #L62 - L64 were not covered by tests

if input == "" {
return p.defaultAnswer, nil
}
return input, nil
}

func (p *Prompt) output() error {
if _, err := Infof("%s ", p.question); err != nil {
return err
}

Check warning on line 75 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L72-L75

Added lines #L72 - L75 were not covered by tests

if p.defaultAnswer != "" {
if _, err := Warnf("[%s] ", p.defaultAnswer); err != nil {
return err
}

Check warning on line 80 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L77-L80

Added lines #L77 - L80 were not covered by tests
}

if _, err := Linef("\n> "); err != nil {
return err
}

Check warning on line 85 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L83-L85

Added lines #L83 - L85 were not covered by tests

return nil

Check warning on line 87 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L87

Added line #L87 was not covered by tests
}

func (p *Prompt) input() (string, error) {
// support secret
if p.secret {
input, err := term.ReadPassword(int(os.Stdin.Fd()))
if err != nil {
return "", err
}
NewLine() //nolint:errcheck // add a new line
return string(input), nil

Check warning on line 98 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L90-L98

Added lines #L90 - L98 were not covered by tests
}

// normal input
reader := bufio.NewReader(os.Stdin)
return reader.ReadString('\n')

Check warning on line 103 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L102-L103

Added lines #L102 - L103 were not covered by tests
}

func Ask(question string, defaults ...string) (string, error) {
var opts []PromptOption
if len(defaults) > 0 {
Expand All @@ -72,3 +111,13 @@

return NewPrompt(question, opts...).Ask()
}

func Secret(question string, defaults ...string) (string, error) {
var opts []PromptOption
if len(defaults) > 0 {
opts = append(opts, WithDefaultAnswer(defaults[0]))
}
opts = append(opts, WithSecret(), WithTrimSpace(false))

return NewPrompt(question, opts...).Ask()

Check warning on line 122 in prints/prompt.go

View check run for this annotation

Codecov / codecov/patch

prints/prompt.go#L115-L122

Added lines #L115 - L122 were not covered by tests
}
Loading