Adding MustGenerate, GenerateOnce, and MustGenerateOnce.

These are largely just convenience functions/wrappers.
This commit is contained in:
brent s. 2022-07-07 06:49:18 -04:00
parent b12131b661
commit cb35a91855
Signed by: bts
GPG Key ID: 8C004C2F93481F6B

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.