Can read from STDIN

This commit is contained in:
mfarah 2015-10-05 15:48:34 +11:00
parent 5ee6a9b9ca
commit c03c4813d4
2 changed files with 22 additions and 1 deletions

View File

@ -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

17
yaml.go
View File

@ -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 {