rpcfetch/fetch/main.go

63 lines
1.1 KiB
Go

package main
import (
"encoding/gob"
"flag"
"fmt"
"log"
"os"
"time"
"git.ophivana.moe/cat/rpcfetch"
)
var (
d *rpcfetch.Client
confPath string
)
func init() {
flag.StringVar(&confPath, "conf", "rpcfetch.conf", "path to rpcfetch configuration file")
}
func main() {
flag.Parse()
if cf, err := os.Open(confPath); err != nil {
if !os.IsNotExist(err) {
fmt.Printf("error opening configuration file: %s\n", err)
os.Exit(1)
}
// use defaults
log.Print("configuration file does not exist, using defaults")
conf = &defaultConfig
} else {
// decode from existing file
if err = gob.NewDecoder(cf).Decode(&conf); err != nil {
fmt.Printf("error reading configuration: %s\n", err)
os.Exit(1)
}
if err = cf.Close(); err != nil {
log.Fatalf("error closing configuration file: %s", err)
}
}
d = rpcfetch.New(conf.ID)
defer func() {
if err := d.Close(); err != nil {
log.Printf("error closing client: %s", err)
}
}()
// restore activity from configuration
apply()
// update periodically
for {
time.Sleep(5 * time.Second)
apply()
}
}