testing ConfigSave() and load
This commit is contained in:
parent
66000419bf
commit
2495995e6e
28
config.go
28
config.go
|
@ -23,6 +23,34 @@ func (m *Portmaps) ConfigSave() error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func ConfigLoad() *Portmaps {
|
||||
if os.Getenv("CLOUD_HOME") == "" {
|
||||
homeDir, _ := os.UserHomeDir()
|
||||
fullpath := filepath.Join(homeDir, ".config/cloud")
|
||||
os.Setenv("CLOUD_HOME", fullpath)
|
||||
}
|
||||
|
||||
var data []byte
|
||||
var err error
|
||||
if data, err = loadFile("gus.text"); err != nil {
|
||||
log.Warn("gus.text failed to load", err)
|
||||
// something went wrong loading the file
|
||||
return nil
|
||||
}
|
||||
|
||||
if data == nil {
|
||||
return nil
|
||||
}
|
||||
p := new(Portmaps)
|
||||
if err = p.UnmarshalTEXT(data); err != nil {
|
||||
log.Warn("unmarshal failed on gus.text config file", err)
|
||||
return nil
|
||||
}
|
||||
|
||||
log.Log(INFO, "gus.ConfigLoad() has", p.Len(), "port mappings")
|
||||
return p
|
||||
}
|
||||
|
||||
func (m *Portmaps) ConfigLoad() error {
|
||||
if m == nil {
|
||||
return errors.New("It's not safe to run ConfigLoad() on a nil ?")
|
||||
|
|
4
doGui.go
4
doGui.go
|
@ -53,6 +53,10 @@ func doGui() {
|
|||
|
||||
grid.NewButton("Events", func() {
|
||||
log.Info("todo: start a list here!")
|
||||
pm := me.portmaps.InsertByListen(5556)
|
||||
pm.Connect = "haha. gotcha"
|
||||
pm.Enabled = true
|
||||
me.portmaps.ConfigSave()
|
||||
})
|
||||
|
||||
// sit here forever refreshing the GUI
|
||||
|
|
12
main.go
12
main.go
|
@ -33,10 +33,14 @@ func main() {
|
|||
|
||||
me = new(gusconf)
|
||||
me.pollDelay = 10 * time.Second
|
||||
me.portmaps = NewPortmaps()
|
||||
p := new(Portmap)
|
||||
p.Connect = "testing:323"
|
||||
me.portmaps.Append(p)
|
||||
me.portmaps = ConfigLoad()
|
||||
|
||||
if me.portmaps == nil {
|
||||
me.portmaps = NewPortmaps()
|
||||
p := new(Portmap)
|
||||
p.Connect = "testing:323"
|
||||
me.portmaps.Append(p)
|
||||
}
|
||||
|
||||
if argv.Daemon {
|
||||
// turn off timestamps for STDOUT (systemd adds them)
|
||||
|
|
|
@ -14,13 +14,14 @@ message Events {
|
|||
}
|
||||
|
||||
message Portmap {
|
||||
int64 listen = 1;
|
||||
string connect = 2;
|
||||
int64 listen = 1; // `autogenpb:unique`
|
||||
string connect = 2; // `autogenpb:unique`
|
||||
bool enabled = 3;
|
||||
}
|
||||
|
||||
message Portmaps { // `autogenpb:marshal` `autogenpb:gui`
|
||||
message Portmaps { // `autogenpb:marshal` `autogenpb:gui` `autogenpb:nomutex`
|
||||
string uuid = 1; // `autogenpb:uuid:49a865ea-292d-48fd-8dc2-d0f82d5fd016`
|
||||
string version = 2; // `autogenpb:version:v0.0.1`
|
||||
repeated Portmap portmaps = 3;
|
||||
Events events = 4;
|
||||
}
|
||||
|
|
|
@ -6,7 +6,6 @@ package main
|
|||
import (
|
||||
"go.wit.com/gui"
|
||||
"go.wit.com/lib/gadgets"
|
||||
"go.wit.com/lib/protobuf/zoopb"
|
||||
"go.wit.com/log"
|
||||
)
|
||||
|
||||
|
@ -17,9 +16,11 @@ func makePortmapWin() {
|
|||
log.Info("test delete window here")
|
||||
}
|
||||
grid := me.portwin.win.Group.RawGrid()
|
||||
grid.NewButton("save machines.pb", func() {
|
||||
grid.NewButton("ConfigSave() ", func() {
|
||||
saveMachineState()
|
||||
})
|
||||
grid.NewButton("Add() ", func() {
|
||||
})
|
||||
grid.NewCheckbox("hide active")
|
||||
grid.NewButton("update", func() {
|
||||
doMachinesUpgradeTable()
|
||||
|
@ -53,63 +54,36 @@ func AddMachinesPB(tbox *gui.Node, pb *Portmaps) *PortmapsTable {
|
|||
t.NewUuid()
|
||||
t.SetParent(tbox)
|
||||
|
||||
f := func(m *Portmap) string {
|
||||
editf := func(m *Portmap) string {
|
||||
log.Info("machine =", m.Connect)
|
||||
return "now"
|
||||
return "edit"
|
||||
}
|
||||
t.AddButtonFunc("upgrade", f)
|
||||
t.AddButtonFunc("edit", editf)
|
||||
|
||||
enablef := func(p *Portmap) string {
|
||||
if p.Enabled {
|
||||
p.Enabled = false
|
||||
} else {
|
||||
p.Enabled = true
|
||||
}
|
||||
return "enable"
|
||||
}
|
||||
t.AddButtonFunc("enable", enablef)
|
||||
|
||||
enabledf := func(p *Portmap) string {
|
||||
if p.Enabled {
|
||||
return "true"
|
||||
}
|
||||
return "false"
|
||||
}
|
||||
t.AddStringFunc("enabled", enabledf)
|
||||
|
||||
t.AddListen()
|
||||
t.AddConnect()
|
||||
// t.AddMemory()
|
||||
// t.AddCpus()
|
||||
/*
|
||||
t.AddStringFunc("sMB", func(m *oopb.Machine) string {
|
||||
return fmt.Sprintf("%d mb", m.Memory/(1024*1024))
|
||||
})
|
||||
|
||||
t.AddStringFunc("portwin", func(m *zoopb.Machine) string {
|
||||
return findVersion(m, "portwin")
|
||||
})
|
||||
*/
|
||||
|
||||
/*
|
||||
// show if the machine needs to be upgraded
|
||||
t.AddStringFunc("triggered?", func(m *zoopb.Machine) string {
|
||||
if m.Upgrade {
|
||||
return "yes"
|
||||
}
|
||||
return ""
|
||||
})
|
||||
*/
|
||||
|
||||
/*
|
||||
t.AddTimeFunc("age", func(m *zoopb.Machine) time.Time {
|
||||
return m.Laststamp.AsTime()
|
||||
})
|
||||
*/
|
||||
|
||||
t.ShowTable()
|
||||
return t
|
||||
}
|
||||
|
||||
func findVersion(m *zoopb.Machine, pkgname string) string {
|
||||
portwin := m.Packages.FindByName(pkgname)
|
||||
if portwin == nil {
|
||||
return "n/a"
|
||||
}
|
||||
return portwin.Version
|
||||
}
|
||||
|
||||
func saveMachineState() {
|
||||
/*
|
||||
cur := zoopb.NewMachines()
|
||||
|
||||
all := me.machines.SortByHostname()
|
||||
for all.Scan() {
|
||||
m := all.Next()
|
||||
log.Info("have machine:", m.Hostname)
|
||||
cur.Append(m)
|
||||
}
|
||||
cur.ConfigSave()
|
||||
*/
|
||||
me.portmaps.ConfigSave()
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue