filebrowser/config/config.go

75 lines
1.7 KiB
Go
Raw Normal View History

2016-06-25 20:57:10 +00:00
package config
import (
"fmt"
"io/ioutil"
"net/http"
"strings"
"github.com/mholt/caddy"
)
// Config is a configuration for browsing in a particualr path.
type Config struct {
PathScope string
Root http.FileSystem
BaseURL string
StyleSheet string // Costum stylesheet
2016-06-29 09:16:10 +00:00
FrontMatter string // Default frontmatter to save files in
2016-06-26 17:30:08 +00:00
HugoEnabled bool // Enables the Hugo plugin for File Manager
2016-06-25 20:57:10 +00:00
}
// Parse parses the configuration set by the user so it can
// be used by the middleware
func Parse(c *caddy.Controller) ([]Config, error) {
var configs []Config
appendConfig := func(cfg Config) error {
for _, c := range configs {
if c.PathScope == cfg.PathScope {
return fmt.Errorf("duplicate file managing config for %s", c.PathScope)
}
}
configs = append(configs, cfg)
return nil
}
for c.Next() {
2016-06-29 09:41:17 +00:00
var cfg = Config{PathScope: ".", BaseURL: "", FrontMatter: "yaml", HugoEnabled: false}
2016-06-25 20:57:10 +00:00
for c.NextBlock() {
switch c.Val() {
case "show":
if !c.NextArg() {
return configs, c.ArgErr()
}
cfg.PathScope = c.Val()
cfg.PathScope = strings.TrimSuffix(cfg.PathScope, "/")
case "on":
if !c.NextArg() {
return configs, c.ArgErr()
}
cfg.BaseURL = c.Val()
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
cfg.BaseURL = strings.TrimSuffix(cfg.BaseURL, "/")
cfg.BaseURL = "/" + cfg.BaseURL
case "styles":
if !c.NextArg() {
return configs, c.ArgErr()
}
tplBytes, err := ioutil.ReadFile(c.Val())
if err != nil {
return configs, err
}
cfg.StyleSheet = string(tplBytes)
}
}
cfg.Root = http.Dir(cfg.PathScope)
if err := appendConfig(cfg); err != nil {
return configs, err
}
}
return configs, nil
}