filebrowser/setup.go

237 lines
5.3 KiB
Go
Raw Normal View History

package hugo
import (
2016-08-25 20:01:33 +00:00
"errors"
2016-07-02 22:29:47 +00:00
"fmt"
2016-06-21 14:28:15 +00:00
"io/ioutil"
"log"
2017-01-03 21:21:40 +00:00
"net/http"
"os"
2016-08-25 19:52:53 +00:00
"os/exec"
2016-06-29 09:45:15 +00:00
"path/filepath"
2016-07-01 15:14:08 +00:00
"reflect"
2016-06-21 14:28:15 +00:00
"strings"
2016-06-21 14:28:15 +00:00
"github.com/hacdias/caddy-filemanager"
2016-06-28 13:59:33 +00:00
"github.com/hacdias/caddy-filemanager/config"
2016-06-29 09:45:15 +00:00
"github.com/hacdias/caddy-filemanager/frontmatter"
2016-06-28 13:59:33 +00:00
"github.com/hacdias/caddy-hugo/utils/commands"
"github.com/mholt/caddy"
"github.com/mholt/caddy/caddyhttp/httpserver"
)
2016-06-29 09:45:15 +00:00
// AssetsURL is the base url for the assets
2016-08-28 09:15:55 +00:00
const (
AssetsURL = "/_hugointernal"
HugoNotFound = "It seems that you don't have 'hugo' on your PATH."
)
2016-06-28 13:59:33 +00:00
func init() {
caddy.RegisterPlugin("hugo", caddy.Plugin{
ServerType: "http",
Action: setup,
})
}
// Setup is the init function of Caddy plugins and it configures the whole
// middleware thing.
func setup(c *caddy.Controller) error {
2016-06-22 20:32:33 +00:00
cnf := httpserver.GetConfig(c)
2016-08-28 09:15:55 +00:00
conf, fm, err := parse(c, cnf.Root)
if err != nil {
return err
}
// Generates the Hugo website for the first time the plugin is activated.
go RunHugo(conf, true)
mid := func(next httpserver.Handler) httpserver.Handler {
fm.Next = next
return &Hugo{
Next: next,
Config: conf,
FileManager: fm,
}
}
cnf.AddMiddleware(mid)
return nil
}
// Config is a configuration for managing a particular hugo website.
type Config struct {
Public string // Public content path
Root string // Hugo files path
Hugo string // Hugo executable location
Styles string // Admin styles path
Args []string // Hugo arguments
BaseURL string // BaseURL of admin interface
WebDavURL string
BeforePublish config.CommandFunc
AfterPublish config.CommandFunc
2016-08-28 09:15:55 +00:00
}
// Parse parses the configuration set by the user so it can be
// used by the middleware
func parse(c *caddy.Controller, root string) (*Config, *filemanager.FileManager, error) {
var (
cfg *Config
fm *filemanager.FileManager
err error
tokens string
)
cfg = new(Config)
if cfg.Hugo, err = exec.LookPath("hugo"); err != nil {
fmt.Println(HugoNotFound)
return cfg, fm, errors.New(HugoNotFound)
}
for c.Next() {
cfg.Public = strings.Replace(root, "./", "", -1)
cfg.BaseURL = "/admin"
cfg.Root = "./"
2017-01-03 21:21:40 +00:00
cfg.BeforePublish = func(r *http.Request, c *config.Config, u *config.User) error { return nil }
cfg.AfterPublish = func(r *http.Request, c *config.Config, u *config.User) error { return nil }
2016-08-28 09:15:55 +00:00
args := c.RemainingArgs()
if len(args) >= 1 {
cfg.Root = args[0]
cfg.Root = strings.TrimSuffix(cfg.Root, "/")
cfg.Root += "/"
}
if len(args) >= 2 {
cfg.BaseURL = args[1]
cfg.BaseURL = strings.TrimPrefix(cfg.BaseURL, "/")
cfg.BaseURL = "/" + cfg.BaseURL
}
for c.NextBlock() {
switch c.Val() {
case "flag":
if !c.NextArg() {
return cfg, &filemanager.FileManager{}, c.ArgErr()
}
2017-01-14 11:04:04 +00:00
flag := c.Val()
2016-08-28 09:15:55 +00:00
value := "true"
2017-01-14 11:04:04 +00:00
if c.NextArg() {
value = c.Val()
2016-08-28 09:15:55 +00:00
}
2017-01-14 11:04:04 +00:00
cfg.Args = append(cfg.Args, "--"+flag+"="+value)
case "before_publish":
if cfg.BeforePublish, err = config.CommandRunner(c); err != nil {
return cfg, &filemanager.FileManager{}, err
}
case "after_publish":
if cfg.AfterPublish, err = config.CommandRunner(c); err != nil {
return cfg, &filemanager.FileManager{}, err
}
2016-08-28 09:15:55 +00:00
default:
line := "\n\t" + c.Val()
if c.NextArg() {
line += " " + c.Val()
}
tokens += line
}
}
}
tokens = "filemanager " + cfg.BaseURL + " {\n\tshow " + cfg.Root + tokens
tokens += "\n}"
fmConfig, err := config.Parse(caddy.NewTestController("http", tokens))
if err != nil {
return cfg, fm, err
}
fm = &filemanager.FileManager{Configs: fmConfig}
fm.Configs[0].HugoEnabled = true
format := getFrontMatter(cfg)
2016-12-11 10:41:03 +00:00
cfg.WebDavURL = fm.Configs[0].WebDavURL
2016-08-28 09:15:55 +00:00
for _, user := range fm.Configs[0].Users {
user.FrontMatter = format
}
if err != nil {
return cfg, fm, err
}
return cfg, fm, nil
}
func getFrontMatter(conf *Config) string {
2016-07-01 14:01:21 +00:00
format := "toml"
2016-06-29 09:45:15 +00:00
// Checks if there is an Hugo website in the path that is provided.
// If not, a new website will be created.
create := true
2016-06-21 14:28:15 +00:00
if _, err := os.Stat(conf.Root + "config.yaml"); err == nil {
2016-06-29 09:45:15 +00:00
format = "yaml"
create = false
}
2016-06-21 14:28:15 +00:00
if _, err := os.Stat(conf.Root + "config.json"); err == nil {
2016-06-29 09:45:15 +00:00
format = "json"
create = false
}
2016-06-21 14:28:15 +00:00
if _, err := os.Stat(conf.Root + "config.toml"); err == nil {
2016-06-29 09:45:15 +00:00
format = "toml"
create = false
}
if create {
2016-06-28 13:59:33 +00:00
err := commands.Run(conf.Hugo, []string{"new", "site", conf.Root, "--force"}, ".")
if err != nil {
2016-07-02 22:29:47 +00:00
log.Fatal(err)
}
}
2016-06-29 09:45:15 +00:00
// Get Default FrontMatter
bytes, err := ioutil.ReadFile(filepath.Clean(conf.Root + "/config." + format))
if err != nil {
2016-07-02 22:29:47 +00:00
log.Println(err)
fmt.Printf("Can't get the default frontmatter from the configuration. %s will be used.\n", format)
2016-07-01 15:14:08 +00:00
} else {
2016-11-14 20:19:31 +00:00
bytes = frontmatter.AppendRune(bytes, format)
2016-07-02 22:29:47 +00:00
f, err := frontmatter.Unmarshal(bytes)
if err != nil {
log.Println(err)
fmt.Printf("Can't get the default frontmatter from the configuration. %s will be used.\n", format)
} else {
kind := reflect.TypeOf(f)
if kind == reflect.TypeOf(map[interface{}]interface{}{}) {
if val, ok := f.(map[interface{}]interface{})["metaDataFormat"]; ok {
format = val.(string)
}
} else {
if val, ok := f.(map[string]interface{})["metaDataFormat"]; ok {
format = val.(string)
}
}
2016-07-01 15:14:08 +00:00
}
2016-06-29 09:45:15 +00:00
}
2016-08-28 09:15:55 +00:00
return format
2016-06-21 14:28:15 +00:00
}