simplify a little bit

pull/48/head
Henrique Dias 2016-02-11 20:19:22 +00:00
parent fe83294b14
commit b266262c7a
2 changed files with 25 additions and 42 deletions

23
hugo.go
View File

@ -18,11 +18,34 @@ 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 configures the middleware
func Setup(c *setup.Controller) (middleware.Middleware, error) {
config, _ := config.ParseHugo(c)
create := false
if _, err := os.Stat(config.Path + "config.yaml"); os.IsNotExist(err) {
create = true
}
if _, err := os.Stat(config.Path + "config.json"); os.IsNotExist(err) {
create = true
}
if _, err := os.Stat(config.Path + "config.toml"); os.IsNotExist(err) {
create = true
}
if create {
cmd := &cobra.Command{}
cmd.Flags().Bool("force", true, "")
commands.NewSite(cmd, []string{config.Path})
}
utils.Run(config)
return func(next middleware.Handler) middleware.Handler {

View File

@ -13,7 +13,6 @@ import (
"github.com/hacdias/caddy-hugo/assets"
"github.com/hacdias/caddy-hugo/config"
"github.com/spf13/cobra"
"github.com/spf13/hugo/commands"
"github.com/spf13/viper"
)
@ -167,51 +166,12 @@ func ParseComponents(r *http.Request) []string {
// Run is used to run the static website generator
func Run(c *config.Config) {
create := false
if _, err := os.Stat(c.Path + "config.yaml"); os.IsNotExist(err) {
create = true
}
if _, err := os.Stat(c.Path + "config.json"); os.IsNotExist(err) {
create = true
}
if _, err := os.Stat(c.Path + "config.toml"); os.IsNotExist(err) {
create = true
}
if create {
cmd := &cobra.Command{}
cmd.Flags().Bool("force", true, "")
commands.NewSite(cmd, []string{c.Path})
}
cwd, err := os.Getwd()
if err != nil {
log.Print("Can't get working directory.")
}
err = os.Chdir(c.Path)
if err != nil {
log.Print("Can't get working directory.")
}
viper.Reset()
c.Args = append([]string{"--source", c.Path, "--destination", "public"}, c.Args...)
commands.HugoCmd.ParseFlags(c.Args)
err = commands.HugoCmd.RunE(nil, nil)
if err != nil {
if err := commands.HugoCmd.RunE(nil, nil); err != nil {
log.Print(err)
}
err = os.Chdir(cwd)
if err != nil {
log.Print("Can't get working directory.")
}
}
var splitCapitalizeExceptions = map[string]string{