From def6085e9308c8e3f60af7e4c9c363ece4391bbe Mon Sep 17 00:00:00 2001 From: Yoriki Yamaguchi Date: Tue, 21 Jun 2016 18:52:04 +0900 Subject: [PATCH 1/4] input from stdin --- .travis.yml | 3 ++- README.md | 13 +++++++++++++ lib/get_input_bytes.go | 16 ++++++++++++++++ main.go | 27 +++++++++++++++++---------- 4 files changed, 48 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index df04194..f79139c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,9 +4,10 @@ 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 +before_deploy: - go get -v github.com/mitchellh/gox - gox -output build/{{.OS}}_{{.Arch}}/{{.Dir}} - cd build diff --git a/README.md b/README.md index 6b612f5..94b1fd2 100644 --- a/README.md +++ b/README.md @@ -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 +``` diff --git a/lib/get_input_bytes.go b/lib/get_input_bytes.go index f95a400..7410110 100644 --- a/lib/get_input_bytes.go +++ b/lib/get_input_bytes.go @@ -4,6 +4,8 @@ import ( "fmt" "io/ioutil" "os" + "bufio" + "strings" ) func GetInputBytes(source, str string) []byte { @@ -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) diff --git a/main.go b/main.go index ee9beb3..e85310a 100644 --- a/main.go +++ b/main.go @@ -2,8 +2,9 @@ package main import ( "fmt" - "github.com/codegangsta/cli" + "github.com/urfave/cli" "os" + "golang.org/x/crypto/ssh/terminal" "github.com/y13i/j2y/lib" ) @@ -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()) @@ -61,6 +66,8 @@ func main() { } j2yLib.Output(outputBytes, command.String("output")) + + return nil } app.Run(os.Args) From c5ed73ef095b54f8ddf833b852fdf391776f8979 Mon Sep 17 00:00:00 2001 From: Yoriki Yamaguchi Date: Tue, 21 Jun 2016 18:53:28 +0900 Subject: [PATCH 2/4] gofmt --- lib/get_input_bytes.go | 14 +++++++------- main.go | 8 ++++---- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/lib/get_input_bytes.go b/lib/get_input_bytes.go index 7410110..0748655 100644 --- a/lib/get_input_bytes.go +++ b/lib/get_input_bytes.go @@ -1,10 +1,10 @@ package j2yLib import ( + "bufio" "fmt" "io/ioutil" "os" - "bufio" "strings" ) @@ -24,17 +24,17 @@ func GetInputBytes(source, str string) []byte { os.Exit(1) } case "STDIN": - scanner := bufio.NewScanner(os.Stdin) + scanner := bufio.NewScanner(os.Stdin) var inputLines []string for scanner.Scan() { - inputLines = append(inputLines, scanner.Text()) - } + inputLines = append(inputLines, scanner.Text()) + } - if err := scanner.Err(); err != nil { - fmt.Fprintln(os.Stderr, "reading standard input:", err) - } + if err := scanner.Err(); err != nil { + fmt.Fprintln(os.Stderr, "reading standard input:", err) + } inputBytes = []byte(strings.Join(inputLines, "")) default: diff --git a/main.go b/main.go index e85310a..5ef842a 100644 --- a/main.go +++ b/main.go @@ -3,16 +3,16 @@ package main import ( "fmt" "github.com/urfave/cli" - "os" - "golang.org/x/crypto/ssh/terminal" "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.Name = "j2y" + app.Usage = "convert JSON to YAML" app.Version = "0.0.7" app.Flags = []cli.Flag{ From 7bb7b708bed6f88e2d972777c0157f1c21d72cda Mon Sep 17 00:00:00 2001 From: Yoriki Yamaguchi Date: Tue, 21 Jun 2016 18:54:13 +0900 Subject: [PATCH 3/4] bump up version --- main.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index 5ef842a..32ac899 100644 --- a/main.go +++ b/main.go @@ -11,9 +11,9 @@ import ( func main() { app := cli.NewApp() - app.Name = "j2y" - app.Usage = "convert JSON to YAML" - app.Version = "0.0.7" + app.Name = "j2y" + app.Usage = "convert JSON to YAML" + app.Version = "0.0.8" app.Flags = []cli.Flag{ cli.StringFlag{ From 1929e88ad48ec77adb51d964d9e74088a82de4a6 Mon Sep 17 00:00:00 2001 From: Yoriki Yamaguchi Date: Tue, 21 Jun 2016 18:58:57 +0900 Subject: [PATCH 4/4] add golang.org/x/crypto/ssh/terminal --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index f79139c..4d6540b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,7 @@ install: - 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}}