diff options
| author | Jeff Carr <[email protected]> | 2025-03-09 09:30:39 -0500 | 
|---|---|---|
| committer | Jeff Carr <[email protected]> | 2025-03-09 09:30:39 -0500 | 
| commit | 2495995e6e50be78e8501eeeaf1a1b7cf067e6ae (patch) | |
| tree | cb4b867efc5402cfcab7ab9ffcfc315a5140aa73 | |
| parent | 66000419bf15cb255e436db44a2a74bf104484e3 (diff) | |
testing ConfigSave() and load
| -rw-r--r-- | config.go | 28 | ||||
| -rw-r--r-- | doGui.go | 4 | ||||
| -rw-r--r-- | main.go | 12 | ||||
| -rw-r--r-- | portmap.proto | 7 | ||||
| -rw-r--r-- | windowPortmap.go | 76 | 
5 files changed, 69 insertions, 58 deletions
@@ -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 ?") @@ -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 @@ -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) diff --git a/portmap.proto b/portmap.proto index a3c3964..e9e21ff 100644 --- a/portmap.proto +++ b/portmap.proto @@ -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;  } diff --git a/windowPortmap.go b/windowPortmap.go index 83ce7aa..3fb1ecf 100644 --- a/windowPortmap.go +++ b/windowPortmap.go @@ -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.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") -		}) -	*/ +	t.AddButtonFunc("edit", editf) -	/* -		// show if the machine needs to be upgraded -		t.AddStringFunc("triggered?", func(m *zoopb.Machine) string { -			if m.Upgrade { -				return "yes" -			} -			return "" -		}) -	*/ +	enablef := func(p *Portmap) string { +		if p.Enabled { +			p.Enabled = false +		} else { +			p.Enabled = true +		} +		return "enable" +	} +	t.AddButtonFunc("enable", enablef) -	/* -		t.AddTimeFunc("age", func(m *zoopb.Machine) time.Time { -			return m.Laststamp.AsTime() -		}) -	*/ +	enabledf := func(p *Portmap) string { +		if p.Enabled { +			return "true" +		} +		return "false" +	} +	t.AddStringFunc("enabled", enabledf) +	t.AddListen() +	t.AddConnect()  	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()  }  | 
