package main import ( "fmt" "os" "path/filepath" "time" pb "go.wit.com/lib/protobuf/virtbuf" "go.wit.com/log" ) func readConfigFile() { me.cluster = new(pb.Cluster) homeDir, _ := os.UserHomeDir() fullname := filepath.Join(homeDir, ".config/virtigo.json") pfile, err := os.ReadFile(fullname) if err != nil { log.Info("open config file :", err) return } err = me.cluster.UnmarshalJSON(pfile) if err != nil { log.Info("read json failed", err) os.Exit(-1) return } // initialize each hypervisor for _, pbh := range me.cluster.Hypervisors { h := findHypervisor(pbh.Hostname) if h != nil { continue } // this is a new unknown droplet (not in the config file) h = new(HyperT) h.pb = pbh h.Delay = 5 * time.Second h.lastpoll = time.Now() h.Scan = func() { h.pollHypervisor() } me.hypers = append(me.hypers, h) log.Log(EVENT, "config new hypervisors", h.pb.Hostname) } // initialize values for each droplet for _, pbd := range me.cluster.Droplets { d := findDroplet(pbd.Hostname) if d != nil { continue } // this is a new unknown droplet (not in the config file) d = new(DropletT) d.pb = pbd me.droplets = append(me.droplets, d) log.Log(EVENT, "config new droplet", d.pb.Hostname, d.pb.StartState, d.pb.PreferredHypervisor) } } func writeConfigFile() { homeDir, _ := os.UserHomeDir() fullname := filepath.Join(homeDir, ".config/virtigo.json") cfgfile, err := os.OpenFile(fullname, os.O_RDWR|os.O_CREATE, 0666) defer cfgfile.Close() if err != nil { log.Info("open config file :", err) return } json := me.cluster.FormatJSON() fmt.Fprintln(cfgfile, json) }