2019-02-01 07:28:17 -06:00
|
|
|
package main
|
|
|
|
|
|
|
|
/*
|
|
|
|
This simply parses the command line arguments using the default golang
|
|
|
|
package called 'flag'. This can be used as a simple template to parse
|
|
|
|
command line arguments in other programs.
|
|
|
|
|
2019-05-29 14:46:44 -05:00
|
|
|
It puts everything in a 'config' Protobuf which I think is a good
|
2019-02-01 07:28:17 -06:00
|
|
|
wrapper around the 'flags' package and doesn't need a whole mess of
|
|
|
|
global variables
|
|
|
|
*/
|
|
|
|
|
|
|
|
import "log"
|
|
|
|
import "os"
|
2019-05-23 13:33:30 -05:00
|
|
|
import "os/user"
|
2019-02-01 07:28:17 -06:00
|
|
|
import "flag"
|
|
|
|
import "fmt"
|
2019-05-23 11:47:48 -05:00
|
|
|
import "runtime"
|
2019-05-25 01:53:52 -05:00
|
|
|
import "io/ioutil"
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
import "github.com/golang/protobuf/jsonpb"
|
|
|
|
import pb "git.wit.com/wit/witProtobuf"
|
|
|
|
|
|
|
|
import "github.com/davecgh/go-spew/spew"
|
2019-05-23 11:47:48 -05:00
|
|
|
|
2019-05-25 01:53:52 -05:00
|
|
|
var config *pb.Config
|
|
|
|
|
|
|
|
// This loads the config file and marshals it into the
|
|
|
|
// config protocol buffer definition.
|
|
|
|
// Then it is very easy to pass all the config options
|
|
|
|
// around and the re-write that JSON file when the GUI
|
|
|
|
// exits
|
|
|
|
func loadConfigFile() {
|
|
|
|
// look up the user information
|
|
|
|
user, err := user.Current()
|
|
|
|
if err != nil {
|
|
|
|
onExit(err)
|
|
|
|
}
|
|
|
|
spew.Dump(user)
|
|
|
|
|
|
|
|
filename := ""
|
2019-05-25 23:28:21 -05:00
|
|
|
// TODO: confirm this is correct for MacOS and Windows
|
2019-05-25 01:53:52 -05:00
|
|
|
if runtime.GOOS == "linux" {
|
2019-05-29 14:46:44 -05:00
|
|
|
log.Println("loadConfigFile() OS: Linux")
|
2019-05-25 01:53:52 -05:00
|
|
|
filename = user.HomeDir + "/.config/cloud-control-panel.json"
|
|
|
|
} else if runtime.GOOS == "windows" {
|
2019-05-29 14:46:44 -05:00
|
|
|
log.Println("loadConfigFile() OS: Windows")
|
2019-05-25 16:40:06 -05:00
|
|
|
filename = user.HomeDir + "\\cloud-control-panel.json"
|
2019-05-25 01:53:52 -05:00
|
|
|
} else {
|
2019-05-29 14:46:44 -05:00
|
|
|
log.Println("loadConfigFile() OS: " + runtime.GOOS)
|
2019-05-25 16:48:02 -05:00
|
|
|
filename = user.HomeDir + "/.cloud-control-panel.json"
|
2019-05-25 01:53:52 -05:00
|
|
|
}
|
|
|
|
|
2019-05-25 02:02:50 -05:00
|
|
|
tmp := loadConfigFromFilename(filename)
|
2019-05-25 01:53:52 -05:00
|
|
|
config = &tmp
|
|
|
|
|
|
|
|
config.Filename = filename
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:28:17 -06:00
|
|
|
var customUsage = func() {
|
|
|
|
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
|
|
|
|
flag.PrintDefaults()
|
|
|
|
|
|
|
|
fmt.Println("")
|
|
|
|
fmt.Println("EXAMPLES:")
|
|
|
|
fmt.Println("")
|
2019-05-23 21:36:20 -05:00
|
|
|
fmt.Println(os.Args[0] + " --hostname test.hostname.wit.com")
|
2019-02-01 07:28:17 -06:00
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFlags() {
|
2019-05-23 15:14:57 -05:00
|
|
|
var hostname string
|
2019-02-01 07:28:17 -06:00
|
|
|
|
2019-05-26 04:19:23 -05:00
|
|
|
// always override the debugging flag from the command line
|
2019-05-28 19:24:45 -05:00
|
|
|
var debug *bool
|
2019-05-26 04:19:23 -05:00
|
|
|
var debugtable *bool
|
|
|
|
|
2019-02-01 07:28:17 -06:00
|
|
|
flag.StringVar (&hostname, "hostname", "localhost", "Your full hostname")
|
|
|
|
|
2019-05-24 20:41:31 -05:00
|
|
|
height := flag.Int ("height", 0, "Height of the Window")
|
|
|
|
width := flag.Int ("width", 0, "Width of the Window")
|
2019-05-28 19:24:45 -05:00
|
|
|
debug = flag.Bool("debug", false, "Enable debugging")
|
2019-05-24 11:04:36 -05:00
|
|
|
debugtable = flag.Bool("debugtable", false, "Enable GUI table debugging")
|
2019-02-01 07:28:17 -06:00
|
|
|
|
|
|
|
// Set the output if something fails to stdout rather than stderr
|
|
|
|
flag.CommandLine.SetOutput(os.Stdout)
|
|
|
|
|
|
|
|
flag.Usage = customUsage
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
if flag.Parsed() {
|
|
|
|
log.Println("flag.Parse() worked")
|
|
|
|
} else {
|
|
|
|
log.Println("flag.Parse() failed")
|
2019-05-23 15:14:57 -05:00
|
|
|
onExit(nil)
|
2019-02-01 07:28:17 -06:00
|
|
|
}
|
|
|
|
|
2019-05-24 20:41:31 -05:00
|
|
|
if (*width > 100) {
|
|
|
|
log.Println("ENABLE width =", int32(*width))
|
2019-05-25 01:53:52 -05:00
|
|
|
config.Width = int32(*width)
|
2019-05-24 20:41:31 -05:00
|
|
|
}
|
|
|
|
if (*height > 100) {
|
|
|
|
log.Println("ENABLE height =", int32(*height))
|
2019-05-25 01:53:52 -05:00
|
|
|
config.Height = int32(*height)
|
2019-05-24 20:41:31 -05:00
|
|
|
}
|
2019-05-13 11:05:03 -05:00
|
|
|
|
2019-05-24 20:41:31 -05:00
|
|
|
if (hostname == "") {
|
2019-05-25 01:53:52 -05:00
|
|
|
config.Hostname = hostname
|
2019-05-24 20:41:31 -05:00
|
|
|
}
|
2019-05-25 23:28:21 -05:00
|
|
|
|
|
|
|
// never allow these to be set in the config file
|
2019-05-28 19:24:45 -05:00
|
|
|
config.Debug = *debug
|
2019-05-25 01:53:52 -05:00
|
|
|
config.Debugtable = *debugtable
|
2019-02-01 07:28:17 -06:00
|
|
|
}
|
|
|
|
|
2019-05-25 23:28:21 -05:00
|
|
|
// Convert a Protobuf to JSON
|
|
|
|
func PBtoJSON() string {
|
|
|
|
// this makes a sample protobuf
|
2019-05-25 01:53:52 -05:00
|
|
|
c := pb.MakeDefaultConfig()
|
2019-05-23 13:33:30 -05:00
|
|
|
|
2019-05-25 01:53:52 -05:00
|
|
|
marshaler := &jsonpb.Marshaler{}
|
|
|
|
stuff, _ := marshaler.MarshalToString(c)
|
|
|
|
log.Println(stuff)
|
|
|
|
return stuff
|
|
|
|
}
|
2019-05-23 13:33:30 -05:00
|
|
|
|
2019-05-25 01:53:52 -05:00
|
|
|
func writeToFile(filename string, a string) {
|
|
|
|
f, _ := os.Create(filename)
|
|
|
|
f.WriteString(a)
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveConfig() {
|
|
|
|
filename := config.Filename
|
|
|
|
if (filename == "") {
|
|
|
|
log.Println("NOT SAVING CONFIG FILE")
|
|
|
|
} else {
|
|
|
|
marshaler := &jsonpb.Marshaler{}
|
|
|
|
stuff, _ := marshaler.MarshalToString(config)
|
|
|
|
log.Println(stuff)
|
|
|
|
|
|
|
|
writeToFile(filename, stuff)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// will load the default config from within the go binary
|
2019-05-25 02:02:50 -05:00
|
|
|
func loadConfigFromFilename(filename string) pb.Config {
|
2019-05-25 01:53:52 -05:00
|
|
|
sets := pb.Config{}
|
2019-05-25 02:02:50 -05:00
|
|
|
b, err := ioutil.ReadFile(filename)
|
2019-05-25 01:53:52 -05:00
|
|
|
if err != nil {
|
|
|
|
log.Println("ioutil.ReadFile() ERROR =", err)
|
2019-05-25 03:38:06 -05:00
|
|
|
// This probably means this is the first time the user is opening this
|
|
|
|
sets.Errors += 1
|
|
|
|
sets.Crash = "err"
|
|
|
|
sets.Counter = 1
|
|
|
|
sets.Width = 500
|
|
|
|
sets.Height = 500
|
|
|
|
return sets
|
2019-05-07 16:54:08 -05:00
|
|
|
}
|
2019-05-25 01:53:52 -05:00
|
|
|
log.Println("ioutil.ReadFile() b =", b)
|
2019-05-09 09:00:38 -05:00
|
|
|
|
2019-05-25 01:53:52 -05:00
|
|
|
err = jsonpb.Unmarshal(strings.NewReader(string(b)), &sets)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("jsonpb.Unmarshal() ERROR =", err)
|
2019-05-09 09:00:38 -05:00
|
|
|
}
|
2019-05-25 01:53:52 -05:00
|
|
|
spew.Dump(sets)
|
|
|
|
return sets
|
2019-02-01 07:28:17 -06:00
|
|
|
}
|
2019-05-25 01:53:52 -05:00
|
|
|
|
2019-05-25 02:02:50 -05:00
|
|
|
// will load the default config from within the go binary
|
2019-05-29 14:46:44 -05:00
|
|
|
func loadDefaultConfig() *pb.Config {
|
2019-05-25 02:02:50 -05:00
|
|
|
log.Println("TRY TO LOAD DEFAULT CONFIG")
|
|
|
|
// defaultConfig, _ := packrBox.FindString("protobuf-config.json")
|
2019-05-28 19:14:47 -05:00
|
|
|
b, err := packrBox.FindString("cloud-control-panel.json")
|
2019-05-25 02:02:50 -05:00
|
|
|
log.Println("b =", b)
|
|
|
|
log.Println("err =", err)
|
|
|
|
// var newpb *pb.Config
|
|
|
|
sets := pb.Config{}
|
|
|
|
|
|
|
|
err = jsonpb.Unmarshal(strings.NewReader(string(b)), &sets)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("jsonpb.Unmarshal() ERROR =", err)
|
2019-05-25 01:53:52 -05:00
|
|
|
}
|
2019-05-25 02:02:50 -05:00
|
|
|
spew.Dump(sets)
|
2019-05-29 14:46:44 -05:00
|
|
|
return &sets
|
2019-05-25 02:02:50 -05:00
|
|
|
}
|
2019-05-26 04:19:23 -05:00
|
|
|
|
|
|
|
//
|
|
|
|
// This is what you call from main()
|
|
|
|
//
|
|
|
|
func parseConfig() {
|
|
|
|
// first load the config file
|
|
|
|
loadConfigFile()
|
|
|
|
|
|
|
|
// override the config file from the command line
|
|
|
|
parseFlags()
|
|
|
|
|
|
|
|
log.Println("config.width", config.Width)
|
|
|
|
log.Println("config.height", config.Height)
|
2019-05-28 19:24:45 -05:00
|
|
|
log.Println("config.debug", config.Debug)
|
2019-05-26 04:19:23 -05:00
|
|
|
|
|
|
|
// check that the config parsing worked
|
|
|
|
for key, foo := range config.Accounts {
|
|
|
|
log.Println("account = ", key, foo)
|
|
|
|
log.Println("Accounts[key] = ", config.Accounts[key])
|
|
|
|
}
|
2019-05-29 14:46:44 -05:00
|
|
|
if (config.Accounts == nil) {
|
|
|
|
log.Println("loadConfigFile() config.Accounts == nil")
|
|
|
|
log.Println("If debugging is on, should load default config here")
|
|
|
|
if (config.Debug) {
|
|
|
|
log.Println("Debugging is on, loading debug config")
|
|
|
|
config = loadDefaultConfig()
|
|
|
|
// onExit(fmt.Errorf("loadConfigFile() config.Accounts == nil"))
|
|
|
|
}
|
|
|
|
}
|
2019-05-26 04:19:23 -05:00
|
|
|
}
|