Add support for YAML and JSON config files

This commit is contained in:
Alex Yatskov 2015-04-02 20:29:29 +09:00
parent 2c89063d13
commit 03272881c6

19
main.go
View File

@ -23,9 +23,11 @@
package main package main
import ( import (
"encoding/json"
"flag" "flag"
"fmt" "fmt"
"github.com/naoina/toml" "github.com/naoina/toml"
"gopkg.in/yaml.v2"
"io/ioutil" "io/ioutil"
"log" "log"
"os" "os"
@ -47,8 +49,21 @@ func parse(filename string) (*config, error) {
} }
conf := &config{} conf := &config{}
if err := toml.Unmarshal(bytes, &conf); err != nil { switch path.Ext(filename) {
return nil, err 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 return conf, nil