Skip to content
This repository has been archived by the owner on Jan 29, 2021. It is now read-only.

Commit

Permalink
Merge pull request #6 from y13i/stdin_input
Browse files Browse the repository at this point in the history
Stdin input
  • Loading branch information
Yoriki Yamaguchi authored Jun 21, 2016
2 parents 45a5e73 + 1929e88 commit 2a16e89
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ go:
env:
- PATH=/home/travis/gopath/bin:$PATH
install:
- go get github.com/codegangsta/cli
- go get github.com/urfave/cli
- go get github.com/ghodss/yaml
- go get github.com/bitly/go-simplejson
- go get golang.org/x/crypto/ssh/terminal
before_deploy:
- go get -v github.com/mitchellh/gox
- gox -output build/{{.OS}}_{{.Arch}}/{{.Dir}}
- cd build
Expand Down
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,16 @@ employees:
- firstName: Peter
lastName: Jones
```

### Input from STDIN

```
$ echo '{"employees":[{"firstName":"John","lastName":"Doe"},{"firstName":"Anna","lastName":"Smith"},{"firstName":"Peter","lastName":"Jones"}]}' | j2y
employees:
- firstName: John
lastName: Doe
- firstName: Anna
lastName: Smith
- firstName: Peter
lastName: Jones
```
16 changes: 16 additions & 0 deletions lib/get_input_bytes.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package j2yLib

import (
"bufio"
"fmt"
"io/ioutil"
"os"
"strings"
)

func GetInputBytes(source, str string) []byte {
Expand All @@ -21,6 +23,20 @@ func GetInputBytes(source, str string) []byte {
fmt.Printf("err: %v\n", err)
os.Exit(1)
}
case "STDIN":
scanner := bufio.NewScanner(os.Stdin)

var inputLines []string

for scanner.Scan() {
inputLines = append(inputLines, scanner.Text())
}

if err := scanner.Err(); err != nil {
fmt.Fprintln(os.Stderr, "reading standard input:", err)
}

inputBytes = []byte(strings.Join(inputLines, ""))
default:
fmt.Println("unknown source.")
os.Exit(1)
Expand Down
31 changes: 19 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@ package main

import (
"fmt"
"github.com/codegangsta/cli"
"os"
"github.com/urfave/cli"
"github.com/y13i/j2y/lib"
"golang.org/x/crypto/ssh/terminal"
"os"
)

func main() {
app := cli.NewApp()

app.Name = "j2y"
app.Usage = "convert JSON to YAML"
app.Version = "0.0.7"
app.Version = "0.0.8"

app.Flags = []cli.Flag{
cli.StringFlag{
Expand All @@ -37,19 +38,23 @@ func main() {
},
}

app.Action = func(command *cli.Context) {
app.Action = func(command *cli.Context) error {
var source string
var outputBytes []byte

if command.Bool("eval") {
source = "ARGV"
} else {
if command.Args().First() == "" {
fmt.Println("`j2y --help` to view usage.")
os.Exit(1)
}
if terminal.IsTerminal(0) {
if command.Bool("eval") {
source = "ARGV"
} else {
if command.Args().First() == "" {
fmt.Println("`j2y --help` to view usage.")
os.Exit(1)
}

source = "FILE"
source = "FILE"
}
} else {
source = "STDIN"
}

inputBytes := j2yLib.GetInputBytes(source, command.Args().First())
Expand All @@ -61,6 +66,8 @@ func main() {
}

j2yLib.Output(outputBytes, command.String("output"))

return nil
}

app.Run(os.Args)
Expand Down

0 comments on commit 2a16e89

Please sign in to comment.