progresses on #23
parent
c44dba20f1
commit
aa8620c52c
|
@ -15,9 +15,9 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "DELETE":
|
case "DELETE":
|
||||||
return DELETE(w, r)
|
return DELETE(w, r, c)
|
||||||
case "POST":
|
case "POST":
|
||||||
return POST(w, r)
|
return POST(w, r, c)
|
||||||
case "GET":
|
case "GET":
|
||||||
return GET(w, r, c)
|
return GET(w, r, c)
|
||||||
default:
|
default:
|
||||||
|
|
|
@ -4,22 +4,26 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hacdias/caddy-cms/config"
|
||||||
)
|
)
|
||||||
|
|
||||||
// DELETE handles the DELETE method on browse page
|
// 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
|
// Remove both beginning and trailing slashes
|
||||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
|
path := r.URL.Path
|
||||||
r.URL.Path = strings.TrimSuffix(r.URL.Path, "/")
|
path = strings.TrimPrefix(path, "/")
|
||||||
|
path = strings.TrimSuffix(path, "/")
|
||||||
|
path = c.Path + path
|
||||||
|
|
||||||
// Check if the file or directory exists
|
// 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
|
var err error
|
||||||
// If it's dir, remove all of the content inside
|
// If it's dir, remove all of the content inside
|
||||||
if stat.IsDir() {
|
if stat.IsDir() {
|
||||||
err = os.RemoveAll(r.URL.Path)
|
err = os.RemoveAll(path)
|
||||||
} else {
|
} else {
|
||||||
err = os.Remove(r.URL.Path)
|
err = os.Remove(path)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for errors
|
// Check for errors
|
||||||
|
|
|
@ -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) {
|
Next: middleware.HandlerFunc(func(w http.ResponseWriter, r *http.Request) (int, error) {
|
||||||
return 404, nil
|
return 404, nil
|
||||||
}),
|
}),
|
||||||
Root: "./",
|
Root: c.Path,
|
||||||
Configs: []browse.Config{
|
Configs: []browse.Config{
|
||||||
{
|
{
|
||||||
PathScope: "/",
|
PathScope: "/",
|
||||||
|
|
|
@ -10,11 +10,12 @@ import (
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/hacdias/caddy-cms/config"
|
||||||
"github.com/hacdias/caddy-cms/utils"
|
"github.com/hacdias/caddy-cms/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// POST handles the POST method on browse page
|
// 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
|
// Remove both beginning slashes
|
||||||
r.URL.Path = strings.TrimPrefix(r.URL.Path, "/")
|
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 := info["filename"].(string)
|
||||||
filename = strings.TrimPrefix(filename, "/")
|
filename = strings.TrimPrefix(filename, "/")
|
||||||
filename = strings.TrimSuffix(filename, "/")
|
filename = strings.TrimSuffix(filename, "/")
|
||||||
filename = r.URL.Path + filename
|
filename = c.Path + r.URL.Path + filename
|
||||||
|
|
||||||
// Check if the archetype is defined
|
// Check if the archetype is defined
|
||||||
if info["archetype"] != "" {
|
if info["archetype"] != "" {
|
||||||
|
|
|
@ -8,17 +8,33 @@ import (
|
||||||
|
|
||||||
// Config is the add-on configuration set on Caddyfile
|
// Config is the add-on configuration set on Caddyfile
|
||||||
type Config struct {
|
type Config struct {
|
||||||
Styles string
|
Public string
|
||||||
Args []string
|
|
||||||
Command string
|
|
||||||
Content string
|
Content string
|
||||||
|
Path string
|
||||||
|
Styles string
|
||||||
|
Command string
|
||||||
|
Hugo bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseCMS parses the configuration file
|
// ParseCMS parses the configuration file
|
||||||
func ParseCMS(c *setup.Controller) (*Config, error) {
|
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() {
|
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() {
|
for c.NextBlock() {
|
||||||
switch c.Val() {
|
switch c.Val() {
|
||||||
case "styles":
|
case "styles":
|
||||||
|
@ -35,28 +51,18 @@ func ParseCMS(c *setup.Controller) (*Config, error) {
|
||||||
return nil, c.ArgErr()
|
return nil, c.ArgErr()
|
||||||
}
|
}
|
||||||
conf.Content = c.Val()
|
conf.Content = c.Val()
|
||||||
conf.Content = strings.TrimPrefix(conf.Content, "/")
|
|
||||||
conf.Content = strings.TrimSuffix(conf.Content, "/")
|
|
||||||
case "command":
|
case "command":
|
||||||
if !c.NextArg() {
|
if !c.NextArg() {
|
||||||
return nil, c.ArgErr()
|
return nil, c.ArgErr()
|
||||||
}
|
}
|
||||||
conf.Command = c.Val()
|
conf.Command = c.Val()
|
||||||
case "args":
|
|
||||||
conf.Args = c.RemainingArgs()
|
if conf.Command != "" && !strings.HasPrefix(conf.Command, "-") {
|
||||||
if len(conf.Args) == 0 {
|
conf.Hugo = false
|
||||||
return conf, c.ArgErr()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
conf.parseArgs()
|
|
||||||
return conf, nil
|
return conf, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Config) parseArgs() {
|
|
||||||
for index, element := range c.Args {
|
|
||||||
c.Args[index] = strings.Replace(element, "\"", "", -1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
// ServeHTTP serves the editor page
|
// ServeHTTP serves the editor page
|
||||||
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
|
||||||
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
filename := strings.Replace(r.URL.Path, "/admin/edit/", "", 1)
|
||||||
|
filename = c.Path + filename
|
||||||
|
|
||||||
switch r.Method {
|
switch r.Method {
|
||||||
case "POST":
|
case "POST":
|
||||||
|
|
|
@ -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
|
// Create a new editor variable and set the extension
|
||||||
page := new(editor)
|
page := new(editor)
|
||||||
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
|
page.Mode = strings.TrimPrefix(filepath.Ext(filename), ".")
|
||||||
page.Name = filename
|
page.Name = strings.Replace(filename, c.Path, "", 1)
|
||||||
page.Config = c
|
page.Config = c
|
||||||
page.IsPost = false
|
page.IsPost = false
|
||||||
|
|
||||||
|
|
|
@ -162,8 +162,20 @@ func ParseComponents(r *http.Request) []string {
|
||||||
|
|
||||||
// Run is used to run the static website generator
|
// Run is used to run the static website generator
|
||||||
func Run(c *config.Config) {
|
func Run(c *config.Config) {
|
||||||
if c.Command != "" {
|
cwd, err := os.Getwd()
|
||||||
out, err := exec.Command(c.Command, c.Args...).Output()
|
|
||||||
|
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))
|
fmt.Print(string(out))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Panic("Can't execute the commands defined on Caddyfile.")
|
log.Panic("Can't execute the commands defined on Caddyfile.")
|
||||||
|
@ -172,8 +184,20 @@ func Run(c *config.Config) {
|
||||||
return
|
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))
|
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
|
// SplitCapitalize splits a string by its uppercase letters and capitalize the
|
||||||
|
|
Loading…
Reference in New Issue