From 89245aef3dcb3f808388763c029d57fab07d120a Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Fri, 24 May 2019 17:39:05 -0700 Subject: [PATCH] start moving away from 'gookit/config' to protobuf Signed-off-by: Jeff Carr --- Makefile | 2 +- main.go | 50 +++++++++++++++++++++++----------- resources/protobuf-config.json | 28 +++++++++++++++++++ 3 files changed, 63 insertions(+), 17 deletions(-) create mode 100644 resources/protobuf-config.json diff --git a/Makefile b/Makefile index 1312d60..3d5265e 100644 --- a/Makefile +++ b/Makefile @@ -45,7 +45,7 @@ update: # make the json config file human readable json-readable: - cat /tmp/cloud-control-panel.json |jq -r --tab + cat ~/.config/cloud-control-panel.json |jq -r --tab json-restore: cp resources/jcarr.json ~/.config/cloud-control-panel.json diff --git a/main.go b/main.go index ee1e6a7..7125218 100644 --- a/main.go +++ b/main.go @@ -15,6 +15,8 @@ import "github.com/davecgh/go-spew/spew" // will try to get this hosts FQDN import "github.com/Showmax/go-fqdn" +import jsonpb "github.com/golang/protobuf/jsonpb" + // import "github.com/golang/protobuf/proto" import pb "git.wit.com/wit/witProtobuf" @@ -24,6 +26,8 @@ var GITCOMMIT string // this is passed in as an ldflag var GOVERSION string // this is passed in as an ldflag var BUILDTIME string // this is passed in as an ldflag +var pbC *pb.Config + // reminder to use this for JSON // https://github.com/tidwall/gjson // value := gjson.Get(json, "name.last") @@ -78,6 +82,13 @@ func main() { // This will parse the command line and config file parseConfig(defaultConfig) + pbC = pb.MakeDefaultConfig() + spew.Dump(pbC) + + marshaler := &jsonpb.Marshaler{} + stuff, _ := marshaler.MarshalToString(pbC) + log.Println(stuff) + // only test the socket code if no GUI if (config.String("nogui") == "true") { log.Println("Need to re-implement this") @@ -95,8 +106,8 @@ func main() { onExit(err) } - gui.Data.Width = config.Int("width") - gui.Data.Height = config.Int("height") + gui.Data.Width = int(pbC.Width) + gui.Data.Height = int(pbC.Height) // TODO: figure out the hostname the right way hostname := fqdn.Get() @@ -131,6 +142,9 @@ func main() { gui.GoMainWindow() } +func processButtonMap(b *gui.ButtonMap) { +} + func mainButtonClick(b *gui.ButtonMap) { log.Println("mainButtonClick() b =", b) @@ -145,17 +159,14 @@ func mainButtonClick(b *gui.ButtonMap) { log.Println("\tb.AccNick", b.AccNick) if (b.Note == "BACK") { gui.Data.State = "splash" - } - if (b.Note == "QUIT") { + } else if (b.Note == "QUIT") { onExit(nil) - } - if (b.Note == "CREATE") { + } else if (b.Note == "CREATE") { log.Println("TRY TO ADD A NEW VIRTUAL MACHINE") log.Println("\tTRIGGER CREATE VM") gui.Data.State = "CREATE" // gui.Data.AccNick = b.AccNick - } - if (b.Note == "CONFIG") { + } else if (b.Note == "CONFIG") { log.Println("TRY TO LOAD DEFAULT CONFIG") defaultConfig, _ := packrBox.FindString("test.json") config.LoadData(defaultConfig) @@ -166,8 +177,7 @@ func mainButtonClick(b *gui.ButtonMap) { log.Println("SETTING gui.State =", gui.Data.State) } gui.Data.State = "done" - } - if (b.Note == "DEBUG") { + } else if (b.Note == "DEBUG") { log.Println("debug.PrintStack() (SHOULD BE JUST THIS goroutine)") debug.PrintStack() @@ -176,8 +186,7 @@ func mainButtonClick(b *gui.ButtonMap) { runtime.Stack(buf, true) log.Printf("%s", buf) log.Println("FINISHED FULL STACK DUMP") - } - if (b.Note == "ADD") { + } else if (b.Note == "ADD") { log.Println("\tSHOULD ADD ACCOUNT HERE") if (gui.Data.AccNick != "") { log.Println("\tADDING ACCOUNT HERE") @@ -190,8 +199,7 @@ func mainButtonClick(b *gui.ButtonMap) { config.Set("accounts." + gui.Data.AccNick + ".password", gui.Data.AccPass) config.Set("accounts." + gui.Data.AccNick + ".hostname", "v000185.testing.com.customers.wprod.wit.com") } - } - if (b.Note == "LOGIN") { + } else if (b.Note == "LOGIN") { log.Println("\tTRIGGER LOGIN ACCOUNT") gui.Data.State = "SEND LOGIN" gui.Data.AccNick = b.AccNick @@ -236,8 +244,7 @@ func mainButtonClick(b *gui.ButtonMap) { return } } - } - if (b.Note == "SHOW") { + } else if (b.Note == "SHOW") { log.Println("\tTRIGGER DISPLAY ACCOUNT") gui.Data.State = "SEND WEBSOCKET" gui.Data.AccNick = b.AccNick @@ -292,6 +299,17 @@ func mainButtonClick(b *gui.ButtonMap) { func watchGUI() { count := 0 + // This is how you marshal into JSON from a Protobuf + event := pb.MakeGetEvent() + event.Token = config.String("accounts." + gui.Data.AccNick + ".token") + marshaler := &jsonpb.Marshaler{} + stuff, _ := marshaler.MarshalToString(event) + log.Println(stuff) + // also: + // var j *bytes.Buffer + // marshaler.Marshal(j, event) + // w.Write(j.Bytes()) + for { if (count > 10) { log.Println("Sleep() in watchGUI() gui.Data.State =", gui.Data.State) diff --git a/resources/protobuf-config.json b/resources/protobuf-config.json new file mode 100644 index 0000000..e7568f5 --- /dev/null +++ b/resources/protobuf-config.json @@ -0,0 +1,28 @@ +{ + "USER": "jcarr", + "width": 700, + "height": 500, + "accounts": [ + { + "nick": "jcarr", + "username": "jcarr@wit.com", + "token": "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjozLCJyIjoiIiwiY3NyZiI6InRBY1p2eXVJbk1YdWUxV0RSbDFIeDI5YSIsImV4cCI6MTU1OTI3MDQwMCwiaXNzIjoid2l0Iiwic3ViIjoiamNhcnJAd2l0LmNvbSJ9.bqXX_6yrUHQGYh3SEmW8ydSa9Xfqx-HIKutTN_GirwhC_VrVX1xJBcgYfjdKGegvwY7Td1vO3rs40Iz7ifcptrtdzJnDX62d_1JJPKBHUQUfnTLr2qoTgaljElFM0Q_e", + "hostname": "hosttest.wit.com", + "domainname": "test.wit.com" + }, + { + "nick": "jcarr2", + "username": "jcarr@wit.com", + "token": "brokenToken", + "hostname": "hosttest.wit.com", + "domainname": "test.wit.com" + }, + { + "nick": "bmath", + "username": "jcarr@wit.com", + "token": "eyJhbGciOiJFUzM4NCIsInR5cCI6IkpXVCJ9.eyJ4IjoyLCJyIjoiIiwiY3NyZiI6Ik9rR0JWenphV2cxQjVlN0R5YjRXSzIyWCIsImV4cCI6MTU1OTE4NTc1MiwiaXNzIjoid2l0Iiwic3ViIjoiYm1hdGhAd2l0LmNvbSJ9.vdOAXyt3VIovqEIbivgt6upqR8glZv2UdzFcyudzCmGV-msdZWi_9TZaATyQMxEaVD3K6gRunakyOWK0jw4xxeDUbQym86IKMU2UOjp0tN0z72OmH822NmQ8_AgWiKNI", + "hostname": "hosttest.wit.com", + "domainname": "test.wit.com" + } + ] +}