docs: add some

This commit is contained in:
Ruidy Nemausat 2021-10-20 17:10:03 +02:00
parent 58913e6c87
commit 530781fb17
No known key found for this signature in database
GPG key ID: A367830814E7096E
6 changed files with 25 additions and 8 deletions

View file

@ -1,4 +1,8 @@
package cmd
var Length int
var WithNumbers bool
var (
// The Length of the generated password.
Length int
// WithNumbers is set to true if the new password must contain numbers.
WithNumbers bool
)

View file

@ -15,6 +15,7 @@ func init() {
rootCommand.AddCommand(newPasswordCommand)
}
// newPasswordCommand creates a new password. It takes into account the supplied flags.
var newPasswordCommand = &cobra.Command{Use: "new",
Short: "New Password",
Long: "Create a secure password",
@ -24,6 +25,7 @@ var newPasswordCommand = &cobra.Command{Use: "new",
display(password)
}}
// display presents information to standard output.
func display(str string) {
red := color.New(color.FgGreen).Add(color.Bold)
whiteBackground := red.Add(color.BgWhite)

View file

@ -1,18 +1,18 @@
package cmd
import (
"github.com/spf13/cobra"
"log"
"os"
"github.com/spf13/cobra"
)
// rootCommand initializes the command-line interface application.
var rootCommand = &cobra.Command{
Use: "passGen",
Short: "PassGen",
Long: "Password Generator",
Run: func(cmd *cobra.Command, args []string) {
log.Println("started")
display("** passGen v0.0.1 **\nUse passGen -h for more information")
},
}

View file

@ -1,7 +1,10 @@
package passGen
const (
// LOWERCASE characters if latin alphabet
LOWERCASE string = "abcdefghijklmnopqrstuvwxyz"
UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
NUMBERS = "0123456789"
//UPPERCASE characters if latin alphabet
UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// NUMBERS arabic
NUMBERS = "0123456789"
)

View file

@ -1,10 +1,13 @@
package passGen
type Options struct {
Length int
// Length of the new password
Length int
// WithNumbers is set to true if the password must contain numbers.
WithNumbers bool
}
// SetDefaults ensure options are usable to avoid certain bugs.
func (o *Options) SetDefaults() *Options {
if o.Length == 0 {
o.Length = 6

View file

@ -4,11 +4,13 @@ import (
"math/rand"
)
// PasswordGenerator handles passwords creation.
type PasswordGenerator struct {
characters []rune
options Options
}
// NewPasswordGenerator returns a valid PasswordGenerator given the specified Options.
func NewPasswordGenerator(opts Options) *PasswordGenerator {
pg := &PasswordGenerator{}
pg.options = *opts.SetDefaults()
@ -16,10 +18,12 @@ func NewPasswordGenerator(opts Options) *PasswordGenerator {
return pg
}
// NewPassword returns a pseudo random string.
func (p PasswordGenerator) NewPassword() string {
return p.generatePassword(p.options.Length)
}
// generatePassword builds the new password.
func (p PasswordGenerator) generatePassword(length int) string {
var res string
for i := 0; i < length; i++ {
@ -28,6 +32,7 @@ func (p PasswordGenerator) generatePassword(length int) string {
return res
}
// shuffleCharacters randomizes the characters.
func (p PasswordGenerator) shuffleCharacters(withNumbers bool) []rune {
letters := []rune(UPPERCASE + LOWERCASE)
if withNumbers {