refactor: use pointer instead

This commit is contained in:
Ruidy 2022-01-21 16:51:01 -04:00
parent 70aa7b03f4
commit 84bc9ee259
3 changed files with 9 additions and 8 deletions

View file

@ -20,7 +20,8 @@ var newPasswordCommand = &cobra.Command{Use: "new",
Short: "New Password", Short: "New Password",
Long: "Create a secure password", Long: "Create a secure password",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
pg := passgen.NewPasswordGenerator(passgen.Options{Length: Length, WithNumbers: WithNumbers}) opts := passgen.Options{Length: Length, WithNumbers: WithNumbers}
pg := passgen.NewPasswordGenerator(opts.SetDefaults())
password := pg.NewPassword() password := pg.NewPassword()
display(password) display(password)
}} }}

View file

@ -8,13 +8,13 @@ import (
// PasswordGenerator handles passwords creation. // 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. // 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()
pg.characters = pg.shuffleCharacters(opts.WithNumbers) pg.characters = pg.shuffleCharacters(opts.WithNumbers)
return pg return pg
} }

View file

@ -9,8 +9,8 @@ import (
) )
func TestGeneratePasswordWithGivenCharacterNumber(t *testing.T) { func TestGeneratePasswordWithGivenCharacterNumber(t *testing.T) {
opts := passgen.Options{ opts := &passgen.Options{
Length: rand.Intn(12), Length: rand.Intn(12), // nolint
} }
pg := passgen.NewPasswordGenerator(opts) pg := passgen.NewPasswordGenerator(opts)
@ -20,7 +20,7 @@ func TestGeneratePasswordWithGivenCharacterNumber(t *testing.T) {
} }
func TestGeneratePasswordWithDefaultCharacterNumber(t *testing.T) { func TestGeneratePasswordWithDefaultCharacterNumber(t *testing.T) {
pg := passgen.NewPasswordGenerator(passgen.Options{}) pg := passgen.NewPasswordGenerator(&passgen.Options{})
if password := pg.NewPassword(); len(password) != 6 { if password := pg.NewPassword(); len(password) != 6 {
t.Errorf("Expected a password to be %d characters long, got %d", 6, len(password)) t.Errorf("Expected a password to be %d characters long, got %d", 6, len(password))
@ -28,7 +28,7 @@ func TestGeneratePasswordWithDefaultCharacterNumber(t *testing.T) {
} }
func TestGeneratePasswordWithLettersAndNumbers(t *testing.T) { func TestGeneratePasswordWithLettersAndNumbers(t *testing.T) {
opts := passgen.Options{ opts := &passgen.Options{
WithNumbers: true, WithNumbers: true,
} }
pg := passgen.NewPasswordGenerator(opts) pg := passgen.NewPasswordGenerator(opts)