Created
February 17, 2018 08:10
-
-
Save akiyoshi83/c6696c3d57eccdb102a726b378f52bbd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"log" | |
"os" | |
"os/signal" | |
"syscall" | |
"github.com/go-fsnotify/fsnotify" | |
"github.com/takama/daemon" | |
) | |
const ( | |
name = "gosync" | |
description = "go file sync service" | |
) | |
var dependencies = []string{} | |
var stdlog, errlog *log.Logger | |
type Service struct { | |
daemon.Daemon | |
} | |
func (service *Service) Manage() (string, error) { | |
usage := "Usage: gosync install | remove | start | stop | status" | |
if len(os.Args) > 1 { | |
command := os.Args[1] | |
switch command { | |
case "install": | |
return service.Install() | |
case "remove": | |
return service.Remove() | |
case "start": | |
return service.Start() | |
case "stop": | |
return service.Stop() | |
case "status": | |
return service.Status() | |
default: | |
return usage, nil | |
} | |
} | |
interrupt := make(chan os.Signal, 1) | |
signal.Notify(interrupt, os.Interrupt, os.Kill, syscall.SIGTERM) | |
sync() | |
for { | |
select { | |
case killSignal := <-interrupt: | |
stdlog.Println("Got signal:", killSignal) | |
if killSignal == os.Interrupt { | |
return "Daemon was interruped by system signal", nil | |
} | |
return "Daemon was killed", nil | |
} | |
} | |
return usage, nil | |
} | |
func sync() { | |
src := "./test/src" | |
//dest := "./test/dest" | |
watcher, err := fsnotify.NewWatcher() | |
if err != nil { | |
errlog.Fatal(err) | |
} | |
defer watcher.Close() | |
done := make(chan bool) | |
go func() { | |
for { | |
select { | |
case event := <-watcher.Events: | |
stdlog.Println("event: ", event) | |
switch { | |
// TODO | |
case event.Op&fsnotify.Write == fsnotify.Write: | |
stdlog.Println("Modified file: ", event.Name) | |
case event.Op&fsnotify.Create == fsnotify.Create: | |
stdlog.Println("Created file: ", event.Name) | |
case event.Op&fsnotify.Remove == fsnotify.Remove: | |
stdlog.Println("Removed file: ", event.Name) | |
case event.Op&fsnotify.Rename == fsnotify.Rename: | |
stdlog.Println("Renamed file: ", event.Name) | |
case event.Op&fsnotify.Chmod == fsnotify.Chmod: | |
stdlog.Println("File changed permission: ", event.Name) | |
} | |
case err := <-watcher.Errors: | |
errlog.Println("error: ", err) | |
done <- true | |
} | |
} | |
}() | |
err = watcher.Add(src) | |
if err != nil { | |
errlog.Fatal(err) | |
} | |
<-done | |
} | |
func init() { | |
stdlog = log.New(os.Stdout, "", log.Ldate|log.Ltime) | |
errlog = log.New(os.Stderr, "", log.Ldate|log.Ltime) | |
} | |
func main() { | |
srv, err := daemon.New(name, description, dependencies...) | |
if err != nil { | |
errlog.Println("Error: ", err) | |
os.Exit(1) | |
} | |
service := &Service{srv} | |
status, err := service.Manage() | |
if err != nil { | |
errlog.Println(status, "\nError: ", err) | |
os.Exit(1) | |
} | |
fmt.Println(status) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment