yq/yaml.go

48 lines
916 B
Go
Raw Normal View History

2015-09-26 22:15:49 +00:00
package main
import (
"fmt"
"gopkg.in/yaml.v2"
"log"
2015-09-26 22:20:42 +00:00
"io/ioutil"
2015-09-26 23:38:44 +00:00
"os"
"github.com/codegangsta/cli"
2015-09-26 22:15:49 +00:00
)
func main() {
2015-09-26 23:38:44 +00:00
app := cli.NewApp()
app.Name = "yaml"
app.Usage = "command line tool for reading and writing yaml"
app.Commands = []cli.Command{
{
Name: "read",
Aliases: []string{"r"},
Usage: "(default) reads a property from a given yaml file",
Action: read_file,
},
}
app.Action = read_file
app.Run(os.Args)
}
func read_file(c *cli.Context) {
if len(c.Args()) == 0 {
log.Fatalf("Must provide filename")
}
var filename = c.Args()[0]
var raw_data, read_error = ioutil.ReadFile(filename)
2015-09-26 22:20:42 +00:00
if read_error != nil {
log.Fatalf("error: %v", read_error)
}
var parsed_data interface{}
2015-09-26 22:15:49 +00:00
2015-09-26 22:20:42 +00:00
err := yaml.Unmarshal([]byte(raw_data), &parsed_data)
2015-09-26 22:15:49 +00:00
if err != nil {
2015-09-26 22:20:42 +00:00
log.Fatalf("error: %v", err)
2015-09-26 22:15:49 +00:00
}
2015-09-26 22:20:42 +00:00
fmt.Println(parsed_data)
2015-09-26 22:15:49 +00:00
}