call hugo as external command

This commit is contained in:
Henrique Dias
2016-02-20 11:31:59 +00:00
parent a9ba42c3d0
commit 9e135967cb
3 changed files with 26 additions and 21 deletions

29
hugo.go
View File

@@ -21,39 +21,40 @@ import (
"github.com/hacdias/caddy-hugo/utils"
"github.com/mholt/caddy/caddy/setup"
"github.com/mholt/caddy/middleware"
"github.com/spf13/cobra"
"github.com/spf13/hugo/commands"
)
// Setup is the init function of Caddy plugins and it configures the whole
// middleware thing.
func Setup(c *setup.Controller) (middleware.Middleware, error) {
// TODO: install Hugo first?
config, _ := config.ParseHugo(c)
// Checks if there is an Hugo website in the path that is provided.
// If not, a new website will be created.
create := false
create := true
if _, err := os.Stat(config.Path + "config.yaml"); os.IsNotExist(err) {
create = true
if _, err := os.Stat(config.Path + "config.yaml"); err == nil {
create = false
}
if _, err := os.Stat(config.Path + "config.json"); os.IsNotExist(err) {
create = true
if _, err := os.Stat(config.Path + "config.json"); err == nil {
create = false
}
if _, err := os.Stat(config.Path + "config.toml"); os.IsNotExist(err) {
create = true
if _, err := os.Stat(config.Path + "config.toml"); err == nil {
create = false
}
if create {
cmd := &cobra.Command{}
cmd.Flags().Bool("force", true, "")
commands.NewSite(cmd, []string{config.Path})
err := utils.RunCommand("hugo", []string{"new", "site", config.Path, "--force"}, ".")
if err != nil {
log.Panic(err)
}
}
// Generates the Hugo website for the first time the plugin is activated.
utils.Run(config)
go utils.Run(config)
return func(next middleware.Handler) middleware.Handler {
return &CaddyHugo{Next: next, Config: config}
@@ -139,7 +140,7 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
// Whenever the header "X-Regenerate" is true, the website should be
// regenerated. Used in edit and settings, for example.
if r.Header.Get("X-Regenerate") == "true" {
utils.Run(h.Config)
go utils.Run(h.Config)
}
if err != nil {