nomino/make.go
2025-03-15 15:35:40 -05:00

32 lines
886 B
Go

package nomino
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 error will be returned instead.
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
}
if conf.original != "" {
conf.original = conf.separator + conf.original
}
if conf.suffix != "" {
conf.suffix = conf.separator + conf.suffix
}
}