From 0badd56291d8e14880f16d61fde4c673de605cc1 Mon Sep 17 00:00:00 2001 From: Winni Neessen Date: Thu, 29 Apr 2021 11:55:18 +0200 Subject: [PATCH] Made bool parameters switchable so the make more sense The previous behavior was wrong in a way, that default values like "use lower case" defaulted to true and using the -L param would not set it to false, but simple keep the true default which made the parameter useless. Now all parameters still use the same defaults but setting the corresponding flag on the CLI will negate the default, so using -L on the CLI would set "useLowerCase" to false instead of the default of true. --- config.go | 52 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/config.go b/config.go index 04ef4aa..ea575c0 100644 --- a/config.go +++ b/config.go @@ -7,16 +7,32 @@ import ( // Parse the CLI flags func parseFlags() Config { - var config Config + var switchConf Config + defaultSwitches := Config{ + useLowerCase: true, + useUpperCase: true, + useNumber: true, + useSpecial: false, + useComplex: false, + humanReadable: false, + } + config := Config{ + useLowerCase: defaultSwitches.useLowerCase, + useUpperCase: defaultSwitches.useUpperCase, + useNumber: defaultSwitches.useNumber, + useSpecial: defaultSwitches.useSpecial, + useComplex: defaultSwitches.useComplex, + humanReadable: defaultSwitches.humanReadable, + } // Read and set all flags - flag.BoolVar(&config.useLowerCase, "L", true, "Use lower case characters in passwords") - flag.BoolVar(&config.useUpperCase, "U", true, "Use upper case characters in passwords") - flag.BoolVar(&config.useNumber, "N", true, "Use numbers in passwords") - flag.BoolVar(&config.useSpecial, "S", false, "Use special characters in passwords") - flag.BoolVar(&config.useComplex, "C", false, "Generate complex passwords (implies -L -U -N -S, disables -H)") + flag.BoolVar(&switchConf.useLowerCase, "L", false, "Use lower case characters in passwords") + flag.BoolVar(&switchConf.useUpperCase, "U", false, "Use upper case characters in passwords") + flag.BoolVar(&switchConf.useNumber, "N", false, "Use numerich characters in passwords") + flag.BoolVar(&switchConf.useSpecial, "S", false, "Use special characters in passwords") + flag.BoolVar(&switchConf.useComplex, "C", false, "Generate complex passwords (implies -L -U -N -S, disables -H)") + flag.BoolVar(&switchConf.humanReadable, "H", false, "Generate human-readable passwords") flag.BoolVar(&config.spellPassword, "l", false, "Spell generated password") - flag.BoolVar(&config.humanReadable, "H", false, "Generate human-readable passwords") flag.BoolVar(&config.showVersion, "v", false, "Show version") flag.IntVar(&config.minPassLen, "m", DefaultMinLenght, "Minimum password length") flag.IntVar(&config.maxPassLen, "x", DefaultMaxLenght, "Maxiumum password length") @@ -26,7 +42,27 @@ func parseFlags() Config { "New style password parameters (higher priority than single parameters)") flag.Parse() - // Parse additional parameters + // Invert-switch the defaults + if switchConf.useLowerCase { + config.useLowerCase = !defaultSwitches.useLowerCase + } + if switchConf.useUpperCase { + config.useUpperCase = !defaultSwitches.useUpperCase + } + if switchConf.useNumber { + config.useNumber = !defaultSwitches.useNumber + } + if switchConf.useSpecial { + config.useSpecial = !defaultSwitches.useSpecial + } + if switchConf.useComplex { + config.useComplex = !defaultSwitches.useComplex + } + if switchConf.humanReadable { + config.humanReadable = !defaultSwitches.humanReadable + } + + // Parse additional parameters and new-style switches parseParams(&config) return config