nomino/make.go

33 lines
886 B
Go
Raw Permalink Normal View History

package nomino
import "fmt"
2025-03-15 15:35:40 -05:00
// 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].
2025-03-15 15:35:40 -05:00
// If the name generator returns an error (generally, it shouldn't), that error will be returned instead.
2025-03-14 21:46:30 -05:00
func Make(conf Config, opts ...Option) (string, error) {
for _, opt := range opts {
opt(&conf)
}
2025-03-13 15:36:30 -05:00
name, err := conf.generator(&conf)
if err != nil {
return "", err
}
2025-03-14 21:46:30 -05:00
seperateConf(&conf)
return fmt.Sprintf("%s%s%s%s%s", conf.prefix, name, conf.original, conf.suffix, conf.extension), nil
}
2025-03-14 21:46:30 -05:00
func seperateConf(conf *Config) {
2025-03-11 16:42:39 -05:00
if conf.prefix != "" {
2025-03-14 13:47:44 -05:00
conf.prefix += conf.separator
2025-03-11 16:42:39 -05:00
}
if conf.original != "" {
conf.original = conf.separator + conf.original
}
if conf.suffix != "" {
conf.suffix = conf.separator + conf.suffix
}
}