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" import "os/user" import "flag" import "fmt" import "runtime" // always override the debugging flag from the command line var debugging *bool var debugtable *bool 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 hostname string flag.StringVar (&hostname, "hostname", "localhost", "Your full hostname") height := flag.Int ("height", 0, "Height of the Window") width := flag.Int ("width", 0, "Width of the Window") debugging = flag.Bool("debugging", false, "Enable debugging") debugtable = flag.Bool("debugtable", false, "Enable GUI table debugging") nogui := flag.Bool("nogui", false, "Do not display the GUI") // 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") onExit(nil) } if (*width > 100) { log.Println("ENABLE width =", int32(*width)) pbC.Width = int32(*width) } if (*height > 100) { log.Println("ENABLE height =", int32(*height)) pbC.Height = int32(*height) } log.Println("ENABLE nogui =", *nogui) log.Println("ENABLE debugging =", *debugging) // if (debugging == true) { if (*debugging) { log.Println("ENABLE width =", *width) log.Println("ENABLE debugging =", *debugging) } else { log.Println("DISABLE DEBUG debugging =", *debugging) } // os.Exit(0) if (hostname == "") { pbC.Hostname = hostname } pbC.Debugging = *debugging pbC.Debugtable = *debugtable log.Println("pbC.Width", pbC.Width) log.Println("pbC.Debugging", pbC.Debugging) } func parseConfig(defaultConfig string) { parseFlags() log.Println("pbC.width", pbC.Width) log.Println("pbC.height", pbC.Height) log.Println("pbC.debugging", pbC.Debugging) // config.LoadOSEnv([]string{"MAIL"}) // config.LoadOSEnv([]string{"USER"}) // config.LoadOSEnv([]string{"BUILDDEBUG"}) // 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") pbC.Filename = user.HomeDir + "/.config/cloud-control-panel.json" } else if runtime.GOOS == "windows" { log.Println("OS: Windows") pbC.Filename = user.HomeDir + "/.config/cloud-control-panel.json" // config.Set("configfile", user.HomeDir + "\\cloud-control-panel.json") } else { log.Println("OS: " + runtime.GOOS) pbC.Filename = user.HomeDir + "/cloud-control-panel.json" // config.Set("configfile", user.HomeDir + "/cloud-control-panel.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) // always override the JSON config file debugging flags from the command line config.Set("debugging", *debugging) config.Set("debugtable", *debugtable) for account, _ := range config.StringMap("cloud") { port := config.String("accounts." + account + ".port") proto := config.String("accounts." + account + ".proto") hostname := config.String("accounts." + account + ".hostname") log.Println(account, hostname, port, proto) } if (config.String("nogui") == "true") { log.Println("DO NOT DISPLAY THE GUI") } */ }