progresses on #49

This commit is contained in:
Henrique Dias 2016-03-06 14:23:37 +00:00
parent bec1c3c510
commit b97ef80596
7 changed files with 78 additions and 12 deletions

File diff suppressed because one or more lines are too long

View File

@ -312,14 +312,23 @@ $(document).on('page:browse', function() {
}));
request.onreadystatechange = function() {
if (request.readyState == 4) {
var data = JSON.parse(request.responseText);
var type = "success"
var timeout = 5000
if (request.status == 200) {
} else {
if (data.success == "false") {
type = "error"
timeout = false
}
notification({
text: 'Something went wrong.',
type: 'error'
text: data.message,
type: type,
timeout: timeout
});
console.log(request.responseText);
} else {
console.log(request.responseText)
}
}
}

View File

@ -25,6 +25,6 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e
case "PUT":
return PUT(w, r, c)
default:
return 400, errors.New("Invalid method.")
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
}
}

View File

@ -19,6 +19,6 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e
case "GET":
return GET(w, r, c, filename)
default:
return 400, errors.New("Invalid method.")
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
}
}

View File

@ -13,6 +13,6 @@ func ServeHTTP(w http.ResponseWriter, r *http.Request, c *config.Config) (int, e
case "POST":
return POST(w, r, c)
default:
return 400, errors.New("Invalid method.")
return http.StatusMethodNotAllowed, errors.New("Invalid method.")
}
}

View File

@ -1,15 +1,72 @@
package git
import (
"bytes"
"encoding/json"
"net/http"
"os/exec"
"strings"
"github.com/hacdias/caddy-hugo/config"
)
// POST handles the POST method on GIT page which is only an API.
func POST(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
if _, err := exec.LookPath("git"); err != nil {
jsonMessage(w, "Git is not installed on your computer.", false)
return 200, nil
}
w.Header().Set("Content-Type", "application/json")
w.Write([]byte("{}"))
// Get the JSON information sent using a buffer
buff := new(bytes.Buffer)
buff.ReadFrom(r.Body)
// Creates the raw file "map" using the JSON
var info map[string]interface{}
json.Unmarshal(buff.Bytes(), &info)
if _, ok := info["command"]; !ok {
jsonMessage(w, "Command not specified.", false)
return 200, nil
}
command := info["command"].(string)
args := strings.Split(command, " ")
if len(args) > 0 && args[0] == "git" {
args = append(args[:0], args[1:]...)
}
if len(args) == 0 {
jsonMessage(w, "Command not specified.", false)
return 200, nil
}
cmd := exec.Command("git", args...)
cmd.Dir = c.Path
output, err := cmd.CombinedOutput()
if err != nil {
jsonMessage(w, err.Error(), false)
return 200, nil
}
jsonMessage(w, string(output), true)
return http.StatusOK, nil
}
type jsonMSG struct {
Message string `json:"message"`
Success bool `json:"success"`
}
func jsonMessage(w http.ResponseWriter, message string, success bool) {
msg := &jsonMSG{
Message: message,
Success: success,
}
m, _ := json.Marshal(msg)
w.Header().Set("Content-Type", "application/json")
w.Write(m)
}

View File

@ -137,7 +137,7 @@ func (h CaddyHugo) ServeHTTP(w http.ResponseWriter, r *http.Request) (int, error
}
// Git API
if page == "edit" {
if page == "git" {
code, err = git.ServeHTTP(w, r, h.Config)
}