2016-05-13 17:38:53 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2016-05-16 20:12:26 -04:00
|
|
|
"fmt"
|
2016-06-16 10:08:56 -04:00
|
|
|
"github.com/go-yaml/yaml"
|
2016-05-13 17:38:53 -04:00
|
|
|
"github.com/tblyler/hoarder/queue"
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
2016-05-16 22:53:08 -04:00
|
|
|
"strconv"
|
|
|
|
"time"
|
2016-05-13 17:38:53 -04:00
|
|
|
)
|
|
|
|
|
2016-05-16 20:12:26 -04:00
|
|
|
var buildVersion = "Unknown"
|
|
|
|
var buildDate = "Unknown"
|
|
|
|
|
2016-05-13 17:38:53 -04:00
|
|
|
func main() {
|
2016-05-16 20:12:26 -04:00
|
|
|
version := flag.Bool("version", false, "display version info")
|
2016-05-13 17:38:53 -04:00
|
|
|
configPath := flag.String("config", "", "path to the config file")
|
|
|
|
flag.Parse()
|
|
|
|
|
2016-05-16 20:12:26 -04:00
|
|
|
if *version {
|
2016-05-16 22:53:08 -04:00
|
|
|
dateUnix, err := strconv.ParseInt(buildDate, 10, 64)
|
|
|
|
if err == nil {
|
|
|
|
date := time.Unix(dateUnix, 0)
|
|
|
|
if !date.IsZero() {
|
|
|
|
buildDate = date.UTC().Format(time.UnixDate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-16 20:12:26 -04:00
|
|
|
fmt.Printf("%s\n%s\n", buildVersion, buildDate)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2016-05-13 17:42:37 -04:00
|
|
|
logger := log.New(os.Stdout, "hoarder ", log.LstdFlags)
|
2016-05-13 17:38:53 -04:00
|
|
|
|
|
|
|
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{}
|
2016-05-14 10:22:10 -04:00
|
|
|
err = yaml.Unmarshal(configRaw, config)
|
2016-05-13 17:38:53 -04:00
|
|
|
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
|
|
|
|
}
|