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

View file

@ -1,18 +1,18 @@
package cmd package cmd
import ( import (
"github.com/spf13/cobra"
"log" "log"
"os" "os"
"github.com/spf13/cobra"
) )
// rootCommand initializes the command-line interface application.
var rootCommand = &cobra.Command{ var rootCommand = &cobra.Command{
Use: "passGen", Use: "passGen",
Short: "PassGen", Short: "PassGen",
Long: "Password Generator", Long: "Password Generator",
Run: func(cmd *cobra.Command, args []string) { 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 package passGen
const ( const (
// LOWERCASE characters if latin alphabet
LOWERCASE string = "abcdefghijklmnopqrstuvwxyz" LOWERCASE string = "abcdefghijklmnopqrstuvwxyz"
UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" //UPPERCASE characters if latin alphabet
NUMBERS = "0123456789" UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
// NUMBERS arabic
NUMBERS = "0123456789"
) )

View file

@ -1,10 +1,13 @@
package passGen package passGen
type Options struct { 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 WithNumbers bool
} }
// SetDefaults ensure options are usable to avoid certain bugs.
func (o *Options) SetDefaults() *Options { func (o *Options) SetDefaults() *Options {
if o.Length == 0 { if o.Length == 0 {
o.Length = 6 o.Length = 6

View file

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