From e2e053e3227a39a8962c12ae694a6ac5bd52bf8f Mon Sep 17 00:00:00 2001 From: Raghu Rajagopalan Date: Fri, 20 Apr 2018 16:55:49 +0530 Subject: [PATCH] feat(cmd): allow reading input from stdin --- cmd/libasciidoc/root_cmd.go | 12 +++++++--- cmd/libasciidoc/root_cmd_test.go | 38 ++++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/cmd/libasciidoc/root_cmd.go b/cmd/libasciidoc/root_cmd.go index c3d84973..effe3a2e 100644 --- a/cmd/libasciidoc/root_cmd.go +++ b/cmd/libasciidoc/root_cmd.go @@ -3,6 +3,7 @@ package main import ( "context" "fmt" + "os" "github.com/bytesparadise/libasciidoc" "github.com/bytesparadise/libasciidoc/renderer" @@ -16,11 +17,16 @@ func NewRootCmd() *cobra.Command { Use: "libasciidoc", Short: "libasciidoc is a tool to generate an html output from an asciidoc file", RunE: func(cmd *cobra.Command, args []string) error { + var err error if cmd.Flag("source").Value.String() == "" { return fmt.Errorf("flag 'source' is required") } - source := cmd.Flag("source").Value.String() - _, err := libasciidoc.ConvertFileToHTML(context.Background(), source, cmd.OutOrStdout(), renderer.IncludeHeaderFooter(true)) //renderer.IncludeHeaderFooter(true) + if cmd.Flag("source").Value.String() == "-" { + _, err = libasciidoc.ConvertToHTML(context.Background(), os.Stdin, cmd.OutOrStdout(), renderer.IncludeHeaderFooter(true)) + } else { + source := cmd.Flag("source").Value.String() + _, err = libasciidoc.ConvertFileToHTML(context.Background(), source, cmd.OutOrStdout(), renderer.IncludeHeaderFooter(true)) //renderer.IncludeHeaderFooter(true) + } if err != nil { return err } @@ -28,6 +34,6 @@ func NewRootCmd() *cobra.Command { }, } flags := rootCmd.Flags() - flags.StringVarP(&source, "source", "s", "", "the path to the asciidoc source to process") + flags.StringVarP(&source, "source", "s", "", "the path to the asciidoc source to process. Use '-' for reading from stdin") return rootCmd } diff --git a/cmd/libasciidoc/root_cmd_test.go b/cmd/libasciidoc/root_cmd_test.go index 9e64f8a5..a8886b51 100644 --- a/cmd/libasciidoc/root_cmd_test.go +++ b/cmd/libasciidoc/root_cmd_test.go @@ -2,15 +2,20 @@ package main_test import ( "bytes" + "io/ioutil" + "log" + "os" "testing" main "github.com/bytesparadise/libasciidoc/cmd/libasciidoc" "github.com/stretchr/testify/require" -) -import . "github.com/onsi/ginkgo" + . "github.com/onsi/ginkgo" + . "github.com/onsi/gomega" +) var _ = Describe("root cmd", func() { + RegisterFailHandler(Fail) It("ok", func() { // given @@ -37,6 +42,35 @@ var _ = Describe("root cmd", func() { require.Error(GinkgoT(), err) }) + It("should process stdin", func() { + // given + root := main.NewRootCmd() + buf := new(bytes.Buffer) + root.SetOutput(buf) + content := "some content" + tmpfile, err := ioutil.TempFile("", "example") + if err != nil { + log.Fatal(err) + } + + defer os.Remove(tmpfile.Name()) // clean up + + if _, err := tmpfile.Write([]byte(content)); err != nil { + log.Fatal(err) + } + tmpfile.Seek(0, 0) + oldstdin := os.Stdin + os.Stdin = tmpfile + defer func() { os.Stdin = oldstdin }() + + root.SetArgs([]string{"-s", "-"}) + // when + err = root.Execute() + GinkgoT().Logf("command output: %v", buf.String()) + Expect(buf.String()).To(ContainSubstring(content)) + require.NoError(GinkgoT(), err) + require.NotEmpty(GinkgoT(), buf) + }) }) func TestRootCommand(t *testing.T) {