diff --git a/config.go b/config.go index 8ce2591..b9124fc 100644 --- a/config.go +++ b/config.go @@ -12,6 +12,7 @@ package main import "log" import "os" +import "os/user" import "flag" import "fmt" import "runtime" @@ -87,16 +88,30 @@ func parseConfig(defaultConfig string) { config.AddDriver(yaml.Driver) config.AddDriver(json.Driver) + // look up the user information to try to figure out where the config file is stored + user, err := user.Current() + if err != nil { + onExit(err) + } + // TODO: figure out how to look for this file in the right // place under Linux, MacOS and Windows if runtime.GOOS == "linux" { log.Println("OS: Linux") + config.Set("configfile", user.HomeDir + "/.config/cloud-control-panel.json") } else if runtime.GOOS == "windows" { log.Println("OS: Windows") + config.Set("configfile", user.HomeDir + "\\cloud-control-panel.json") } else { log.Println("OS: " + runtime.GOOS) + config.Set("configfile", user.HomeDir + "/cloud-control-panel.json") } - config.LoadFiles("config.json") + + // TODO: load the default config file from the resources dir if the config file doesn't exist yet + // config.LoadFiles("config.json") + + filename := config.String("configfile") + config.LoadFiles(filename) for _, addr := range config.Strings("arr1") { log.Println("addr =", addr) diff --git a/main.go b/main.go index 912648b..2405163 100644 --- a/main.go +++ b/main.go @@ -3,7 +3,7 @@ package main import "log" import "os" import "time" -// import "fmt" +import "os/user" import "github.com/gookit/config" import "github.com/gobuffalo/packr" @@ -34,20 +34,26 @@ var GOVERSION string // this is passed in as an ldflag // Exit and write out the config // (in yaml and json I guess.) // switch to json once it can be made human readable -func onExit() { +func onExit(err error) { log.Println("Sleep for 1 second") time.Sleep(1 * 1000 * 1000 * 1000) - f, err := os.Create("/tmp/cloud-control-panel.yaml") + filename := config.String("configfile") + + f, err := os.Create(filename + ".yaml") if err == nil { config.DumpTo(f, "yaml") } - f, err = os.Create("/tmp/cloud-control-panel.json") + f, err = os.Create(filename) if err == nil { config.DumpTo(f, "json") } + if (err != nil) { + panic(err) + } + os.Exit(0) } @@ -63,7 +69,7 @@ func main() { // only test the socket code if no GUI if (config.String("nogui") == "true") { log.Println("Need to re-implement this") - onExit() + onExit(nil) } initChannel() @@ -72,12 +78,27 @@ func main() { go gorillaDial("v000185.testing.com.customers.wprod.wit.com:9000") go watchGUI() + user, err := user.Current() + if err != nil { + onExit(err) + } + gui.Data.Width = config.Int("width") gui.Data.Height = config.Int("height") gui.Data.Version = "v0.4" gui.Data.GitCommit = GITCOMMIT gui.Data.GoVersion = GOVERSION gui.Data.ButtonClickNew = buttonClickNew + gui.Data.HomeDir = user.HomeDir + + // Current User + log.Println("Hi " + user.Name + " (id: " + user.Uid + ")") + log.Println("Username: " + user.Username) + log.Println("Home Dir: " + user.HomeDir) + + // Get "Real" User under sudo. + // More Info: https://stackoverflow.com/q/29733575/402585 + log.Println("Real User: " + os.Getenv("SUDO_USER")) // make this the main loop in an attempt to figure out the crashes // do not change this until the GUI is stable @@ -93,18 +114,21 @@ func buttonClickNew(b *gui.ButtonMap) { gui.Data.State = "splash" } if (b.Note == "QUIT") { - onExit() + onExit(nil) } if (b.Note == "ADD") { log.Println("\tSHOULD ADD ACCOUNT HERE") - log.Println("\tSHOULD ADD ACCOUNT HERE") - log.Println("\tSHOULD ADD ACCOUNT HERE") - log.Println("\tData.AccNick = ", gui.Data.AccNick) - log.Println("\tData.AccUser = ", gui.Data.AccUser) - log.Println("\tData.AccPass = ", gui.Data.AccPass) - config.Set("accounts." + gui.Data.AccNick + ".username", gui.Data.AccUser) - 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 (gui.Data.AccNick != "") { + log.Println("\tADDING ACCOUNT HERE") + log.Println("\tADDING ACCOUNT HERE") + log.Println("\tADDING ACCOUNT HERE") + log.Println("\tData.AccNick = ", gui.Data.AccNick) + log.Println("\tData.AccUser = ", gui.Data.AccUser) + log.Println("\tData.AccPass = ", gui.Data.AccPass) + config.Set("accounts." + gui.Data.AccNick + ".username", gui.Data.AccUser) + 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 == "BMATH") { log.Println("\tTRIGGER BMATH HERE")