1
0
Fork 0
go-asink/asink.go

69 lines
1.3 KiB
Go
Raw Normal View History

2013-02-10 23:39:23 -05:00
package main
import (
2013-02-11 23:17:12 -05:00
"code.google.com/p/goconf/conf"
2013-02-10 23:39:23 -05:00
"flag"
2013-02-11 23:17:12 -05:00
"fmt"
2013-02-10 23:39:23 -05:00
"os/user"
2013-02-11 23:17:12 -05:00
"path"
2013-02-10 23:39:23 -05:00
)
var configFileName string
func init() {
const config_usage = "Config File to use"
userHomeDir := "~"
u, err := user.Current()
if err == nil {
userHomeDir = u.HomeDir
2013-02-11 23:17:12 -05:00
}
2013-02-10 23:39:23 -05:00
flag.StringVar(&configFileName, "config", path.Join(userHomeDir, ".asink", "config"), config_usage)
flag.StringVar(&configFileName, "c", path.Join(userHomeDir, ".asink", "config"), config_usage+" (shorthand)")
}
func main() {
flag.Parse()
fmt.Println("config file:", configFileName)
config, err := conf.ReadConfigFile(configFileName)
if err != nil {
fmt.Println(err)
fmt.Println("Error reading config file at ", configFileName, ". Does it exist?")
return
}
storage, err := GetStorage(config)
if err != nil {
fmt.Println(err)
return
}
syncdir, err := config.GetString("local", "syncdir")
cachedir, err := config.GetString("local", "cachedir")
fmt.Println(syncdir)
fmt.Println(cachedir)
fmt.Println(storage)
2013-02-11 23:16:19 -05:00
fileUpdates := make(chan *Event)
go StartWatching(syncdir, fileUpdates)
2013-02-10 23:39:23 -05:00
2013-02-11 23:16:19 -05:00
for {
2013-02-11 23:17:12 -05:00
event := <-fileUpdates
2013-02-11 23:16:19 -05:00
ProcessEvent(storage, event)
2013-02-10 23:39:23 -05:00
}
2013-02-11 23:16:19 -05:00
}
2013-02-10 23:39:23 -05:00
2013-02-11 23:16:19 -05:00
func ProcessEvent(storage Storage, event *Event) {
fmt.Println(event)
2013-02-10 23:39:23 -05:00
2013-02-11 23:16:19 -05:00
if event.IsUpdate() {
err := storage.Put(event.Path, event.Hash)
2013-02-11 23:17:12 -05:00
if err != nil {
panic(err)
}
2013-02-10 23:39:23 -05:00
}
}