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 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 From de3df3f74dd376ee088770add56ae249f17fcd29 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Sat, 8 Feb 2020 19:10:03 +0000 Subject: allow whitespaces in regexes --- internal/server/handlers/readcommand.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/server/handlers/readcommand.go b/internal/server/handlers/readcommand.go index e4079e8..67f9572 100644 --- a/internal/server/handlers/readcommand.go +++ b/internal/server/handlers/readcommand.go @@ -27,7 +27,7 @@ func newReadCommand(server *ServerHandler, mode omode.Mode) *readCommand { func (r *readCommand) Start(ctx context.Context, argc int, args []string) { regex := "." if argc >= 4 { - regex = args[3] + regex = strings.Join(args[3:], " ") } if argc < 3 { r.server.sendServerMessage(logger.Warn(r.server.user, commandParseWarning, args, argc)) -- cgit v1.2.3 From 7f0f6e09948e188a6c3221d9eb77290d25a251e2 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 11 Feb 2020 07:02:26 +0000 Subject: more debug output --- internal/server/handlers/readcommand.go | 1 + internal/server/handlers/serverhandler.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/server/handlers/readcommand.go b/internal/server/handlers/readcommand.go index 67f9572..6ab41cd 100644 --- a/internal/server/handlers/readcommand.go +++ b/internal/server/handlers/readcommand.go @@ -28,6 +28,7 @@ func (r *readCommand) Start(ctx context.Context, argc int, args []string) { regex := "." if argc >= 4 { regex = strings.Join(args[3:], " ") + logger.Debug("Joined regex", regex) } if argc < 3 { r.server.sendServerMessage(logger.Warn(r.server.user, commandParseWarning, args, argc)) diff --git a/internal/server/handlers/serverhandler.go b/internal/server/handlers/serverhandler.go index 3f0d6ce..b840c77 100644 --- a/internal/server/handlers/serverhandler.go +++ b/internal/server/handlers/serverhandler.go @@ -172,7 +172,7 @@ func (h *ServerHandler) handleProtocolVersion(args []string) ([]string, int, err } func (h *ServerHandler) handleBase64(args []string, argc int) ([]string, int, error) { - err := errors.New("Unable to decode client message") + err := errors.New("Unable to decode client message, DTail server and client versions not compatible") if argc != 2 || args[0] != "base64" { return args, argc, err -- cgit v1.2.3 From b0fe5dc0ebf7a25dbd8361061865ede11d582861 Mon Sep 17 00:00:00 2001 From: Paul Buetow Date: Tue, 11 Feb 2020 07:03:33 +0000 Subject: remove develop string --- internal/version/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/version/version.go b/internal/version/version.go index 3fd729b..e3014a4 100644 --- a/internal/version/version.go +++ b/internal/version/version.go @@ -13,7 +13,7 @@ const ( // Version of DTail. Version string = "2.0.3" // Additional information for DTail - Additional string = "develop" + Additional string = "" // ProtocolCompat -ibility version. ProtocolCompat string = "2" ) -- cgit v1.2.3