Go code updates

This commit is contained in:
Henrique Dias
2017-07-08 17:51:47 +01:00
parent 18414dbc5e
commit 1a4242ca06
8 changed files with 728 additions and 599 deletions

View File

@@ -2,12 +2,16 @@ package filemanager
import (
"errors"
"log"
"net/http"
"os"
"os/exec"
"regexp"
"strings"
rice "github.com/GeertJohan/go.rice"
"github.com/asdine/storm"
"github.com/mholt/caddy"
"golang.org/x/net/webdav"
)
@@ -41,6 +45,9 @@ type FileManager struct {
// Users is a map with the different configurations for each user.
Users map[string]*User
// A map of events to a slice of commands.
Commands map[string][]string
// The plugins that have been plugged in.
Plugins []*Plugin
}
@@ -136,6 +143,10 @@ func New(database string, base User) (*FileManager, error) {
assets: rice.MustFindBox("./assets/dist"),
}
m.Commands = map[string][]string{
"before_save": []string{"cmd /c \"echo %file%\""},
}
// Tries to open a database on the location provided. This
// function will automatically create a new one if it doesn't
// exist.
@@ -273,3 +284,44 @@ func (r *Regexp) MatchString(s string) bool {
return r.regexp.MatchString(s)
}
// Runner ...
func (m FileManager) Runner(event string, path string) error {
for _, command := range m.Commands[event] {
args := strings.Split(command, " ")
nonblock := false
if len(args) > 1 && args[len(args)-1] == "&" {
// Run command in background; non-blocking
nonblock = true
args = args[:len(args)-1]
}
command, args, err := caddy.SplitCommandAndArgs(strings.Join(args, " "))
if err != nil {
return err
}
cmd := exec.Command(command, args...)
cmd.Env = append(os.Environ(), "file="+path)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
if nonblock {
log.Printf("[INFO] Nonblocking Command:\"%s %s\"", command, strings.Join(args, " "))
if err := cmd.Start(); err != nil {
return err
}
continue
}
log.Printf("[INFO] Blocking Command:\"%s %s\"", command, strings.Join(args, " "))
if err := cmd.Run(); err != nil {
return err
}
}
return nil
}