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.
|
|
|
|
|
|
|
|
It puts everything in the 'config' package which I think is a good
|
|
|
|
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-02-01 07:28:17 -06:00
|
|
|
import "github.com/gookit/config"
|
|
|
|
import "github.com/gookit/config/yaml"
|
2019-05-07 07:02:39 -05:00
|
|
|
import "github.com/gookit/config/json"
|
2019-02-01 07:28:17 -06:00
|
|
|
|
2019-05-06 20:57:59 -05:00
|
|
|
// import "github.com/davecgh/go-spew/spew"
|
|
|
|
|
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("")
|
|
|
|
fmt.Println(os.Args[0] + "--hostname test.hostname.wit.com")
|
|
|
|
fmt.Println("")
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseFlags() {
|
|
|
|
var version string
|
2019-05-09 09:00:38 -05:00
|
|
|
var nogui bool
|
2019-05-13 11:05:03 -05:00
|
|
|
var debugging bool
|
2019-02-01 07:28:17 -06:00
|
|
|
var hostname string
|
|
|
|
var width int
|
|
|
|
var height int
|
|
|
|
|
|
|
|
flag.StringVar (&version, "version", "v0.1", "Set compiled in version string")
|
|
|
|
|
|
|
|
flag.StringVar (&hostname, "hostname", "localhost", "Your full hostname")
|
|
|
|
flag.IntVar (&width, "width", 400, "Width of the Window")
|
|
|
|
flag.IntVar (&height, "height", 600, "Height of the Window")
|
|
|
|
|
2019-05-09 09:00:38 -05:00
|
|
|
flag.BoolVar (&nogui, "nogui", nogui, "Do not display the GUI")
|
2019-05-13 11:05:03 -05:00
|
|
|
flag.BoolVar (&debugging, "debugging", debugging, "Enable 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-13 11:05:03 -05:00
|
|
|
if (debugging == true) {
|
|
|
|
log.Println("ENABLE DEBUG", debugging)
|
|
|
|
} else {
|
|
|
|
log.Println("DISABLE DEBUG", debugging)
|
|
|
|
}
|
|
|
|
|
2019-02-01 07:28:17 -06:00
|
|
|
config.Set("width", width)
|
|
|
|
config.Set("height", height)
|
|
|
|
config.Set("hostname", hostname)
|
2019-05-09 09:00:38 -05:00
|
|
|
config.Set("nogui", nogui)
|
2019-05-13 11:05:03 -05:00
|
|
|
config.Set("debugging", debugging)
|
2019-02-01 07:28:17 -06:00
|
|
|
}
|
|
|
|
|
2019-05-23 11:47:48 -05:00
|
|
|
func parseConfig(defaultConfig string) {
|
2019-02-01 07:28:17 -06:00
|
|
|
config.WithOptions(config.ParseEnv)
|
|
|
|
parseFlags()
|
|
|
|
|
|
|
|
config.LoadOSEnv([]string{"MAIL"})
|
|
|
|
config.LoadOSEnv([]string{"USER"})
|
|
|
|
config.LoadOSEnv([]string{"BUILDDEBUG"})
|
|
|
|
|
2019-05-07 07:02:39 -05:00
|
|
|
// add driver for support of yaml and json
|
2019-02-01 07:28:17 -06:00
|
|
|
config.AddDriver(yaml.Driver)
|
2019-05-07 07:02:39 -05:00
|
|
|
config.AddDriver(json.Driver)
|
|
|
|
|
2019-05-23 13:33:30 -05:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2019-05-23 11:47:48 -05:00
|
|
|
// 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")
|
2019-05-23 13:33:30 -05:00
|
|
|
config.Set("configfile", user.HomeDir + "/.config/cloud-control-panel.json")
|
2019-05-23 11:47:48 -05:00
|
|
|
} else if runtime.GOOS == "windows" {
|
|
|
|
log.Println("OS: Windows")
|
2019-05-23 13:33:30 -05:00
|
|
|
config.Set("configfile", user.HomeDir + "\\cloud-control-panel.json")
|
2019-05-23 11:47:48 -05:00
|
|
|
} else {
|
|
|
|
log.Println("OS: " + runtime.GOOS)
|
2019-05-23 13:33:30 -05:00
|
|
|
config.Set("configfile", user.HomeDir + "/cloud-control-panel.json")
|
2019-05-23 11:47:48 -05:00
|
|
|
}
|
2019-05-23 13:33:30 -05:00
|
|
|
|
|
|
|
// 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)
|
2019-02-01 07:28:17 -06:00
|
|
|
|
|
|
|
for _, addr := range config.Strings("arr1") {
|
|
|
|
log.Println("addr =", addr)
|
|
|
|
}
|
|
|
|
|
2019-05-06 20:57:59 -05:00
|
|
|
map1 := config.StringMap("map1")
|
|
|
|
fmt.Printf("%#v\n",map1)
|
|
|
|
|
|
|
|
log.Println(config.String("map1.key1.jwc1"))
|
2019-05-07 07:02:39 -05:00
|
|
|
|
2019-05-07 16:54:08 -05:00
|
|
|
for account, _ := range config.StringMap("cloud") {
|
2019-05-23 12:04:22 -05:00
|
|
|
port := config.String("accounts." + account + ".port")
|
|
|
|
proto := config.String("accounts." + account + ".proto")
|
|
|
|
hostname := config.String("accounts." + account + ".hostname")
|
|
|
|
fmt.Println(account, hostname, port, proto)
|
2019-05-07 16:54:08 -05:00
|
|
|
}
|
2019-05-09 09:00:38 -05:00
|
|
|
|
|
|
|
if (config.String("nogui") == "true") {
|
|
|
|
log.Println("DO NOT DISPLAY THE GUI")
|
|
|
|
}
|
2019-05-13 11:05:03 -05:00
|
|
|
|
|
|
|
if (config.String("debugging") == "true") {
|
|
|
|
log.Println("ENABLE DEBUG", config.String("debugging"))
|
|
|
|
} else {
|
|
|
|
log.Println("DISABLE DEBUG", config.String("debugging"))
|
|
|
|
}
|
2019-02-01 07:28:17 -06:00
|
|
|
}
|