From 9c8321f2e0abb569458269dfc06e846445a8eee1 Mon Sep 17 00:00:00 2001 From: Norwin Date: Mon, 1 Mar 2021 01:47:36 +0800 Subject: [PATCH] tea comment: handle piped stdin (#322) fixes #321 Co-authored-by: Norwin Roosen Reviewed-on: https://gitea.com/gitea/tea/pulls/322 Reviewed-by: 6543 <6543@obermui.de> Reviewed-by: Andrew Thornton Co-authored-by: Norwin Co-committed-by: Norwin --- cmd/comment.go | 14 +++++++++++++- modules/interact/comments.go | 9 +++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/cmd/comment.go b/cmd/comment.go index 576840f..169aaa3 100644 --- a/cmd/comment.go +++ b/cmd/comment.go @@ -6,6 +6,7 @@ package cmd import ( "fmt" + "io/ioutil" "strings" "code.gitea.io/tea/modules/interact" @@ -45,12 +46,23 @@ func runAddComment(cmd *cli.Context) error { } body := strings.Join(ctx.Args().Tail(), " ") - if len(body) == 0 { + if interact.IsStdinPiped() { + // custom solution until https://github.com/AlecAivazis/survey/issues/328 is fixed + if bodyStdin, err := ioutil.ReadAll(ctx.App.Reader); err != nil { + return err + } else if len(bodyStdin) != 0 { + body = strings.Join([]string{body, string(bodyStdin)}, "\n\n") + } + } else if len(body) == 0 { if body, err = interact.PromptMultiline("Content"); err != nil { return err } } + if len(body) == 0 { + return fmt.Errorf("No comment body provided") + } + client := ctx.Login.Client() comment, _, err := client.CreateIssueComment(ctx.Owner, ctx.Repo, idx, gitea.CreateIssueCommentOption{ Body: body, diff --git a/modules/interact/comments.go b/modules/interact/comments.go index 6aba46e..aebe4bd 100644 --- a/modules/interact/comments.go +++ b/modules/interact/comments.go @@ -26,7 +26,7 @@ func ShowCommentsMaybeInteractive(ctx *context.TeaContext, idx int64, totalComme return err } print.Comments(comments) - } else if isInteractive() && !ctx.IsSet("comments") { + } else if IsInteractive() && !ctx.IsSet("comments") { // if we're interactive, but --comments hasn't been explicitly set to false if err := ShowCommentsPaginated(ctx, idx, totalComments); err != nil { fmt.Printf("error while loading comments: %v\n", err) @@ -70,6 +70,11 @@ func ShowCommentsPaginated(ctx *context.TeaContext, idx int64, totalComments int } // IsInteractive checks if the output is piped, but NOT if the session is run interactively.. -func isInteractive() bool { +func IsInteractive() bool { return terminal.IsTerminal(int(os.Stdout.Fd())) } + +// IsStdinPiped checks if stdin is piped +func IsStdinPiped() bool { + return !terminal.IsTerminal(int(os.Stdin.Fd())) +}