From a25e944d92c3f0575bb15e68921e2368c87f1895 Mon Sep 17 00:00:00 2001 From: Danilo Gvozdenovic Date: Fri, 20 Jul 2018 14:42:50 +0200 Subject: [PATCH 1/2] added support for YAML conf file and changed default conf file to api2html.conf --- cmd/serve.go | 2 +- engine/config.go | 25 ++++++++++++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/cmd/serve.go b/cmd/serve.go index 872ffef..65037e1 100644 --- a/cmd/serve.go +++ b/cmd/serve.go @@ -29,7 +29,7 @@ var ( func init() { rootCmd.AddCommand(serveCmd) - serveCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "config.json", "Path to the configuration filename") + serveCmd.PersistentFlags().StringVarP(&cfgFile, "config", "c", "api2html.conf", "Path to the configuration filename") serveCmd.PersistentFlags().BoolVarP(&devel, "devel", "d", false, "Enable the devel") serveCmd.PersistentFlags().IntVarP(&port, "port", "p", 8080, "Listen port") } diff --git a/engine/config.go b/engine/config.go index 1f2ff25..cc03897 100644 --- a/engine/config.go +++ b/engine/config.go @@ -2,10 +2,15 @@ package engine import ( "encoding/json" + "bytes" "io" "os" + + "github.com/ghodss/yaml" + ) + // ParseConfigFromFile creates a Config with the contents of the received filepath func ParseConfigFromFile(path string) (Config, error) { configFile, err := os.Open(path) @@ -20,10 +25,24 @@ func ParseConfigFromFile(path string) (Config, error) { // ParseConfig parses the content of the reader into a Config func ParseConfig(r io.Reader) (Config, error) { var cfg Config - err := json.NewDecoder(r).Decode(&cfg) - if err != nil { - return cfg, err + var buf bytes.Buffer + + buf.ReadFrom(r) + cb := buf.Bytes() + + switch { + case bytes.HasPrefix(bytes.TrimSpace(cb), []byte("{")): + err := json.Unmarshal(cb, &cfg) + if err != nil { + return cfg, err + } + default: + err := yaml.Unmarshal(cb, &cfg) + if err != nil { + return cfg, err + } } + for p, page := range cfg.Pages { if len(page.Extra) == 0 { cfg.Pages[p].Extra = cfg.Extra From f56dcec7adc31c39b2814f56de7f806e395bcac5 Mon Sep 17 00:00:00 2001 From: Danilo Gvozdenovic Date: Sun, 22 Jul 2018 20:05:15 +0200 Subject: [PATCH 2/2] added github.com/ghodss/yaml to Gopkg.toml --- Gopkg.toml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/Gopkg.toml b/Gopkg.toml index 3802c63..89a4379 100644 --- a/Gopkg.toml +++ b/Gopkg.toml @@ -38,3 +38,7 @@ [[constraint]] name = "github.com/rakyll/statik" version = "0.1.1" + +[[constraint]] + name = "github.com/ghodss/yaml" + version = "1.0.0"