Add the ability to do everything in Go

This commit is contained in:
Tony Blyler 2016-05-13 17:38:53 -04:00
parent d088e413b7
commit a6dfb7e698
No known key found for this signature in database
GPG key ID: 25C9D3A655D1A65C
6 changed files with 476 additions and 258 deletions
cmd/hoarder

57
cmd/hoarder/main.go Normal file
View file

@ -0,0 +1,57 @@
package main
import (
"encoding/json"
"flag"
"github.com/tblyler/hoarder/queue"
"io/ioutil"
"log"
"os"
"os/signal"
)
func main() {
configPath := flag.String("config", "", "path to the config file")
flag.Parse()
logger := log.New(os.Stdout, "hoarder", log.LstdFlags)
if *configPath == "" {
logger.Println("Missing config path")
os.Exit(1)
}
configRaw, err := ioutil.ReadFile(*configPath)
if err != nil {
logger.Printf("Failed to read config file '%s': '%s'", *configPath, err)
os.Exit(1)
}
config := &queue.Config{}
err = json.Unmarshal(configRaw, config)
if err != nil {
logger.Printf("Unable to decode config json at '%s': '%s'", *configPath, err)
os.Exit(1)
}
q, err := queue.NewQueue(config, logger)
if err != nil {
logger.Printf("Failed to start hoarder: '%s'", err)
os.Exit(1)
}
stop := make(chan bool)
done := make(chan bool)
go func() {
q.Run(stop)
done <- true
}()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
sig := <-sigChan
logger.Println("Got signal ", sig, " quitting")
stop <- true
<-done
}