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",
Long: "Create a secure password",
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()
display(password)
}}

View file

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

View file

@ -9,8 +9,8 @@ import (
)
func TestGeneratePasswordWithGivenCharacterNumber(t *testing.T) {
opts := passgen.Options{
Length: rand.Intn(12),
opts := &passgen.Options{
Length: rand.Intn(12), // nolint
}
pg := passgen.NewPasswordGenerator(opts)
@ -20,7 +20,7 @@ func TestGeneratePasswordWithGivenCharacterNumber(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 {
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) {
opts := passgen.Options{
opts := &passgen.Options{
WithNumbers: true,
}
pg := passgen.NewPasswordGenerator(opts)