mirror of
https://github.com/mikefarah/yq.git
synced 2024-11-14 07:08:06 +00:00
Can read from STDIN
This commit is contained in:
parent
5ee6a9b9ca
commit
c03c4813d4
@ -27,6 +27,12 @@ yaml sample.yaml b.c
|
|||||||
```
|
```
|
||||||
will output the value of '2'.
|
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
|
### Handling '.' in the yaml key
|
||||||
Given a sample.yaml file of:
|
Given a sample.yaml file of:
|
||||||
```yaml
|
```yaml
|
||||||
|
17
yaml.go
17
yaml.go
@ -115,7 +115,14 @@ func readYaml(c *cli.Context, parsedData *map[interface{}]interface{}) {
|
|||||||
if len(c.Args()) == 0 {
|
if len(c.Args()) == 0 {
|
||||||
log.Fatalf("Must provide filename")
|
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)
|
err := yaml.Unmarshal([]byte(rawData), &parsedData)
|
||||||
if err != nil {
|
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 {
|
func readFile(filename string) []byte {
|
||||||
var rawData, readError = ioutil.ReadFile(filename)
|
var rawData, readError = ioutil.ReadFile(filename)
|
||||||
if readError != nil {
|
if readError != nil {
|
||||||
|
Loading…
Reference in New Issue
Block a user