Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cmd): allow reading input from stdin #86

Merged
merged 1 commit into from
Apr 22, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -19,11 +20,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
}
Expand All @@ -41,7 +47,7 @@ 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")
rootCmd.PersistentFlags().StringVar(&logLevel, "log", "warning", "log level to set {debug, info, warning, error, fatal, panic}")
return rootCmd
}
41 changes: 39 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 Down Expand Up @@ -49,6 +54,38 @@ var _ = Describe("root cmd", func() {
GinkgoT().Logf("command output: %v", buf.String())
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()

//then
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