From aa8620c52c496908cab2cbd0f0dde46ef48dbc53 Mon Sep 17 00:00:00 2001 From: Henrique Dias Date: Wed, 30 Sep 2015 22:03:28 +0100 Subject: [PATCH] progresses on #23 --- browse/browse.go | 4 ++-- browse/delete.go | 16 ++++++++++------ browse/get.go | 2 +- browse/post.go | 5 +++-- config/config.go | 40 +++++++++++++++++++++++----------------- editor/editor.go | 1 + editor/get.go | 2 +- utils/utils.go | 30 +++++++++++++++++++++++++++--- 8 files changed, 68 insertions(+), 32 deletions(-) diff --git a/browse/browse.go b/browse/browse.go index 3c33e017..8527d6c5 100644 --- a/browse/browse.go +++ b/browse/browse.go @@ -15,9 +15,9 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e switch r.Method { case "DELETE": - return DELETE(w, r) + return DELETE(w, r, c) case "POST": - return POST(w, r) + return POST(w, r, c) case "GET": return GET(w, r, c) default: diff --git a/browse/delete.go b/browse/delete.go index 39febcb1..2da7a19a 100644 --- a/browse/delete.go +++ b/browse/delete.go @@ -4,22 +4,26 @@ import ( "net/http" "os" "strings" + + "github.com/hacdias/caddy-cms/config" ) // DELETE handles the DELETE method on browse page -func DELETE(w http.ResponseWriter, r *http.Request) (int, error) { +func DELETE(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { // Remove both beginning and trailing slashes - r.URL.Path = strings.TrimPrefix(r.URL.Path, "/") - r.URL.Path = strings.TrimSuffix(r.URL.Path, "/") + path := r.URL.Path + path = strings.TrimPrefix(path, "/") + path = strings.TrimSuffix(path, "/") + path = c.Path + path // Check if the file or directory exists - if stat, err := os.Stat(r.URL.Path); err == nil { + if stat, err := os.Stat(path); err == nil { var err error // If it's dir, remove all of the content inside if stat.IsDir() { - err = os.RemoveAll(r.URL.Path) + err = os.RemoveAll(path) } else { - err = os.Remove(r.URL.Path) + err = os.Remove(path) } // Check for errors diff --git a/browse/get.go b/browse/get.go index 82cce6d7..7006d512 100644 --- a/browse/get.go +++ b/browse/get.go @@ -27,7 +27,7 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) { return 404, nil }), - Root: "./", + Root: c.Path, Configs: []browse.Config{ { PathScope: "/", diff --git a/browse/post.go b/browse/post.go index 32c6eee8..b0f09c7f 100644 --- a/browse/post.go +++ b/browse/post.go @@ -10,11 +10,12 @@ import ( "os" "strings" + "github.com/hacdias/caddy-cms/config" "github.com/hacdias/caddy-cms/utils" ) // POST handles the POST method on browse page -func POST(w http.ResponseWriter, r *http.Request) (int, error) { +func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { // Remove both beginning slashes r.URL.Path = strings.TrimPrefix(r.URL.Path, "/") @@ -45,7 +46,7 @@ func POST(w http.ResponseWriter, r *http.Request) (int, error) { filename := info["filename"].(string) filename = strings.TrimPrefix(filename, "/") filename = strings.TrimSuffix(filename, "/") - filename = r.URL.Path + filename + filename = c.Path + r.URL.Path + filename // Check if the archetype is defined if info["archetype"] != "" { diff --git a/config/config.go b/config/config.go index a7a8b37b..cb115cb1 100644 --- a/config/config.go +++ b/config/config.go @@ -8,17 +8,33 @@ import ( // Config is the add-on configuration set on Caddyfile type Config struct { - Styles string - Args []string - Command string + Public string Content string + Path string + Styles string + Command string + Hugo bool } // ParseCMS parses the configuration file func ParseCMS(c *setup.Controller) (*Config, error) { - conf := &Config{Content: "content"} + conf := &Config{ + Public: strings.Replace(c.Root, "./", "", -1), + Content: "content", + Hugo: true, + Path: "./", + } for c.Next() { + args := c.RemainingArgs() + + switch len(args) { + case 1: + conf.Path = args[0] + conf.Path = strings.TrimSuffix(conf.Path, "/") + conf.Path += "/" + } + for c.NextBlock() { switch c.Val() { case "styles": @@ -35,28 +51,18 @@ func ParseCMS(c *setup.Controller) (*Config, error) { return nil, c.ArgErr() } conf.Content = c.Val() - conf.Content = strings.TrimPrefix(conf.Content, "/") - conf.Content = strings.TrimSuffix(conf.Content, "/") 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() + + if conf.Command != "" && !strings.HasPrefix(conf.Command, "-") { + conf.Hugo = false } } } } - conf.parseArgs() return conf, nil } - -func (c *Config) parseArgs() { - for index, element := range c.Args { - c.Args[index] = strings.Replace(element, "\"", "", -1) - } -} diff --git a/editor/editor.go b/editor/editor.go index 3fb4d781..40ad2e61 100644 --- a/editor/editor.go +++ b/editor/editor.go @@ -10,6 +10,7 @@ import ( // ServeHTTP serves the editor page func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) { filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1) + filename = c.Path + filename switch r.Method { case "POST": diff --git a/editor/get.go b/editor/get.go index 06bd90c6..22159a48 100644 --- a/editor/get.go +++ b/editor/get.go @@ -48,7 +48,7 @@ func GET(w http.ResponseWriter, r *http.Request, c *config.Config, filename stri // Create a new editor variable and set the extension page := new(editor) page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".") - page.Name = filename + page.Name = strings.Replace(filename, c.Path, "", 1) page.Config = c page.IsPost = false diff --git a/utils/utils.go b/utils/utils.go index d4af6aca..0f6876a1 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -162,8 +162,20 @@ func ParseComponents(r *http.Request) []string { // Run is used to run the static website generator func Run(c *config.Config) { - if c.Command != "" { - out, err := exec.Command(c.Command, c.Args...).Output() + 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.") + } + + if !c.Hugo { + out, err := exec.Command(c.Command).Output() fmt.Print(string(out)) if err != nil { log.Panic("Can't execute the commands defined on Caddyfile.") @@ -172,8 +184,20 @@ func Run(c *config.Config) { return } - commands.HugoCmd.ParseFlags(c.Args) + args := strings.Split(c.Command, " ") + + for index, element := range args { + args[index] = strings.Replace(element, "\"", "", -1) + } + + commands.HugoCmd.ParseFlags(args) commands.HugoCmd.Run(commands.HugoCmd, make([]string, 0)) + + err = os.Chdir(cwd) + + if err != nil { + log.Print("Can't get working directory.") + } } // SplitCapitalize splits a string by its uppercase letters and capitalize the