@@ -12,7 +12,7 @@ import (
1212 "github.com/analogj/drawbridge/pkg/utils"
1313 "github.com/analogj/drawbridge/pkg/version"
1414 "github.com/fatih/color"
15- "gopkg.in /urfave/cli. v2"
15+ "github.com /urfave/cli/ v2"
1616 "log"
1717 "strings"
1818)
@@ -390,14 +390,14 @@ OPTIONS:
390390func createFlags (appConfig config.Interface ) ([]cli.Flag , error ) {
391391 flags := []cli.Flag {
392392 & cli.StringFlag {
393- Name : "active_config_template" ,
394- Usage : "Active config_template" ,
395- Value : appConfig .GetString ("options.active_config_template" ),
393+ Name : "active_config_template" ,
394+ Usage : "Active config_template" ,
395+ DefaultText : appConfig .GetString ("options.active_config_template" ),
396396 },
397397 & cli.StringSliceFlag {
398- Name : "active_custom_templates" ,
399- Usage : "Activated custom_templates" ,
400- Value : cli . NewStringSlice (appConfig .GetStringSlice ("options.active_custom_templates" )... ),
398+ Name : "active_custom_templates" ,
399+ Usage : "Activated custom_templates" ,
400+ DefaultText : strings . Join (appConfig .GetStringSlice ("options.active_custom_templates" ), ", " ),
401401 },
402402 & cli.BoolFlag {
403403 Name : "dryrun" ,
@@ -451,16 +451,36 @@ func createFlags(appConfig config.Interface) ([]cli.Flag, error) {
451451 return flags , nil
452452}
453453
454- func createFlagHandler (appConfig config.Interface , defaultValues map [string ]interface {}, cliFlags []string , c * cli.Context ) (map [string ]interface {}, error ) {
455-
456- cliAnswers := defaultValues
454+ func createFlagHandler (appConfig config.Interface , answerValues map [string ]interface {}, cliFlags []string , c * cli.Context ) (map [string ]interface {}, error ) {
455+ //there's 4 special cases we need to handle for "defaultOptions":
456+ //case 1: no flag override and no answer option
457+ //case 2: flag override and no answer option
458+ //case 3: no flag override and answer option
459+ //case 4: flag override and answer option
460+
461+ // the override order is:
462+ // default value from config
463+ // answer value from config
464+ // flag override
465+
466+ //get default defaultOptions from the config
467+ defaultOptions := map [string ]interface {}{}
468+ appConfig .UnmarshalKey ("options" , & defaultOptions )
469+
470+ cliAnswers := answerValues
471+ //find answer defaultOptions (and set them)
472+ for optionName , _ := range defaultOptions {
473+ //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+ appConfig .Set (fmt .Sprintf ("options.%v" , optionName ), answerOptionValue )
477+ }
478+ }
457479
458480 for _ , flagName := range cliFlags {
459- //handle options
460- options := map [string ]interface {}{}
461- appConfig .UnmarshalKey ("options" , & options )
462- if _ , ok := options [flagName ]; ok {
463- //this flag is actually an option. Lets set it.
481+
482+ if _ , ok := defaultOptions [flagName ]; ok {
483+ //this flag is actually an "option". Lets set it.
464484 appConfig .Set (fmt .Sprintf ("options.%v" , flagName ), c .String (flagName ))
465485 continue
466486 }
0 commit comments