mirror of
https://github.com/mikefarah/yq.git
synced 2024-12-19 20:19:04 +00:00
Can update yam to stdout
This commit is contained in:
parent
2a0290aeac
commit
364c1a8af3
18
README.md
18
README.md
@ -44,6 +44,22 @@ yaml sample.yaml b.e.1.name
|
||||
```
|
||||
will output 'sam'
|
||||
|
||||
### Updating yaml
|
||||
Given a sample.yaml file of:
|
||||
```yaml
|
||||
b:
|
||||
c: 2
|
||||
```
|
||||
then
|
||||
```bash
|
||||
yaml w sample.yaml b.c 'cat'
|
||||
```
|
||||
will output:
|
||||
```yaml
|
||||
b:
|
||||
c: cat
|
||||
```
|
||||
|
||||
|
||||
## TODO
|
||||
* Updating yaml files
|
||||
* Handling '.' in path names
|
||||
|
35
yaml.go
35
yaml.go
@ -30,37 +30,59 @@ func main() {
|
||||
},
|
||||
}
|
||||
app.Action = readProperty
|
||||
app.Flags = []cli.Flag{
|
||||
cli.StringFlag{
|
||||
Name: "trim, t",
|
||||
Value: "true",
|
||||
Usage: "trim output",
|
||||
},
|
||||
}
|
||||
app.Run(os.Args)
|
||||
}
|
||||
|
||||
func readProperty(c *cli.Context) {
|
||||
|
||||
var parsedData map[interface{}]interface{}
|
||||
|
||||
readYaml(c, &parsedData)
|
||||
|
||||
if len(c.Args()) == 1 {
|
||||
printYaml(parsedData, c.Bool("trim"))
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
var path = c.Args()[1]
|
||||
var paths = strings.Split(path, ".")
|
||||
|
||||
printYaml(readMap(parsedData, paths[0], paths[1:len(paths)]))
|
||||
printYaml(readMap(parsedData, paths[0], paths[1:len(paths)]), c.Bool("trim"))
|
||||
}
|
||||
|
||||
func writeProperty(c *cli.Context) {
|
||||
var parsedData map[interface{}]interface{}
|
||||
readYaml(c, &parsedData)
|
||||
|
||||
if len(c.Args()) != 3 {
|
||||
log.Fatalf("Must provide <filename> <path_to_update> <value>")
|
||||
}
|
||||
|
||||
var path = c.Args()[1]
|
||||
var paths = strings.Split(path, ".")
|
||||
|
||||
write(parsedData, paths[0], paths[1:len(paths)], c.Args()[2])
|
||||
|
||||
printYaml(parsedData)
|
||||
printYaml(parsedData, c.Bool("trim"))
|
||||
}
|
||||
|
||||
func printYaml(context interface{}) {
|
||||
func printYaml(context interface{}, trim bool) {
|
||||
out, err := yaml.Marshal(context)
|
||||
if err != nil {
|
||||
log.Fatalf("error printing yaml: %v", err)
|
||||
}
|
||||
fmt.Println(string(out))
|
||||
outStr := string(out)
|
||||
if trim {
|
||||
outStr = strings.Trim(outStr, "\n ")
|
||||
}
|
||||
fmt.Println(outStr)
|
||||
}
|
||||
|
||||
func readYaml(c *cli.Context, parsedData *map[interface{}]interface{}) {
|
||||
@ -69,11 +91,6 @@ func readYaml(c *cli.Context, parsedData *map[interface{}]interface{}) {
|
||||
}
|
||||
var rawData = readFile(c.Args()[0])
|
||||
|
||||
if len(c.Args()) == 1 {
|
||||
fmt.Println(string(rawData[:]))
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
err := yaml.Unmarshal([]byte(rawData), &parsedData)
|
||||
if err != nil {
|
||||
log.Fatalf("error: %v", err)
|
||||
|
Loading…
Reference in New Issue
Block a user