rpcfetch/fetch/main.go
Ophestra Umiker 2c859c1a46
rpcfetch: add template program
Since documentation does not yet exist a template program is added showcasing the intended usage of this library. A configuration interface will be added in a future commit.

Signed-off-by: Ophestra Umiker <cat@ophivana.moe>
2024-06-20 22:19:38 +09:00

64 lines
1.1 KiB
Go

package main
import (
"encoding/gob"
"flag"
"fmt"
"log"
"os"
"time"
"git.ophivana.moe/cat/rpcfetch"
)
var (
d *rpcfetch.Client
conf config
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()
}
}