This commit is contained in:
simon 2020-02-07 19:40:12 -05:00
parent 65be543878
commit d433d81dbe
2 changed files with 11 additions and 6 deletions

4
cli.go
View File

@ -28,7 +28,7 @@ func main() {
app.Usage = "Execute a command on a file after a delay of inactivity" app.Usage = "Execute a command on a file after a delay of inactivity"
app.Email = "me@simon987.net" app.Email = "me@simon987.net"
app.Author = "simon987" app.Author = "simon987"
app.Version = "1.1" app.Version = "1.2"
var cmdString string var cmdString string
var watchDir string var watchDir string
@ -85,7 +85,7 @@ func main() {
fileMap: &sync.Map{}, fileMap: &sync.Map{},
beemChan: make(chan string, transfers), beemChan: make(chan string, transfers),
tarChan: make(chan string, 100), tarChan: make(chan string, 100),
beemCommand: parseCommand(cmdString), beemCommand: parseCommand(watchDir, cmdString),
inactiveDelay: inactiveDelay, inactiveDelay: inactiveDelay,
beemWg: &sync.WaitGroup{}, beemWg: &sync.WaitGroup{},
tarWg: &sync.WaitGroup{}, tarWg: &sync.WaitGroup{},

13
util.go
View File

@ -82,8 +82,9 @@ func copyFile(src string, dst string) error {
return out.Close() return out.Close()
} }
func parseCommand(command string) func(string, string) (string, []string) { func parseCommand(watchDir, command string) func(string, string) (string, []string) {
args, _ := argv.Argv([]rune(command), argv.ParseEnv(os.Environ()), argv.Run) args, _ := argv.Argv([]rune(command), argv.ParseEnv(os.Environ()), argv.Run)
absWatchDir, _ := filepath.Abs(watchDir)
logrus.WithField("cmd", args[0]).Info("Parsed beem command") logrus.WithField("cmd", args[0]).Info("Parsed beem command")
@ -93,7 +94,11 @@ func parseCommand(command string) func(string, string) (string, []string) {
for i := range newTokens { for i := range newTokens {
newTokens[i] = strings.Replace(newTokens[i], "%file", name, -1) newTokens[i] = strings.Replace(newTokens[i], "%file", name, -1)
newTokens[i] = strings.Replace(newTokens[i], "%dir", dir, -1) d, _ := filepath.Rel(absWatchDir, dir)
if d == "." {
d = ""
}
newTokens[i] = strings.Replace(newTokens[i], "%dir", d, -1)
newTokens[i] = strings.Replace(newTokens[i], "%name", filepath.Base(name), -1) newTokens[i] = strings.Replace(newTokens[i], "%name", filepath.Base(name), -1)
} }
return args[0][0], newTokens[1:] return args[0][0], newTokens[1:]
@ -118,9 +123,9 @@ func (b *Beemer) dispose() {
}) })
go func() { go func() {
time.Sleep(15 * time.Second) time.Sleep(30 * time.Second)
close(b.beemChan)
logrus.WithField("chanLen", len(b.beemChan)).Info("Forcefully closed beem channel") logrus.WithField("chanLen", len(b.beemChan)).Info("Forcefully closed beem channel")
close(b.beemChan)
}() }()
logrus.WithField("chanLen", len(b.beemChan)).Info("Waiting for beem queue to drain...") logrus.WithField("chanLen", len(b.beemChan)).Info("Waiting for beem queue to drain...")