Skip to content

Commit

Permalink
feat(prints): Add a Secret method to support secure input. (#100)
Browse files Browse the repository at this point in the history
Signed-off-by: Flc゛ <[email protected]>
  • Loading branch information
flc1125 authored Feb 16, 2024
1 parent 6b26e35 commit 7bcc6f9
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 14 deletions.
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 @@ import (
"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 WithSecret() PromptOption {
}
}

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

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

for _, opt := range opts {
Expand All @@ -40,30 +50,59 @@ func NewPrompt(question string, opts ...PromptOption) *Prompt {
}

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 {
return "", err
}

reader := bufio.NewReader(os.Stdin)
input, err := reader.ReadString('\n')
input, err := p.input()
if err != nil {
return "", err
}
input = strings.TrimSpace(input)

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

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

func (p *Prompt) output() 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 {
return err
}

return nil
}

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
}

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

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

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()
}

0 comments on commit 7bcc6f9

Please sign in to comment.