apg-go/cmd/apg/apg.go
Winni Neessen b31219046a
#53 Refactor mode handling and bitmask functions
The naming and handling of mode bitmasks have been refactored for improved code readability and maintainability. The term "Mode" was replaced with "ModeMask" for clarity and all associated functions were renamed accordingly (e.g., "SetMode" to "MaskSetMode"). These changes provide better insight into the function of the code and increase understandability for future development efforts. The command-line utility now also supports specifying modes via the "-M" flag.
2023-08-04 17:14:24 +02:00

76 lines
2.8 KiB
Go

// Package main is the APG command line client that makes use of the apg-go library
package main
import (
"flag"
"fmt"
"os"
"github.com/wneessen/apg-go"
)
func main() {
c := apg.NewConfig()
// Configure and parse the CLI flags
var ms string
flag.Int64Var(&c.MinLength, "m", c.MinLength, "")
flag.Int64Var(&c.MaxLength, "x", c.MaxLength, "")
flag.StringVar(&ms, "M", "", "")
flag.Int64Var(&c.NumberPass, "n", c.NumberPass, "")
flag.Usage = usage
flag.Parse()
if ms != "" {
c.Modes = apg.ModesFromFlags(ms)
}
for _, m := range []apg.Mode{apg.ModeHumanReadable, apg.ModeLowerCase, apg.ModeNumber, apg.ModeSpecial, apg.ModeUpperCase} {
fmt.Printf("%s: %t\n", m, apg.MaskHasMode(c.Modes, m))
}
/*
g := apg.New(c)
rb, err := g.RandomBytes(c.MinLength)
if err != nil {
fmt.Println("ERROR", err)
os.Exit(1)
}
fmt.Printf("Random: %#v\n", rb)
*/
}
// usage is used by the flag package to display the CLI usage message
func usage() {
// Usage text
const ut = `apg-go // A "Automated Password Generator"-clone
Copyleft (c) 2021-2023 Winni Neessen
apg [-a <algo>] [-m <length>] [-x <length>] [-L] [-U] [-N] [-S] [-H] [-C]
[-l] [-M mode] [-E char_string] [-n num_of_pass] [-v] [-h] [-t]
Options:
-a ALGORITH Choose the password generation algorithm (Default: 1)
- 0: pronounceable password generation (koremutake syllables)
- 1: random password generation according to password modes/flags
-m LENGTH Minimum length of the password to be generated (Default: 12)
-x LENGTH Maximum length of the password to be generated (Default: 20)
-n NUMBER Amount of password to be generated (Default: 6)
-E CHARS List of characters to be excluded in the generated password
-M [LUNSHClunshc] New style password parameters (upper case: on, lower case: off)
-L Use lower case characters in passwords (Default: on)
-U Use upper case characters in passwords (Default: on)
-N Use numeric characters in passwords (Default: on)
-S Use special characters in passwords (Default: off)
-H Avoid ambiguous characters in passwords (i. e.: 1, l, I, O, 0) (Default: off)
-C Enable complex password mode (implies -L -U -N -S and disables -H) (Default: off)
-l Spell generated passwords in phonetic alphabet (Default: off)
-p Check the HIBP database if the generated passwords was found in a leak before (Default: off)
- Note: this feature requires internet connectivity
-h Show this help text
-v Show version string
`
_, _ = os.Stderr.WriteString(ut)
}