add vcs command

This commit is contained in:
Henrique Dias
2016-06-26 18:30:08 +01:00
parent fff716b2b3
commit 0f18f74848
6 changed files with 140 additions and 148 deletions

View File

@@ -15,7 +15,7 @@ type Config struct {
Root http.FileSystem
BaseURL string
StyleSheet string // Costum stylesheet
HugoEnabled bool // This must be only used by Hugo plugin
HugoEnabled bool // Enables the Hugo plugin for File Manager
}
// Parse parses the configuration set by the user so it can

View File

@@ -1,55 +0,0 @@
package git
import (
"bytes"
"encoding/json"
"net/http"
"os/exec"
"strings"
"github.com/hacdias/caddy-filemanager/internal/config"
"github.com/hacdias/caddy-filemanager/internal/page"
)
// Handle handles the POST method on GIT page which is only an API.
func Handle(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
// Check if git is installed on the computer
if _, err := exec.LookPath("git"); err != nil {
return http.StatusNotImplemented, nil
}
// 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)
// Check if command was sent
if _, ok := info["command"]; !ok {
return http.StatusBadRequest, 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 {
return http.StatusBadRequest, nil
}
cmd := exec.Command("git", args...)
cmd.Dir = c.PathScope
output, err := cmd.CombinedOutput()
if err != nil {
return http.StatusInternalServerError, err
}
page := &page.Page{Info: &page.Info{Data: string(output)}}
return page.PrintAsJSON(w)
}

36
internal/vcs/vcs.go Normal file
View File

@@ -0,0 +1,36 @@
package vcs
import (
"net/http"
"os/exec"
"strings"
"github.com/hacdias/caddy-filemanager/internal/config"
"github.com/hacdias/caddy-filemanager/internal/page"
)
// Handle handles the POST method on GIT page which is only an API.
func Handle(w http.ResponseWriter, r *http.Request, c *config.Config) (int, error) {
command := strings.Split(r.Header.Get("command"), " ")
// Check if the command is for git, mercurial or svn
if command[0] != "git" && command[0] != "hg" && command[0] != "svn" {
return http.StatusForbidden, nil
}
// Check if the program is talled is installed on the computer
if _, err := exec.LookPath(command[0]); err != nil {
return http.StatusNotImplemented, nil
}
cmd := exec.Command(command[0], command[1:len(command)]...)
cmd.Dir = c.PathScope
output, err := cmd.CombinedOutput()
if err != nil {
return http.StatusInternalServerError, err
}
page := &page.Page{Info: &page.Info{Data: string(output)}}
return page.PrintAsJSON(w)
}