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

15
main.go
View File

@ -23,9 +23,11 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/naoina/toml"
"gopkg.in/yaml.v2"
"io/ioutil"
"log"
"os"
@ -47,9 +49,22 @@ func parse(filename string) (*config, error) {
}
conf := &config{}
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
}