From c1ce0fdf65573a4b51262da869cf59aecd587cd2 Mon Sep 17 00:00:00 2001 From: Ondrej Sika Date: Mon, 8 Apr 2024 11:18:34 +0200 Subject: [PATCH] refactor: Creare RandomPassword in random_utils and use in `slu random password` --- cmd/random/password/password.go | 56 ++----------------- utils/random_utils/random_password_utils.go | 60 +++++++++++++++++++++ 2 files changed, 64 insertions(+), 52 deletions(-) create mode 100644 utils/random_utils/random_password_utils.go diff --git a/cmd/random/password/password.go b/cmd/random/password/password.go index 9f7e0cce..80df4334 100644 --- a/cmd/random/password/password.go +++ b/cmd/random/password/password.go @@ -3,8 +3,6 @@ package password import ( "fmt" "log" - "strings" - "unicode" random_cmd "github.com/sikalabs/slu/cmd/random" "github.com/sikalabs/slu/utils/random_utils" @@ -18,60 +16,14 @@ var Cmd = &cobra.Command{ Aliases: []string{"pwd", "passwd", "pass"}, Args: cobra.NoArgs, Run: func(c *cobra.Command, args []string) { - i := 0 - for { - s := random_utils.RandomString(16, random_utils.ALL) - if containsLowercase(s) && containsUpercase(s) && containsDigit(s) { - fmt.Println(addUnderscores(s)) - break - } - if i > 20 { - log.Fatalln("Cannot generate password") - } + password, err := random_utils.RandomPassword() + if err != nil { + log.Fatal(err) } + fmt.Println(password) }, } func init() { random_cmd.Cmd.AddCommand(Cmd) } - -func containsLowercase(s string) bool { - for _, r := range s { - if unicode.IsLower(r) { - return true - } - } - return false -} - -func containsUpercase(s string) bool { - for _, r := range s { - if unicode.IsLower(r) { - return true - } - } - return false -} - -func containsDigit(s string) bool { - for _, r := range s { - if unicode.IsDigit(r) { - return true - } - } - return false -} - -func addUnderscores(input string) string { - var blocks []string - for i := 0; i < len(input); i += 4 { - end := i + 4 - if end > len(input) { - end = len(input) - } - blocks = append(blocks, input[i:end]) - } - - return strings.Join(blocks, "_") -} diff --git a/utils/random_utils/random_password_utils.go b/utils/random_utils/random_password_utils.go new file mode 100644 index 00000000..9bd7ea3e --- /dev/null +++ b/utils/random_utils/random_password_utils.go @@ -0,0 +1,60 @@ +package random_utils + +import ( + "fmt" + "strings" + "unicode" +) + +func RandomPassword() (string, error) { + i := 0 + for { + s := RandomString(16, ALL) + if containsLowercase(s) && containsUpercase(s) && containsDigit(s) { + return addUnderscores(s), nil + } + if i > 20 { + return "", fmt.Errorf("cannot generate password") + } + } +} + +func containsLowercase(s string) bool { + for _, r := range s { + if unicode.IsLower(r) { + return true + } + } + return false +} + +func containsUpercase(s string) bool { + for _, r := range s { + if unicode.IsLower(r) { + return true + } + } + return false +} + +func containsDigit(s string) bool { + for _, r := range s { + if unicode.IsDigit(r) { + return true + } + } + return false +} + +func addUnderscores(input string) string { + var blocks []string + for i := 0; i < len(input); i += 4 { + end := i + 4 + if end > len(input) { + end = len(input) + } + blocks = append(blocks, input[i:end]) + } + + return strings.Join(blocks, "_") +}