2015-09-20 08:15:21 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
2016-03-06 12:39:36 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2015-09-20 08:15:21 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-06-07 15:08:52 +00:00
|
|
|
"github.com/hacdias/caddy-hugo/tools/installer"
|
|
|
|
"github.com/mholt/caddy"
|
2015-09-20 08:15:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config is the add-on configuration set on Caddyfile
|
|
|
|
type Config struct {
|
2015-10-16 18:34:27 +00:00
|
|
|
Public string // Public content path
|
|
|
|
Path string // Hugo files path
|
|
|
|
Styles string // Admin styles path
|
|
|
|
Args []string // Hugo arguments
|
2016-02-20 22:31:46 +00:00
|
|
|
Hugo string // Hugo executable path
|
2016-06-07 06:54:10 +00:00
|
|
|
Admin string // Hugo admin URL
|
2016-03-06 12:39:36 +00:00
|
|
|
Git bool // Is this site a git repository
|
2015-09-20 08:15:21 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 15:08:52 +00:00
|
|
|
// Parse parses the configuration file
|
|
|
|
func Parse(c *caddy.Controller, root string) (*Config, error) {
|
2015-09-30 21:03:28 +00:00
|
|
|
conf := &Config{
|
2016-06-07 15:08:52 +00:00
|
|
|
Public: strings.Replace(root, "./", "", -1),
|
2016-06-07 06:54:10 +00:00
|
|
|
Admin: "/admin",
|
2015-10-16 18:34:27 +00:00
|
|
|
Path: "./",
|
2016-03-06 12:39:36 +00:00
|
|
|
Git: false,
|
2016-02-20 22:31:46 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 15:08:52 +00:00
|
|
|
conf.Hugo = installer.GetPath()
|
2015-09-20 08:15:21 +00:00
|
|
|
|
|
|
|
for c.Next() {
|
2015-09-30 21:03:28 +00:00
|
|
|
args := c.RemainingArgs()
|
|
|
|
|
|
|
|
switch len(args) {
|
|
|
|
case 1:
|
|
|
|
conf.Path = args[0]
|
|
|
|
conf.Path = strings.TrimSuffix(conf.Path, "/")
|
|
|
|
conf.Path += "/"
|
|
|
|
}
|
|
|
|
|
2015-09-20 08:15:21 +00:00
|
|
|
for c.NextBlock() {
|
|
|
|
switch c.Val() {
|
|
|
|
case "styles":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
conf.Styles = c.Val()
|
|
|
|
// Remove the beginning slash if it exists or not
|
|
|
|
conf.Styles = strings.TrimPrefix(conf.Styles, "/")
|
|
|
|
// Add a beginning slash to make a
|
|
|
|
conf.Styles = "/" + conf.Styles
|
2016-06-07 06:54:10 +00:00
|
|
|
case "admin":
|
|
|
|
if !c.NextArg() {
|
|
|
|
return nil, c.ArgErr()
|
|
|
|
}
|
|
|
|
conf.Admin = c.Val()
|
|
|
|
conf.Admin = strings.TrimPrefix(conf.Admin, "/")
|
|
|
|
conf.Admin = "/" + conf.Admin
|
2016-02-19 20:58:56 +00:00
|
|
|
default:
|
|
|
|
key := "--" + c.Val()
|
|
|
|
value := "true"
|
2015-09-30 21:03:28 +00:00
|
|
|
|
2016-02-19 20:58:56 +00:00
|
|
|
if c.NextArg() {
|
|
|
|
value = c.Val()
|
2015-09-20 21:00:25 +00:00
|
|
|
}
|
2015-10-16 18:34:27 +00:00
|
|
|
|
2016-02-20 12:04:12 +00:00
|
|
|
conf.Args = append(conf.Args, key+"="+value)
|
2015-09-20 08:15:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-06 12:39:36 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(conf.Path, ".git")); err == nil {
|
|
|
|
conf.Git = true
|
|
|
|
}
|
|
|
|
|
2015-09-20 08:15:21 +00:00
|
|
|
return conf, nil
|
|
|
|
}
|