Compare commits

...

1 Commits

Author SHA1 Message Date
brent s. cb35a91855
Adding MustGenerate, GenerateOnce, and MustGenerateOnce.
These are largely just convenience functions/wrappers.
2022-07-07 06:49:18 -04:00
1 changed files with 43 additions and 0 deletions

View File

@ -46,6 +46,49 @@ func (o *GenOpts) Generate() (passwords []string, err error) {
return
}

// MustGenerate is like Generate, but will panic on error instead of returning.
func (o *GenOpts) MustGenerate() (passwords []string) {

var err error

if passwords, err = o.Generate(); err != nil {
panic(err)
}

return
}

/*
GenOnce generates a *single* password from a GenOpts.

This method is particularly useful/convenient if GenOpts.Count is 1
and you don't want to have to assign to a slice and then get the first index.
*/
func (o GenOpts) GenerateOnce() (password string, err error) {

var passwds []string

o.Count = 1
if passwds, err = o.Generate(); err != nil {
return
}
password = passwds[0]

return
}

// MustGenerateOnce is like GenerateOnce, but will panic on error instead of returning.
func (o *GenOpts) MustGenerateOnce() (password string) {

var err error

if password, err = o.GenerateOnce(); err != nil {
panic(err)
}

return
}

/*
GenerateCollection returns a PwCollection instead of a slice of password text.