From 75c530dcd9fbe3376f3a3c617663af0303a935e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Paul=20B=C3=BCtow?= Date: Sat, 8 Feb 2020 18:59:05 +0000 Subject: move prompt to io/ --- internal/io/prompt/prompt.go | 95 ++++++++++++++++++++++++++++++++++ internal/prompt/prompt.go | 95 ---------------------------------- internal/ssh/client/hostkeycallback.go | 2 +- internal/version/version.go | 4 +- 4 files changed, 98 insertions(+), 98 deletions(-) create mode 100644 internal/io/prompt/prompt.go delete mode 100644 internal/prompt/prompt.go (limited to 'internal') diff --git a/internal/io/prompt/prompt.go b/internal/io/prompt/prompt.go new file mode 100644 index 0000000..a438d33 --- /dev/null +++ b/internal/io/prompt/prompt.go @@ -0,0 +1,95 @@ +package prompt + +import ( + "bufio" + "fmt" + "github.com/mimecast/dtail/internal/io/logger" + "os" + "strings" +) + +// Answer is a user input of a prompt question. +type Answer struct { + // Long version of the expected user input + Long string + // Short version of the expected user input + Short string + // Runs when user input matches + Callback func() + // Runs after Callback and after logging resumes + EndCallback func() + + AskAgain bool +} + +// Prompt used for interactive user input. +type Prompt struct { + question string + answers []Answer +} + +func (p *Prompt) askString() string { + var sb strings.Builder + + sb.WriteString(p.question) + sb.WriteString("? (") + + var ax []string + for _, a := range p.answers { + ax = append(ax, fmt.Sprintf("%s=%s", a.Short, a.Long)) + } + + sb.WriteString(strings.Join(ax, ",")) + sb.WriteString("): ") + + return sb.String() +} + +// New returns a new prompt. +func New(question string) *Prompt { + return &Prompt{question: question} +} + +// Add an answer. +func (p *Prompt) Add(answer Answer) { + p.answers = append(p.answers, answer) +} + +// Ask a question. +func (p *Prompt) Ask() { + reader := bufio.NewReader(os.Stdin) + logger.Pause() + + for { + fmt.Print(p.askString()) + answerStr, _ := reader.ReadString('\n') + + if a, ok := p.answer(strings.TrimSpace(answerStr)); ok { + if a.Callback != nil { + a.Callback() + } + + if !a.AskAgain { + logger.Resume() + if a.EndCallback != nil { + a.EndCallback() + } + return + } + } + } +} + +func (p *Prompt) answer(answerStr string) (*Answer, bool) { + for _, a := range p.answers { + switch answerStr { + case a.Long: + return &a, true + case a.Short: + return &a, true + default: + } + } + + return nil, false +} diff --git a/internal/prompt/prompt.go b/internal/prompt/prompt.go deleted file mode 100644 index a438d33..0000000 --- a/internal/prompt/prompt.go +++ /dev/null @@ -1,95 +0,0 @@ -package prompt - -import ( - "bufio" - "fmt" - "github.com/mimecast/dtail/internal/io/logger" - "os" - "strings" -) - -// Answer is a user input of a prompt question. -type Answer struct { - // Long version of the expected user input - Long string - // Short version of the expected user input - Short string - // Runs when user input matches - Callback func() - // Runs after Callback and after logging resumes - EndCallback func() - - AskAgain bool -} - -// Prompt used for interactive user input. -type Prompt struct { - question string - answers []Answer -} - -func (p *Prompt) askString() string { - var sb strings.Builder - - sb.WriteString(p.question) - sb.WriteString("? (") - - var ax []string - for _, a := range p.answers { - ax = append(ax, fmt.Sprintf("%s=%s", a.Short, a.Long)) - } - - sb.WriteString(strings.Join(ax, ",")) - sb.WriteString("): ") - - return sb.String() -} - -// New returns a new prompt. -func New(question string) *Prompt { - return &Prompt{question: question} -} - -// Add an answer. -func (p *Prompt) Add(answer Answer) { - p.answers = append(p.answers, answer) -} - -// Ask a question. -func (p *Prompt) Ask() { - reader := bufio.NewReader(os.Stdin) - logger.Pause() - - for { - fmt.Print(p.askString()) - answerStr, _ := reader.ReadString('\n') - - if a, ok := p.answer(strings.TrimSpace(answerStr)); ok { - if a.Callback != nil { - a.Callback() - } - - if !a.AskAgain { - logger.Resume() - if a.EndCallback != nil { - a.EndCallback() - } - return - } - } - } -} - -func (p *Prompt) answer(answerStr string) (*Answer, bool) { - for _, a := range p.answers { - switch answerStr { - case a.Long: - return &a, true - case a.Short: - return &a, true - default: - } - } - - return nil, false -} diff --git a/internal/ssh/client/hostkeycallback.go b/internal/ssh/client/hostkeycallback.go index 7ae2396..d090d7f 100644 --- a/internal/ssh/client/hostkeycallback.go +++ b/internal/ssh/client/hostkeycallback.go @@ -11,7 +11,7 @@ import ( "time" "github.com/mimecast/dtail/internal/io/logger" - "github.com/mimecast/dtail/internal/prompt" + "github.com/mimecast/dtail/internal/io/prompt" "golang.org/x/crypto/ssh" "golang.org/x/crypto/ssh/knownhosts" diff --git a/internal/version/version.go b/internal/version/version.go index 3c057df..3fd729b 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -11,9 +11,9 @@ const ( // Name of DTail. Name string = "DTail" // Version of DTail. - Version string = "2.0.0" + Version string = "2.0.3" // Additional information for DTail - Additional string = "" + Additional string = "develop" // ProtocolCompat -ibility version. ProtocolCompat string = "2" ) -- cgit v1.2.3