@@ -2,6 +2,7 @@ package main
22
33import (
44 "fmt"
5+ log "github.com/sirupsen/logrus"
56 "os"
67 "time"
78
@@ -13,7 +14,6 @@ import (
1314 "github.com/analogj/drawbridge/pkg/version"
1415 "github.com/fatih/color"
1516 "github.com/urfave/cli/v2"
16- "log"
1717 "strings"
1818)
1919
@@ -99,6 +99,11 @@ OPTIONS:
9999 //UsageText: "doo - does the dooing",
100100 Action : func (c * cli.Context ) error {
101101 fmt .Fprintln (c .App .Writer , c .Command .Usage )
102+ if c .Bool ("debug" ) {
103+ log .SetLevel (log .DebugLevel )
104+ } else {
105+ log .SetLevel (log .InfoLevel )
106+ }
102107
103108 projectList , err := project .CreateProjectListFromProvidedAnswers (config )
104109 if err != nil {
@@ -206,6 +211,7 @@ OPTIONS:
206211 destServer = ""
207212 }
208213
214+ config .SetOptionsFromAnswers (answerData )
209215 connectAction := actions.ConnectAction {Config : config }
210216 return connectAction .Start (answerData , destServer )
211217 },
@@ -276,6 +282,7 @@ OPTIONS:
276282 }
277283 }
278284
285+ config .SetOptionsFromAnswers (answerData )
279286 downloadAction := actions.DownloadAction {Config : config }
280287 return downloadAction .Start (answerData , strRemoteHostname , strRemotePath , strLocalPath )
281288 },
@@ -320,7 +327,7 @@ OPTIONS:
320327 }
321328
322329 //delete one config file.
323-
330+ config . SetOptionsFromAnswers ( answerData )
324331 deleteAction := actions.DeleteAction {Config : config }
325332 err = deleteAction .One (answerData , c .Bool ("force" ))
326333
@@ -404,6 +411,11 @@ func createFlags(appConfig config.Interface) ([]cli.Flag, error) {
404411 Usage : "Dry Run mode. Will print files and paths to STDOUT rather than writing them to disk." ,
405412 Value : false ,
406413 },
414+ & cli.BoolFlag {
415+ Name : "debug" ,
416+ Usage : "Enable verbose logging for debugging" ,
417+ Value : false ,
418+ },
407419 }
408420
409421 configQuestions , err := appConfig .GetQuestions ()
@@ -464,30 +476,40 @@ func createFlagHandler(appConfig config.Interface, answerValues map[string]inter
464476 // flag override
465477
466478 //get default defaultOptions from the config
467- defaultOptions := map [string ]interface {}{}
468- appConfig .UnmarshalKey ("options" , & defaultOptions )
479+ options := map [string ]interface {}{}
480+ appConfig .UnmarshalKey ("options" , & options )
481+ log .Debugf ("\n Default Options: %v" , options )
482+
483+ optionKeys := []string {}
484+ for key := range options {
485+ optionKeys = append (optionKeys , key )
486+ }
469487
470488 cliAnswers := answerValues
471- //find answer defaultOptions (and set them)
472- for optionName , _ := range defaultOptions {
489+
490+ //find optionKeys in answerValues
491+ for _ , optionKey := range optionKeys {
473492 //check if the key is set as an answer/default
474- if answerOptionValue , ok := answerValues [optionName ]; ok {
475- //this answer is actuall for an option. lets set it.
476- //fmt.Printf("Setting Option from Answer: %v (%v)", optionName, answerOptionValue)
477- appConfig .SetDefault (fmt .Sprintf ("options.%v" , optionName ), answerOptionValue )
493+ if answerOptionValue , ok := answerValues [optionKey ]; ok {
494+ //this answer is actualy for an option. lets set it.
495+ log .Debugf ("\n Setting option from Answer: %v (%v)" , optionKey , answerOptionValue )
496+ options [optionKey ] = answerOptionValue
497+ //appConfig.SetDefault(fmt.Sprintf("options.%v", optionKey), answerOptionValue)
478498 }
479499 }
480500
481501 for _ , flagName := range cliFlags {
482502
483- if _ , ok := defaultOptions [ flagName ]; ok {
503+ if utils . SliceIncludes ( optionKeys , flagName ) {
484504 //this flag is actually an "option". Lets set it.
485- appConfig .Set (fmt .Sprintf ("options.%v" , flagName ), c .String (flagName ))
505+ log .Debugf ("\n Setting option from CLI: %v (%v)" , flagName , c .String (flagName ))
506+ options [flagName ] = c .String (flagName )
507+ //appConfig.SetDefault(fmt.Sprintf("options.%v", flagName), c.String(flagName))
486508 continue
487509 }
488510
489- //skip dryrun
490- if flagName == "dryrun" {
511+ //skip dryrun & debug
512+ if flagName == "dryrun" || flagName == "debug" {
491513 continue
492514 }
493515
@@ -511,5 +533,14 @@ func createFlagHandler(appConfig config.Interface, answerValues map[string]inter
511533 }
512534 }
513535
536+ //set the config options section
537+ appConfig .Set ("options" , options )
538+
539+ if log .GetLevel () == log .DebugLevel {
540+ afterOptions := map [string ]interface {}{}
541+ appConfig .UnmarshalKey ("options" , & afterOptions )
542+ log .Debugf ("\n Options after overrides: %v" , afterOptions )
543+ }
544+
514545 return cliAnswers , nil
515546}
0 commit comments