diff --git a/config/config.go b/config/config.go index 20332902..ba8a817c 100644 --- a/config/config.go +++ b/config/config.go @@ -55,6 +55,5 @@ func ParseHugo(c *setup.Controller) (*Config, error) { } } - conf.Args = append([]string{"--source", conf.Path}, conf.Args...) return conf, nil } diff --git a/hugo.go b/hugo.go index 9d0b679c..c1736cc2 100644 --- a/hugo.go +++ b/hugo.go @@ -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 { diff --git a/utils/utils.go b/utils/utils.go index bd9388f4..3f719bf2 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -6,6 +6,7 @@ import ( "log" "net/http" "os" + "os/exec" "reflect" "strings" "text/template" @@ -13,8 +14,6 @@ import ( "github.com/hacdias/caddy-hugo/assets" "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 @@ -168,14 +167,20 @@ func ParseComponents(r *http.Request) []string { func Run(c *config.Config) { os.RemoveAll(c.Path + "public") - commands.MainSite = nil - viper.Reset() - commands.HugoCmd.ParseFlags(c.Args) - if err := commands.HugoCmd.RunE(nil, nil); err != nil { + if err := RunCommand("hugo", c.Args, c.Path); err != nil { 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{ "youtube": "YouTube", "github": "GitHub",