From 77b3713fc1ce13e9f6f077203961e8b3d8ca4fde Mon Sep 17 00:00:00 2001
From: Henrique Dias <hacdias@gmail.com>
Date: Sun, 27 Sep 2015 13:20:05 +0100
Subject: [PATCH] progresses on #19

---
 README.md         |  8 +++++---
 hugo.go => cms.go | 16 ++++++++--------
 config/config.go  | 39 +++++++++++++++++++++++++++------------
 editor/post.go    |  2 +-
 utils/utils.go    | 17 ++++++++++++++---
 5 files changed, 55 insertions(+), 27 deletions(-)
 rename hugo.go => cms.go (92%)

diff --git a/README.md b/README.md
index c1b7c5e8..eaaccde3 100644
--- a/README.md
+++ b/README.md
@@ -8,15 +8,17 @@ Deploy your Hugo website and enjoy of an admin interface with Caddy server.
 ## Configuration
 
 ```
-hugo {
+cms {
   styles    file
-  flags     flags...
+  hugo      true / false    # default is true
+  command   command         # needed when hugo is false
+  args      args...        # hugo or whatever command flags/args
 }
 ```
 
 + **file** is the relative path to ```public``` folder of the admin UI styles. They will not replace the defaults, they will be added.
 
-+ **flags** are the Hugo flags (those which can be set in the command line) and they must follow one of these syntaxes: ```-f=value``` and ```--flag=value```.
++ **args** are the Hugo flags (those which can be set in the command line) and they must follow one of these syntaxes: ```-f=value``` and ```--flag=value```.
 
 ## Build it from source
 
diff --git a/hugo.go b/cms.go
similarity index 92%
rename from hugo.go
rename to cms.go
index 6afa6c9e..42efde45 100644
--- a/hugo.go
+++ b/cms.go
@@ -2,7 +2,7 @@
 //go:generate go install github.com/jteeuwen/go-bindata/go-bindata
 //go:generate go-bindata -pkg assets -o assets/assets.go templates/ assets/css/ assets/js/ assets/fonts/
 
-package hugo
+package cms
 
 import (
 	"mime"
@@ -22,21 +22,21 @@ import (
 
 // Setup configures the middleware
 func Setup(c *setup.Controller) (middleware.Middleware, error) {
-	config, _ := config.ParseHugo(c)
-	utils.RunHugo(config)
+	config, _ := config.ParseCMS(c)
+	utils.Run(config)
 
 	return func(next middleware.Handler) middleware.Handler {
-		return &CaddyHugo{Next: next, Config: config}
+		return &CaddyCMS{Next: next, Config: config}
 	}, nil
 }
 
-// CaddyHugo main type
-type CaddyHugo struct {
+// CaddyCMS main type
+type CaddyCMS struct {
 	Next   middleware.Handler
 	Config *config.Config
 }
 
-func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
+func (h CaddyCMS) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error) {
 	// Only handle /admin path
 	if middleware.Path(r.URL.Path).Matches("/admin") {
 		var err error
@@ -106,7 +106,7 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
 		// Whenever the header "X-Refenerate" is true, the website should be
 		// regenerated. Used in edit and settings, for example.
 		if r.Header.Get("X-Regenerate") == "true" {
-			utils.RunHugo(h.Config)
+			utils.Run(h.Config)
 		}
 
 		return code, err
diff --git a/config/config.go b/config/config.go
index 91ef7f2f..829fc95c 100644
--- a/config/config.go
+++ b/config/config.go
@@ -1,6 +1,7 @@
 package config
 
 import (
+	"strconv"
 	"strings"
 
 	"github.com/mholt/caddy/config/setup"
@@ -8,15 +9,15 @@ import (
 
 // Config is the add-on configuration set on Caddyfile
 type Config struct {
-	Styles string
-	Flags  []string
+	Styles  string
+	Hugo    bool
+	Args    []string
+	Command string
 }
 
-// ParseHugo parses the configuration file
-func ParseHugo(c *setup.Controller) (*Config, error) {
-	conf := &Config{
-		Styles: "",
-	}
+// ParseCMS parses the configuration file
+func ParseCMS(c *setup.Controller) (*Config, error) {
+	conf := &Config{Hugo: true}
 
 	for c.Next() {
 		for c.NextBlock() {
@@ -30,9 +31,23 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
 				conf.Styles = strings.TrimPrefix(conf.Styles, "/")
 				// Add a beginning slash to make a
 				conf.Styles = "/" + conf.Styles
-			case "flags":
-				conf.Flags = c.RemainingArgs()
-				if len(conf.Flags) == 0 {
+			case "hugo":
+				if !c.NextArg() {
+					return nil, c.ArgErr()
+				}
+				var err error
+				conf.Hugo, err = strconv.ParseBool(c.Val())
+				if err != nil {
+					return conf, err
+				}
+			case "command":
+				if !c.NextArg() {
+					return nil, c.ArgErr()
+				}
+				conf.Command = c.Val()
+			case "args":
+				conf.Args = c.RemainingArgs()
+				if len(conf.Args) == 0 {
 					return conf, c.ArgErr()
 				}
 			}
@@ -44,7 +59,7 @@ func ParseHugo(c *setup.Controller) (*Config, error) {
 }
 
 func (c *Config) parseFlags() {
-	for index, element := range c.Flags {
-		c.Flags[index] = strings.Replace(element, "\"", "", -1)
+	for index, element := range c.Args {
+		c.Args[index] = strings.Replace(element, "\"", "", -1)
 	}
 }
diff --git a/editor/post.go b/editor/post.go
index 6e974982..7b74ecd2 100644
--- a/editor/post.go
+++ b/editor/post.go
@@ -151,7 +151,7 @@ func parseCompleteFile(r *http.Request, c *config.Config, rawFile map[string]int
 				return
 			}
 
-			utils.RunHugo(c)
+			utils.Run(c)
 		})
 		scheduler.Start()
 	}
diff --git a/utils/utils.go b/utils/utils.go
index 6cdb3756..5244e6ec 100644
--- a/utils/utils.go
+++ b/utils/utils.go
@@ -6,6 +6,7 @@ import (
 	"log"
 	"net/http"
 	"os"
+	"os/exec"
 	"reflect"
 	"strings"
 	"text/template"
@@ -158,9 +159,19 @@ func ParseComponents(r *http.Request) []string {
 	return components
 }
 
-// RunHugo is used to run hugo
-func RunHugo(c *config.Config) {
-	commands.HugoCmd.ParseFlags(c.Flags)
+// Run is used to run the static website generator
+func Run(c *config.Config) {
+	if !c.Hugo {
+		out, err := exec.Command(c.Command, c.Args...).Output()
+		if err != nil {
+			log.Panic("Can't execute the commands defined on Caddyfile.")
+			log.Print(out)
+		}
+
+		return
+	}
+
+	commands.HugoCmd.ParseFlags(c.Args)
 	commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0))
 }