feat: add CLI to use it every day
This commit is contained in:
parent
81e23c0436
commit
8ef26fd974
2 changed files with 94 additions and 0 deletions
68
cmd/pwgen/main.go
Normal file
68
cmd/pwgen/main.go
Normal file
|
@ -0,0 +1,68 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"code.icb4dc0.de/prskr/go-pwgen"
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// although there might be terminals where it's less, let's be pessimistic
|
||||
const tabWidth = 8
|
||||
|
||||
var (
|
||||
length uint
|
||||
count int
|
||||
letters uint
|
||||
lowercase uint
|
||||
uppercase uint
|
||||
digits uint
|
||||
specials uint
|
||||
specialsAlphabet string
|
||||
)
|
||||
|
||||
func main() {
|
||||
flags := flag.NewFlagSet("pwgen", flag.ExitOnError)
|
||||
flags.IntVar(&count, "count", 20, "Number of passwords to generate")
|
||||
flags.UintVar(&length, "length", 20, "Length of each password to generate")
|
||||
flags.UintVar(&letters, "letters", 0, "Number of letters to include at least")
|
||||
flags.UintVar(&lowercase, "lowercase", 3, "Number of lowercase characters to include at least")
|
||||
flags.UintVar(&uppercase, "uppercase", 3, "Number of uppercase characters to include at least")
|
||||
flags.UintVar(&digits, "digits", 3, "Number of digits to include at least")
|
||||
flags.UintVar(&specials, "specials", 0, "Number of special characters to include at least")
|
||||
flags.StringVar(&specialsAlphabet, "specials-alphabet", pwgen.DefaultSpecialsAlphabet, "Special characters to include as special characters")
|
||||
|
||||
if err := flags.Parse(os.Args[1:]); err != nil {
|
||||
log.Fatalf("Failed to parse arguments: %v", err)
|
||||
}
|
||||
|
||||
terminalWidth := getWidth()
|
||||
|
||||
passwordsPerRow := int(terminalWidth / (length + tabWidth))
|
||||
|
||||
builder := strings.Builder{}
|
||||
for i := 0; i < count; i += passwordsPerRow {
|
||||
for j := 0; j < passwordsPerRow && i+j < count; j++ {
|
||||
pw, err := pwgen.Generate(
|
||||
pwgen.WithLength(length),
|
||||
pwgen.WithLetters(letters),
|
||||
pwgen.WithLowercase(lowercase),
|
||||
pwgen.WithUppercase(uppercase),
|
||||
pwgen.WithDigits(digits),
|
||||
pwgen.WithSpecials(specials),
|
||||
pwgen.WithSpecialsAlphabet(specialsAlphabet),
|
||||
)
|
||||
if err != nil {
|
||||
log.Fatalf("Error occurred generating password: %v", err)
|
||||
}
|
||||
|
||||
builder.WriteString(pw)
|
||||
builder.WriteRune('\t')
|
||||
}
|
||||
|
||||
fmt.Println(strings.TrimSuffix(builder.String(), "\t"))
|
||||
builder.Reset()
|
||||
}
|
||||
}
|
26
cmd/pwgen/terminal.go
Normal file
26
cmd/pwgen/terminal.go
Normal file
|
@ -0,0 +1,26 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"syscall"
|
||||
"unsafe"
|
||||
)
|
||||
|
||||
type winsize struct {
|
||||
Row uint16
|
||||
Col uint16
|
||||
Xpixel uint16
|
||||
Ypixel uint16
|
||||
}
|
||||
|
||||
func getWidth() uint {
|
||||
ws := &winsize{}
|
||||
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
|
||||
uintptr(syscall.Stdin),
|
||||
uintptr(syscall.TIOCGWINSZ),
|
||||
uintptr(unsafe.Pointer(ws)))
|
||||
|
||||
if int(retCode) == -1 {
|
||||
panic(errno)
|
||||
}
|
||||
return uint(ws.Col)
|
||||
}
|
Loading…
Reference in a new issue