2013-07-04 15:32:55 +00:00
|
|
|
// Copyright © 2013 Steve Francia <spf@spf13.com>.
|
|
|
|
//
|
|
|
|
// Licensed under the Simple Public License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://opensource.org/licenses/Simple-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package hugolib
|
|
|
|
|
|
|
|
import (
|
2013-07-12 02:04:57 +00:00
|
|
|
"encoding/json"
|
2013-07-04 15:32:55 +00:00
|
|
|
"fmt"
|
2013-07-19 07:10:42 +00:00
|
|
|
"github.com/BurntSushi/toml"
|
2013-07-04 15:32:55 +00:00
|
|
|
"io/ioutil"
|
2013-07-19 07:10:42 +00:00
|
|
|
"launchpad.net/goyaml"
|
2013-07-04 15:32:55 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2013-07-12 02:55:07 +00:00
|
|
|
"strings"
|
2013-07-04 15:32:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// config file items
|
|
|
|
type Config struct {
|
2013-07-26 22:06:13 +00:00
|
|
|
ContentDir, PublishDir, BaseUrl, StaticDir string
|
|
|
|
Path, CacheDir, LayoutDir, DefaultLayout string
|
|
|
|
ConfigFile string
|
|
|
|
Title string
|
|
|
|
Indexes map[string]string // singular, plural
|
|
|
|
ProcessFilters map[string][]string
|
|
|
|
BuildDrafts, UglyUrls, Verbose bool
|
2013-07-04 15:32:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var c Config
|
|
|
|
|
|
|
|
// Read cfgfile or setup defaults.
|
|
|
|
func SetupConfig(cfgfile *string, path *string) *Config {
|
|
|
|
c.setPath(*path)
|
|
|
|
|
2013-07-19 07:10:42 +00:00
|
|
|
cfg, err := c.findConfigFile(*cfgfile)
|
|
|
|
c.ConfigFile = cfg
|
2013-07-04 15:32:55 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("%v", err)
|
|
|
|
fmt.Println(" using defaults instead")
|
|
|
|
}
|
|
|
|
|
|
|
|
// set defaults
|
2013-07-26 22:06:13 +00:00
|
|
|
c.ContentDir = "content"
|
2013-07-04 15:32:55 +00:00
|
|
|
c.LayoutDir = "layouts"
|
|
|
|
c.PublishDir = "public"
|
|
|
|
c.StaticDir = "static"
|
|
|
|
c.DefaultLayout = "post"
|
|
|
|
c.BuildDrafts = false
|
2013-07-19 07:10:42 +00:00
|
|
|
c.UglyUrls = false
|
|
|
|
c.Verbose = false
|
2013-07-04 15:32:55 +00:00
|
|
|
|
2013-07-19 07:10:42 +00:00
|
|
|
c.readInConfig()
|
2013-07-04 15:32:55 +00:00
|
|
|
|
|
|
|
// set index defaults if none provided
|
|
|
|
if len(c.Indexes) == 0 {
|
|
|
|
c.Indexes = make(map[string]string)
|
|
|
|
c.Indexes["tag"] = "tags"
|
|
|
|
c.Indexes["category"] = "categories"
|
|
|
|
}
|
2013-07-12 02:55:07 +00:00
|
|
|
|
2013-07-19 07:10:42 +00:00
|
|
|
if !strings.HasSuffix(c.BaseUrl, "/") {
|
|
|
|
c.BaseUrl = c.BaseUrl + "/"
|
|
|
|
}
|
2013-07-12 02:55:07 +00:00
|
|
|
|
2013-07-04 15:32:55 +00:00
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
2013-07-12 02:04:57 +00:00
|
|
|
func (c *Config) readInConfig() {
|
2013-07-19 07:10:42 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-07-12 02:04:57 +00:00
|
|
|
}
|
|
|
|
|
2013-07-04 15:32:55 +00:00
|
|
|
func (c *Config) setPath(p string) {
|
|
|
|
if p == "" {
|
|
|
|
path, err := FindPath()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error finding path: %s", err)
|
|
|
|
}
|
|
|
|
c.Path = path
|
|
|
|
} else {
|
|
|
|
path, err := filepath.Abs(p)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("Error finding path: %s", err)
|
|
|
|
}
|
|
|
|
c.Path = path
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) GetPath() string {
|
|
|
|
if c.Path == "" {
|
|
|
|
c.setPath("")
|
|
|
|
}
|
|
|
|
return c.Path
|
|
|
|
}
|
|
|
|
|
|
|
|
func FindPath() (string, error) {
|
|
|
|
serverFile, err := filepath.Abs(os.Args[0])
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("Can't get absolute path for executable: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Dir(serverFile)
|
|
|
|
realFile, err := filepath.EvalSymlinks(serverFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if _, err = os.Stat(serverFile + ".exe"); err == nil {
|
|
|
|
realFile = filepath.Clean(serverFile + ".exe")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && realFile != serverFile {
|
|
|
|
path = filepath.Dir(realFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
return path, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) GetAbsPath(name string) string {
|
2013-08-12 20:57:47 +00:00
|
|
|
if filepath.IsAbs(name) {
|
2013-07-04 15:32:55 +00:00
|
|
|
return name
|
|
|
|
}
|
|
|
|
|
|
|
|
p := filepath.Join(c.GetPath(), name)
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Config) findConfigFile(configFileName string) (string, error) {
|
|
|
|
|
2013-07-19 07:10:42 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
2013-07-04 15:32:55 +00:00
|
|
|
}
|