From 03272881c6a18c2009c369edfc1debd829aeaea4 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Thu, 2 Apr 2015 20:29:29 +0900 Subject: [PATCH] Add support for YAML and JSON config files --- main.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index 3e8608b..27f02fb 100644 --- a/main.go +++ b/main.go @@ -23,9 +23,11 @@ package main import ( + "encoding/json" "flag" "fmt" "github.com/naoina/toml" + "gopkg.in/yaml.v2" "io/ioutil" "log" "os" @@ -47,8 +49,21 @@ func parse(filename string) (*config, error) { } conf := &config{} - if err := toml.Unmarshal(bytes, &conf); err != nil { - return nil, err + switch path.Ext(filename) { + case "json": + if err := json.Unmarshal(bytes, &conf); err != nil { + return nil, err + } + case "toml": + if err := toml.Unmarshal(bytes, &conf); err != nil { + return nil, err + } + case "yaml": + if err := yaml.Unmarshal(bytes, &conf); err != nil { + return nil, err + } + default: + return nil, fmt.Errorf("Unsupported configuration file format") } return conf, nil