diff --git a/pwgenerator/funcs_genopts.go b/pwgenerator/funcs_genopts.go index a79eaa3..470af19 100644 --- a/pwgenerator/funcs_genopts.go +++ b/pwgenerator/funcs_genopts.go @@ -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.