1
0
Fork 0
mirror of https://github.com/Luzifer/password.git synced 2024-11-08 17:30:10 +00:00
password/cmdGet.go
Knut Ahlers f9c435af1a
Add commandline flag to generate multiple passwords
Signed-off-by: Knut Ahlers <knut@ahlers.me>
2017-10-31 12:57:19 +01:00

69 lines
1.8 KiB
Go

package main
import (
"encoding/json"
"fmt"
"os"
"github.com/Luzifer/password/hasher"
pwd "github.com/Luzifer/password/lib"
"github.com/spf13/cobra"
)
func getCmdGet() *cobra.Command {
cmd := cobra.Command{
Use: "get",
Short: "generate and return a secure random password",
Run: actionCmdGet,
}
cmd.Flags().BoolVarP(&flags.CLI.JSON, "json", "j", false, "return output in JSON format")
cmd.Flags().IntVarP(&flags.CLI.Length, "length", "l", 20, "length of the generated password")
cmd.Flags().IntVarP(&flags.CLI.Num, "number", "n", 1, "number of passwords to generate")
cmd.Flags().BoolVarP(&flags.CLI.SpecialCharacters, "special", "s", false, "use special characters in your password")
cmd.Flags().BoolVarP(&flags.CLI.XKCD, "xkcd", "x", false, "use XKCD style password")
cmd.Flags().BoolVarP(&flags.CLI.PrependDate, "date", "d", true, "prepend current date to XKCD style passwords")
return &cmd
}
func actionCmdGet(cmd *cobra.Command, args []string) {
var (
password string
err error
)
for i := 0; i < flags.CLI.Num; i++ {
if flags.CLI.XKCD {
password, err = pwd.DefaultXKCD.GeneratePassword(flags.CLI.Length, flags.CLI.PrependDate)
} else {
password, err = pwd.NewSecurePassword().GeneratePassword(flags.CLI.Length, flags.CLI.SpecialCharacters)
}
if err != nil {
switch {
case err == pwd.ErrLengthTooLow:
fmt.Println("The password has to be more than 4 characters long to meet the security considerations")
default:
fmt.Println("An unknown error occured")
}
os.Exit(1)
}
if !flags.CLI.JSON {
fmt.Println(password)
continue
}
hashes, err := hasher.GetHashMap(password)
if err != nil {
fmt.Printf("Unable to generate hashes: %s", err)
os.Exit(1)
}
hashes["password"] = password
json.NewEncoder(os.Stdout).Encode(hashes)
}
}