✨ Add extra Options to Make
This commit is contained in:
parent
9402d80704
commit
db2f12522d
3 changed files with 42 additions and 4 deletions
14
make.go
14
make.go
|
|
@ -5,12 +5,21 @@ import "fmt"
|
|||
// Make generates a random filename. The behavior can be controlled by specifying Options
|
||||
// In general, the final filename will be [prefix]_[generated_string]_[original_filename]_[suffix].[extension].
|
||||
// If the name generator returns an error (generally, it shouldn't), that will be returned instead.
|
||||
func Make(conf Config) (string, error) {
|
||||
func Make(conf Config, opts ...Option) (string, error) {
|
||||
for _, opt := range opts {
|
||||
opt(&conf)
|
||||
}
|
||||
|
||||
name, err := conf.generator(&conf)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
seperateConf(&conf)
|
||||
|
||||
return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.extension), nil
|
||||
}
|
||||
|
||||
func seperateConf(conf *Config) {
|
||||
if conf.prefix != "" {
|
||||
conf.prefix += conf.separator
|
||||
}
|
||||
|
|
@ -19,8 +28,5 @@ func Make(conf Config) (string, error) {
|
|||
}
|
||||
if conf.suffix != "" {
|
||||
conf.suffix = conf.separator + conf.suffix
|
||||
|
||||
}
|
||||
|
||||
return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.extension), nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue