call hugo as external command
This commit is contained in:
parent
a9ba42c3d0
commit
9e135967cb
|
@ -55,6 +55,5 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
|
|||
}
|
||||
}
|
||||
|
||||
conf.Args = append([]string{"--source", conf.Path}, conf.Args...)
|
||||
return conf, nil
|
||||
}
|
||||
|
|
29
hugo.go
29
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 {
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue