diff --git a/README.md b/README.md index 19dc5bfe..4d53764a 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,12 @@ yaml sample.yaml b.c ``` will output the value of '2'. +### Reading from STDIN +Given a sample.yaml file of: +cat sample.yaml | yaml - b.c +``` +will output the value of '2'. + ### Handling '.' in the yaml key Given a sample.yaml file of: ```yaml diff --git a/yaml.go b/yaml.go index db1781fd..7c21d8e5 100644 --- a/yaml.go +++ b/yaml.go @@ -115,7 +115,14 @@ func readYaml(c *cli.Context, parsedData *map[interface{}]interface{}) { if len(c.Args()) == 0 { log.Fatalf("Must provide filename") } - var rawData = readFile(c.Args()[0]) + + var rawData []byte + fmt.Println("c.Args()[0]", c.Args()[0]) + if( c.Args()[0] == "-") { + rawData = readStdin() + } else { + rawData = readFile(c.Args()[0]) + } err := yaml.Unmarshal([]byte(rawData), &parsedData) if err != nil { @@ -123,6 +130,14 @@ func readYaml(c *cli.Context, parsedData *map[interface{}]interface{}) { } } +func readStdin() []byte { + bytes, err := ioutil.ReadAll(os.Stdin) + if err != nil { + log.Fatalf("error reading stdin", err) + } + return bytes +} + func readFile(filename string) []byte { var rawData, readError = ioutil.ReadFile(filename) if readError != nil {