start moving away from 'gookit/config' to protobuf

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-24 17:39:05 -07:00
parent 86aee5482c
commit 89245aef3d
3 changed files with 63 additions and 17 deletions

View File

@ -45,7 +45,7 @@ update:
# make the json config file human readable # make the json config file human readable
json-readable: json-readable:
cat /tmp/cloud-control-panel.json |jq -r --tab cat ~/.config/cloud-control-panel.json |jq -r --tab
json-restore: json-restore:
cp resources/jcarr.json ~/.config/cloud-control-panel.json cp resources/jcarr.json ~/.config/cloud-control-panel.json

50
main.go
View File

@ -15,6 +15,8 @@ import "github.com/davecgh/go-spew/spew"
// will try to get this hosts FQDN // will try to get this hosts FQDN
import "github.com/Showmax/go-fqdn" import "github.com/Showmax/go-fqdn"
import jsonpb "github.com/golang/protobuf/jsonpb"
// import "github.com/golang/protobuf/proto" // import "github.com/golang/protobuf/proto"
import pb "git.wit.com/wit/witProtobuf" 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 GOVERSION string // this is passed in as an ldflag
var BUILDTIME 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 // reminder to use this for JSON
// https://github.com/tidwall/gjson // https://github.com/tidwall/gjson
// value := gjson.Get(json, "name.last") // value := gjson.Get(json, "name.last")
@ -78,6 +82,13 @@ func main() {
// This will parse the command line and config file // This will parse the command line and config file
parseConfig(defaultConfig) 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 // only test the socket code if no GUI
if (config.String("nogui") == "true") { if (config.String("nogui") == "true") {
log.Println("Need to re-implement this") log.Println("Need to re-implement this")
@ -95,8 +106,8 @@ func main() {
onExit(err) onExit(err)
} }
gui.Data.Width = config.Int("width") gui.Data.Width = int(pbC.Width)
gui.Data.Height = config.Int("height") gui.Data.Height = int(pbC.Height)
// TODO: figure out the hostname the right way // TODO: figure out the hostname the right way
hostname := fqdn.Get() hostname := fqdn.Get()
@ -131,6 +142,9 @@ func main() {
gui.GoMainWindow() gui.GoMainWindow()
} }
func processButtonMap(b *gui.ButtonMap) {
}
func mainButtonClick(b *gui.ButtonMap) { func mainButtonClick(b *gui.ButtonMap) {
log.Println("mainButtonClick() b =", b) log.Println("mainButtonClick() b =", b)
@ -145,17 +159,14 @@ func mainButtonClick(b *gui.ButtonMap) {
log.Println("\tb.AccNick", b.AccNick) log.Println("\tb.AccNick", b.AccNick)
if (b.Note == "BACK") { if (b.Note == "BACK") {
gui.Data.State = "splash" gui.Data.State = "splash"
} } else if (b.Note == "QUIT") {
if (b.Note == "QUIT") {
onExit(nil) onExit(nil)
} } else if (b.Note == "CREATE") {
if (b.Note == "CREATE") {
log.Println("TRY TO ADD A NEW VIRTUAL MACHINE") log.Println("TRY TO ADD A NEW VIRTUAL MACHINE")
log.Println("\tTRIGGER CREATE VM") log.Println("\tTRIGGER CREATE VM")
gui.Data.State = "CREATE" gui.Data.State = "CREATE"
// gui.Data.AccNick = b.AccNick // gui.Data.AccNick = b.AccNick
} } else if (b.Note == "CONFIG") {
if (b.Note == "CONFIG") {
log.Println("TRY TO LOAD DEFAULT CONFIG") log.Println("TRY TO LOAD DEFAULT CONFIG")
defaultConfig, _ := packrBox.FindString("test.json") defaultConfig, _ := packrBox.FindString("test.json")
config.LoadData(defaultConfig) config.LoadData(defaultConfig)
@ -166,8 +177,7 @@ func mainButtonClick(b *gui.ButtonMap) {
log.Println("SETTING gui.State =", gui.Data.State) log.Println("SETTING gui.State =", gui.Data.State)
} }
gui.Data.State = "done" gui.Data.State = "done"
} } else if (b.Note == "DEBUG") {
if (b.Note == "DEBUG") {
log.Println("debug.PrintStack() (SHOULD BE JUST THIS goroutine)") log.Println("debug.PrintStack() (SHOULD BE JUST THIS goroutine)")
debug.PrintStack() debug.PrintStack()
@ -176,8 +186,7 @@ func mainButtonClick(b *gui.ButtonMap) {
runtime.Stack(buf, true) runtime.Stack(buf, true)
log.Printf("%s", buf) log.Printf("%s", buf)
log.Println("FINISHED FULL STACK DUMP") log.Println("FINISHED FULL STACK DUMP")
} } else if (b.Note == "ADD") {
if (b.Note == "ADD") {
log.Println("\tSHOULD ADD ACCOUNT HERE") log.Println("\tSHOULD ADD ACCOUNT HERE")
if (gui.Data.AccNick != "") { if (gui.Data.AccNick != "") {
log.Println("\tADDING ACCOUNT HERE") 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 + ".password", gui.Data.AccPass)
config.Set("accounts." + gui.Data.AccNick + ".hostname", "v000185.testing.com.customers.wprod.wit.com") config.Set("accounts." + gui.Data.AccNick + ".hostname", "v000185.testing.com.customers.wprod.wit.com")
} }
} } else if (b.Note == "LOGIN") {
if (b.Note == "LOGIN") {
log.Println("\tTRIGGER LOGIN ACCOUNT") log.Println("\tTRIGGER LOGIN ACCOUNT")
gui.Data.State = "SEND LOGIN" gui.Data.State = "SEND LOGIN"
gui.Data.AccNick = b.AccNick gui.Data.AccNick = b.AccNick
@ -236,8 +244,7 @@ func mainButtonClick(b *gui.ButtonMap) {
return return
} }
} }
} } else if (b.Note == "SHOW") {
if (b.Note == "SHOW") {
log.Println("\tTRIGGER DISPLAY ACCOUNT") log.Println("\tTRIGGER DISPLAY ACCOUNT")
gui.Data.State = "SEND WEBSOCKET" gui.Data.State = "SEND WEBSOCKET"
gui.Data.AccNick = b.AccNick gui.Data.AccNick = b.AccNick
@ -292,6 +299,17 @@ func mainButtonClick(b *gui.ButtonMap) {
func watchGUI() { func watchGUI() {
count := 0 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 { for {
if (count > 10) { if (count > 10) {
log.Println("Sleep() in watchGUI() gui.Data.State =", gui.Data.State) log.Println("Sleep() in watchGUI() gui.Data.State =", gui.Data.State)

View File

@ -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"
}
]
}