call hugo as external command

pull/57/head
Henrique Dias 2016-02-20 11:31:59 +00:00
parent a9ba42c3d0
commit 9e135967cb
3 changed files with 26 additions and 21 deletions

View File

@ -55,6 +55,5 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
} }
} }
conf.Args = append([]string{"--source", conf.Path}, conf.Args...)
return conf, nil return conf, nil
} }

29
hugo.go
View File

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

View File

@ -6,6 +6,7 @@ import (
"log" "log"
"net/http" "net/http"
"os" "os"
"os/exec"
"reflect" "reflect"
"strings" "strings"
"text/template" "text/template"
@ -13,8 +14,6 @@ import (
"github.com/hacdias/caddy-hugo/assets" "github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/config" "github.com/hacdias/caddy-hugo/config"
"github.com/spf13/hugo/commands"
"github.com/spf13/viper"
) )
// CanBeEdited checks if the extension of a file is supported by the editor // CanBeEdited checks if the extension of a file is supported by the editor
@ -168,14 +167,20 @@ func ParseComponents(r *http.Request) []string {
func Run(c *config.Config) { func Run(c *config.Config) {
os.RemoveAll(c.Path + "public") os.RemoveAll(c.Path + "public")
commands.MainSite = nil if err := RunCommand("hugo", c.Args, c.Path); err != nil {
viper.Reset()
commands.HugoCmd.ParseFlags(c.Args)
if err := commands.HugoCmd.RunE(nil, nil); err != nil {
log.Panic(err) log.Panic(err)
} }
} }
// RunCommand executes an external command
func RunCommand(command string, args []string, path string) error {
cmd := exec.Command(command, args...)
cmd.Dir = path
cmd.Stdout = os.Stderr
cmd.Stderr = os.Stderr
return cmd.Run()
}
var splitCapitalizeExceptions = map[string]string{ var splitCapitalizeExceptions = map[string]string{
"youtube": "YouTube", "youtube": "YouTube",
"github": "GitHub", "github": "GitHub",