override the config file from the cmdline for debugging

Signed-off-by: Jeff Carr <jcarr@wit.com>
This commit is contained in:
Jeff Carr 2019-05-23 13:14:57 -07:00
parent 3238695374
commit d9633fe2db
2 changed files with 48 additions and 25 deletions

View File

@ -23,6 +23,9 @@ import "github.com/gookit/config/json"
// import "github.com/davecgh/go-spew/spew" // import "github.com/davecgh/go-spew/spew"
// always override the debugging flag from the command line
var debugging *bool
var customUsage = func() { var customUsage = func() {
fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0]) fmt.Fprintf(flag.CommandLine.Output(), "Usage of %s:\n", os.Args[0])
flag.PrintDefaults() flag.PrintDefaults()
@ -35,21 +38,17 @@ var customUsage = func() {
} }
func parseFlags() { func parseFlags() {
var version string var version string
var nogui bool var hostname string
var debugging bool var height int
var hostname string
var width int
var height int
flag.StringVar (&version, "version", "v0.1", "Set compiled in version string") flag.StringVar (&version, "version", "v0.1", "Set compiled in version string")
flag.StringVar (&hostname, "hostname", "localhost", "Your full hostname") 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") flag.IntVar (&height, "height", 600, "Height of the Window")
flag.BoolVar (&nogui, "nogui", nogui, "Do not display the GUI") width := flag.Int ("width", 400, "Width of the Window")
flag.BoolVar (&debugging, "debugging", debugging, "Enable debugging") debugging = flag.Bool("debugging", false, "Enable debugging")
nogui := flag.Bool("nogui", false, "Do not display the GUI")
// Set the output if something fails to stdout rather than stderr // Set the output if something fails to stdout rather than stderr
flag.CommandLine.SetOutput(os.Stdout) flag.CommandLine.SetOutput(os.Stdout)
@ -61,25 +60,38 @@ func parseFlags() {
log.Println("flag.Parse() worked") log.Println("flag.Parse() worked")
} else { } else {
log.Println("flag.Parse() failed") log.Println("flag.Parse() failed")
onExit(nil)
} }
if (debugging == true) { log.Println("ENABLE width =", *width)
log.Println("ENABLE DEBUG", debugging) 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 { } else {
log.Println("DISABLE DEBUG", debugging) log.Println("DISABLE DEBUG debugging =", *debugging)
} }
// os.Exit(0)
config.Set("width", width) config.Set("width", *width)
config.Set("height", height) config.Set("height", height)
config.Set("hostname", hostname) config.Set("hostname", hostname)
config.Set("nogui", nogui) config.Set("nogui", *nogui)
config.Set("debugging", debugging) config.Set("debugging", *debugging)
log.Println("config.Set width", config.String("width"))
log.Println("config.Set debugging", config.Bool("debugging"))
} }
func parseConfig(defaultConfig string) { func parseConfig(defaultConfig string) {
config.WithOptions(config.ParseEnv) config.WithOptions(config.ParseEnv)
parseFlags() parseFlags()
log.Println("config.Set width", config.String("width"))
log.Println("config.Set debugging", config.Bool("debugging"))
config.LoadOSEnv([]string{"MAIL"}) config.LoadOSEnv([]string{"MAIL"})
config.LoadOSEnv([]string{"USER"}) config.LoadOSEnv([]string{"USER"})
config.LoadOSEnv([]string{"BUILDDEBUG"}) config.LoadOSEnv([]string{"BUILDDEBUG"})
@ -113,6 +125,9 @@ func parseConfig(defaultConfig string) {
filename := config.String("configfile") filename := config.String("configfile")
config.LoadFiles(filename) config.LoadFiles(filename)
// always override the debugging flag from the command line
config.Set("debugging", *debugging)
for _, addr := range config.Strings("arr1") { for _, addr := range config.Strings("arr1") {
log.Println("addr =", addr) log.Println("addr =", addr)
} }
@ -135,7 +150,9 @@ func parseConfig(defaultConfig string) {
if (config.String("debugging") == "true") { if (config.String("debugging") == "true") {
log.Println("ENABLE DEBUG", config.String("debugging")) log.Println("ENABLE DEBUG", config.String("debugging"))
log.Println("ENABLE DEBUG", config.Bool("debugging"))
} else { } else {
log.Println("DISABLE DEBUG", config.String("debugging")) log.Println("DISABLE DEBUG", config.String("debugging"))
log.Println("DISABLE DEBUG", config.Bool("debugging"))
} }
} }

24
main.go
View File

@ -39,16 +39,19 @@ func onExit(err error) {
time.Sleep(1 * 1000 * 1000 * 1000) time.Sleep(1 * 1000 * 1000 * 1000)
filename := config.String("configfile") filename := config.String("configfile")
log.Println("SAVING CONFIG FILE AS:", filename) if (filename == "") {
log.Println("NOT SAVING CONFIG FILE")
} else {
log.Println("SAVING CONFIG FILE AS:", filename)
f, err := os.Create(filename + ".yaml")
if err == nil {
config.DumpTo(f, "yaml")
}
f, err := os.Create(filename + ".yaml") f, err = os.Create(filename)
if err == nil { if err == nil {
config.DumpTo(f, "yaml") config.DumpTo(f, "json")
} }
f, err = os.Create(filename)
if err == nil {
config.DumpTo(f, "json")
} }
if (err != nil) { if (err != nil) {
@ -91,6 +94,9 @@ func main() {
gui.Data.GoVersion = GOVERSION gui.Data.GoVersion = GOVERSION
gui.Data.ButtonClickNew = buttonClickNew gui.Data.ButtonClickNew = buttonClickNew
gui.Data.HomeDir = user.HomeDir gui.Data.HomeDir = user.HomeDir
gui.Data.Debug = config.Bool("debugging")
log.Println("config Bool debugging = ", config.Bool("debugging"))
log.Println("gui.Data.Debug = ", gui.Data.Debug)
// Current User // Current User
log.Println("Hi " + user.Name + " (id: " + user.Uid + ")") log.Println("Hi " + user.Name + " (id: " + user.Uid + ")")