beemer/cli.go
2019-07-16 21:39:27 -04:00

97 lines
1.9 KiB
Go
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package main
import (
"errors"
"github.com/fsnotify/fsnotify"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
"os"
"time"
)
func globalInit() {
logrus.SetLevel(logrus.TraceLevel)
}
func main() {
globalInit()
app := cli.NewApp()
app.Name = "work"
app.Usage = "Execute a command on a file after a delay of inactivity"
app.Email = "me@simon987.net"
app.Author = "simon987"
app.Version = "1.1"
var cmdString string
var watchDir string
var transfers int
var inactiveDelay time.Duration
app.Flags = []cli.Flag{
cli.IntFlag{
Name: "transfers, t",
Usage: "Number of simultaneous transfers",
Destination: &transfers,
Value: 10,
},
cli.StringFlag{
Name: "command, c",
Usage: "Will be executed on file write. You can use %file, %name and %dir. " +
"Example: \"rclone move %file remote:/beem/%dir\"",
Destination: &cmdString,
},
cli.DurationFlag{
Name: "wait, w",
Usage: "Files will be beemed after `DELAY` of inactivity",
Destination: &inactiveDelay,
Value: time.Second * 10,
},
cli.StringFlag{
Name: "directory, d",
Usage: "`DIRECTORY` to watch.",
Destination: &watchDir,
},
}
app.Action = func(c *cli.Context) error {
if !c.IsSet("directory") {
return errors.New("directory must be specified")
}
beemer := Beemer{
fileMap: make(map[string]*File, 0),
beemChan: make(chan string, transfers),
beemCommand: parseCommand(cmdString),
inactiveDelay: inactiveDelay,
}
beemer.initTempDir()
beemer.watcher, _ = fsnotify.NewWatcher()
defer beemer.dispose()
go beemer.handleWatcherEvents()
beemer.initWatchDir(watchDir)
for i := 0; i < transfers; i++ {
go beemer.work()
}
//TODO gracefully handle SIGINT
done := make(chan bool)
<-done
return nil
}
err := app.Run(os.Args)
if err != nil {
logrus.Fatal(app.OnUsageError)
}
}