Skip to content

Commit

Permalink
feat(cmd): allow reading input from stdin
Browse files Browse the repository at this point in the history
  • Loading branch information
raghur committed Apr 20, 2018
1 parent 2f6ae3b commit e2e053e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 5 deletions.
12 changes: 9 additions & 3 deletions cmd/libasciidoc/root_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"os"

"github.com/bytesparadise/libasciidoc"
"github.com/bytesparadise/libasciidoc/renderer"
Expand All @@ -16,18 +17,23 @@ 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
}
return nil
},
}
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
}
38 changes: 36 additions & 2 deletions cmd/libasciidoc/root_cmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit e2e053e

Please sign in to comment.