filebrowser/config/commands.go

63 lines
1.3 KiB
Go
Raw Normal View History

2016-12-18 12:34:31 +00:00
package config
import (
"log"
"net/http"
"os"
"os/exec"
"path/filepath"
"strings"
"github.com/mholt/caddy"
)
2016-12-18 12:36:36 +00:00
// CommandFunc ...
type CommandFunc func(r *http.Request, c *Config, u *User) error
2016-12-18 12:34:31 +00:00
// CommandRunner ...
2016-12-18 12:36:36 +00:00
func CommandRunner(c *caddy.Controller) (CommandFunc, error) {
2016-12-18 12:34:31 +00:00
fn := func(r *http.Request, c *Config, u *User) error { return nil }
args := c.RemainingArgs()
if len(args) == 0 {
return fn, c.ArgErr()
}
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 fn, c.Err(err.Error())
}
fn = func(r *http.Request, c *Config, u *User) error {
2017-04-15 13:00:41 +00:00
path := strings.Replace(r.URL.Path, c.WebDavURL, "", 1)
2016-12-18 12:34:31 +00:00
path = u.Scope + "/" + path
path = filepath.Clean(path)
for i := range args {
args[i] = strings.Replace(args[i], "{path}", path, -1)
}
cmd := exec.Command(command, args...)
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, " "))
return cmd.Start()
}
log.Printf("[INFO] Blocking Command:\"%s %s\"", command, strings.Join(args, " "))
return cmd.Run()
}
return fn, nil
}