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.
This commit is contained in:
Winni Neessen 2021-04-29 11:55:18 +02:00
parent 98e81e4857
commit 0badd56291
Signed by: wneessen
GPG key ID: 385AC9889632126E

View file

@ -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