summaryrefslogtreecommitdiffstats
path: root/hugolib/config.go
diff options
context:
space:
mode:
authorspf13 <steve.francia@gmail.com>2013-07-11 22:04:57 -0400
committerspf13 <steve.francia@gmail.com>2013-07-11 22:04:57 -0400
commit75a2e6d4e808ab585c10dc330f9ab2f6c4619411 (patch)
treeb4b8b834f0f1b54b1b76580d0528206b658981b4 /hugolib/config.go
parentd9b5f9cd9e2807555c428b3df40035086bafec4f (diff)
Now support for config files as yaml, json or toml
Diffstat (limited to 'hugolib/config.go')
-rw-r--r--hugolib/config.go84
1 files changed, 62 insertions, 22 deletions
diff --git a/hugolib/config.go b/hugolib/config.go
index 84d6ce966..a7fdbd4d2 100644
--- a/hugolib/config.go
+++ b/hugolib/config.go
@@ -15,6 +15,8 @@ package hugolib
import (
"launchpad.net/goyaml"
+ "github.com/BurntSushi/toml"
+ "encoding/json"
"fmt"
"io/ioutil"
"os"
@@ -26,6 +28,8 @@ import (
type Config struct {
SourceDir, PublishDir, BaseUrl, StaticDir string
Path, CacheDir, LayoutDir, DefaultLayout string
+ ConfigFile string
+ Title string
Indexes map[string]string // singular, plural
ProcessFilters map[string][]string
BuildDrafts bool
@@ -37,7 +41,8 @@ var c Config
func SetupConfig(cfgfile *string, path *string) *Config {
c.setPath(*path)
- configPath, err := c.findConfigFile(*cfgfile)
+ cfg , err := c.findConfigFile(*cfgfile)
+ c.ConfigFile = cfg
if err != nil {
fmt.Printf("%v", err)
@@ -45,7 +50,6 @@ func SetupConfig(cfgfile *string, path *string) *Config {
}
// set defaults
-
c.SourceDir = "content"
c.LayoutDir = "layouts"
c.PublishDir = "public"
@@ -53,13 +57,7 @@ func SetupConfig(cfgfile *string, path *string) *Config {
c.DefaultLayout = "post"
c.BuildDrafts = false
- file, err := ioutil.ReadFile(configPath)
- if err == nil {
- if err := goyaml.Unmarshal(file, &c); err != nil {
- fmt.Printf("Error parsing config: %s", err)
- os.Exit(1)
- }
- }
+ c.readInConfig()
// set index defaults if none provided
if len(c.Indexes) == 0 {
@@ -70,6 +68,32 @@ func SetupConfig(cfgfile *string, path *string) *Config {
return &c
}
+func (c *Config) readInConfig() {
+ file, err := ioutil.ReadFile(c.ConfigFile)
+ if err == nil {
+ switch path.Ext(c.ConfigFile) {
+ case ".yaml":
+ if err := goyaml.Unmarshal(file, &c); err != nil {
+ fmt.Printf("Error parsing config: %s", err)
+ os.Exit(1)
+ }
+
+ case ".json":
+ if err := json.Unmarshal(file, &c); err != nil {
+ fmt.Printf("Error parsing config: %s", err)
+ os.Exit(1)
+ }
+
+ case ".toml":
+ if _, err := toml.Decode(string(file), &c); err != nil {
+ fmt.Printf("Error parsing config: %s", err)
+ os.Exit(1)
+ }
+ }
+ }
+ Printer(c)
+}
+
func (c *Config) setPath(p string) {
if p == "" {
path, err := FindPath()
@@ -126,18 +150,34 @@ func (c *Config) GetAbsPath(name string) string {
}
func (c *Config) findConfigFile(configFileName string) (string, error) {
- // If the full path is given, just use that
- if path.IsAbs(configFileName) {
- return configFileName, nil
- }
-
- // Else check the local directory
- t := c.GetAbsPath(configFileName)
- if b, _ := exists(t); b {
- return t, nil
- } else {
- return "", fmt.Errorf("config file not found at: %s", t)
- }
- return "", nil // This line won't ever happen.. looking forward to go 1.1 when I don't need it
+ if configFileName == "" { // config not specified, let's search
+ if b, _ := exists(c.GetAbsPath("config.json")); b {
+ return c.GetAbsPath("config.json"), nil
+ }
+
+ if b, _ := exists(c.GetAbsPath("config.toml")); b {
+ return c.GetAbsPath("config.toml"), nil
+ }
+
+ if b, _ := exists(c.GetAbsPath("config.yaml")); b {
+ return c.GetAbsPath("config.yaml"), nil
+ }
+
+ return "", fmt.Errorf("config file not found in: %s", c.GetPath())
+
+ } else {
+ // If the full path is given, just use that
+ if path.IsAbs(configFileName) {
+ return configFileName, nil
+ }
+
+ // Else check the local directory
+ t := c.GetAbsPath(configFileName)
+ if b, _ := exists(t); b {
+ return t, nil
+ } else {
+ return "", fmt.Errorf("config file not found at: %s", t)
+ }
+ }
}