mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-22 13:50:49 +01:00
Reduce complexity in main() function
This commit is contained in:
parent
f153754dc4
commit
cc45ec1119
1 changed files with 42 additions and 26 deletions
|
@ -65,26 +65,8 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Old style character modes
|
// Old style character modes
|
||||||
if humanReadable {
|
configOldStyle(config, humanReadable, lowerCase, upperCase, numeric,
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeHumanReadable)
|
special, complexPass)
|
||||||
}
|
|
||||||
if lowerCase {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeLowerCase)
|
|
||||||
}
|
|
||||||
if upperCase {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeUpperCase)
|
|
||||||
}
|
|
||||||
if numeric {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeNumeric)
|
|
||||||
}
|
|
||||||
if special {
|
|
||||||
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeSpecial)
|
|
||||||
}
|
|
||||||
if complexPass {
|
|
||||||
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeLowerCase|apg.ModeNumeric|
|
|
||||||
apg.ModeSpecial|apg.ModeUpperCase)
|
|
||||||
config.Mode = apg.MaskClearMode(config.Mode, apg.ModeHumanReadable)
|
|
||||||
}
|
|
||||||
|
|
||||||
// New style character modes (has higher priority than the old style modes)
|
// New style character modes (has higher priority than the old style modes)
|
||||||
if modeString != "" {
|
if modeString != "" {
|
||||||
|
@ -93,6 +75,21 @@ func main() {
|
||||||
|
|
||||||
// For the "minimum amount of" modes we need to imply at the type
|
// For the "minimum amount of" modes we need to imply at the type
|
||||||
// of character mode is set
|
// of character mode is set
|
||||||
|
configMinRequirement(config)
|
||||||
|
|
||||||
|
// Check if algorithm is supported
|
||||||
|
config.Algorithm = apg.IntToAlgo(algorithm)
|
||||||
|
if config.Algorithm == apg.AlgoUnsupported {
|
||||||
|
_, _ = fmt.Fprintf(os.Stderr, "unsupported algorithm value: %d\n", algorithm)
|
||||||
|
os.Exit(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Generate the password based on the given flags and print it to stdout
|
||||||
|
generate(config)
|
||||||
|
}
|
||||||
|
|
||||||
|
// configMinRequirement configures the "minimum amount" feature
|
||||||
|
func configMinRequirement(config *apg.Config) {
|
||||||
if config.MinLowerCase > 0 {
|
if config.MinLowerCase > 0 {
|
||||||
if float64(config.MinLength)/2 < float64(config.MinNumeric) {
|
if float64(config.MinLength)/2 < float64(config.MinNumeric) {
|
||||||
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
_, _ = os.Stderr.WriteString(MinimumAmountTooHigh)
|
||||||
|
@ -117,15 +114,34 @@ func main() {
|
||||||
}
|
}
|
||||||
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeUpperCase)
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeUpperCase)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Check if algorithm is supported
|
// configOldStyle configures the old style character modes
|
||||||
config.Algorithm = apg.IntToAlgo(algorithm)
|
func configOldStyle(config *apg.Config, humanReadable, lowerCase, upperCase,
|
||||||
if config.Algorithm == apg.AlgoUnsupported {
|
numeric, special, complexPass bool) {
|
||||||
_, _ = fmt.Fprintf(os.Stderr, "unsupported algorithm value: %d\n", algorithm)
|
if humanReadable {
|
||||||
os.Exit(1)
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeHumanReadable)
|
||||||
}
|
}
|
||||||
|
if lowerCase {
|
||||||
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeLowerCase)
|
||||||
|
}
|
||||||
|
if upperCase {
|
||||||
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeUpperCase)
|
||||||
|
}
|
||||||
|
if numeric {
|
||||||
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeNumeric)
|
||||||
|
}
|
||||||
|
if special {
|
||||||
|
config.Mode = apg.MaskToggleMode(config.Mode, apg.ModeSpecial)
|
||||||
|
}
|
||||||
|
if complexPass {
|
||||||
|
config.Mode = apg.MaskSetMode(config.Mode, apg.ModeLowerCase|apg.ModeNumeric|
|
||||||
|
apg.ModeSpecial|apg.ModeUpperCase)
|
||||||
|
config.Mode = apg.MaskClearMode(config.Mode, apg.ModeHumanReadable)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Generate the password based on the given flags
|
func generate(config *apg.Config) {
|
||||||
generator := apg.New(config)
|
generator := apg.New(config)
|
||||||
for i := int64(0); i < config.NumberPass; i++ {
|
for i := int64(0); i < config.NumberPass; i++ {
|
||||||
password, err := generator.Generate()
|
password, err := generator.Generate()
|
||||||
|
|
Loading…
Reference in a new issue