nomino/generators.go
2025-03-14 15:01:39 -05:00

232 lines
6.8 KiB
Go

package nomino
import (
"crypto/md5"
"crypto/sha1"
"crypto/sha256"
"errors"
"fmt"
"hash"
"strconv"
"time"
"github.com/google/uuid"
"github.com/gosimple/slug"
)
// Generator is a function that returns the "random" portion of the returned filename.
// Technically, it doesn't necessarily need to be random, and could be based on time, or a counter,
// for example.
type Generator func(conf *Config) (string, error)
// WithGenerator sets the specified generator
func WithGenerator(g Generator) Option {
return func(c *Config) {
c.generator = g
}
}
// ErrMissingGenerators is returned by a multi-generator if no generators are supplied.
var ErrMissingGenerators = errors.New("no generators supplied")
func missingGen(*Config) (string, error) {
return "", ErrMissingGenerators
}
// MultiGeneratorInOrder allows the use of multiple generators. Each new invokation will use the next generator in turn.
// If none are passed, the generator will always return ErrMissingGenerators.
func MultiGeneratorInOrder(gens ...Generator) Generator {
if len(gens) == 0 {
return missingGen
}
if len(gens) == 1 {
return gens[0]
}
var idx int
return func(c *Config) (string, error) {
st, err := gens[idx](c)
idx = (idx + 1) % len(gens)
return st, err
}
}
func uuidGen(*Config) (string, error) {
u, err := uuid.NewRandom()
if err != nil {
return "", err
}
return u.String(), nil
}
// UUID generates a UUIDv4.
func UUID() Generator {
return uuidGen
}
// FileTimestamp is the default format for WithTimestamp and WithTime
const FileTimestamp string = "2006-01-02_03-05-06-0700"
// Timestamp generates a a date and time for the current time.
// It is formatted accourding to FileTimestamp
func Timestamp() Generator {
return TimestampWithFormat(FileTimestamp)
}
// TimestampWithFormat generates a date and time for the current time with the supplied format.
func TimestampWithFormat(f string) Generator {
return FormattedTime(time.Now(), FileTimestamp)
}
// Time generates a date and time for the supplied time.
// It is formatted accourding to FileTimestamp
func Time(t time.Time) Generator {
return FormattedTime(t, FileTimestamp)
}
// FormattedTime generates a date and time for the supplied time with the supplied format.
func FormattedTime(t time.Time, f string) Generator {
return func(*Config) (string, error) {
return t.Format(f), nil
}
}
// FileTimestampNoTZ is the default format for WithTimestampUTC and WithTimeUTC
const FileTimestampNoTZ string = "2006-01-02_03-05-06"
// TimestampUTC generates a date and time for the current time in UTC without a timezone in the format.
func TimestampUTC() Generator {
return TimeUTC(time.Now())
}
// TimeUTC generates a date and time for the supplied time in UTC without a timezone in the format.
func TimeUTC(t time.Time) Generator {
return FormattedTime(t.UTC(), FileTimestampNoTZ)
}
// Incremental generates a name that is a series of integers starting at 0
func Incremental() Generator {
return IncrementalWithStartAndStep(0, 1)
}
// IncrementalFormat generates a name that is a series of integers starting at 0, formatted with Printf
// This is mostly likely useful with a format like "%02d"
func IncrementalFormat(format string) Generator {
return IncrementalFormatWithStartAndStep(0, 1, format)
}
// IncrementalWithStart generates a name that is a series of integers starting at the specified number
func IncrementalWithStart(start int) Generator {
return IncrementalWithStartAndStep(start, 1)
}
// IncrementalFormatWithStart generates a name that is a series of integers starting at the specified number, formatted with Printf
func IncrementalFormatWithStart(start int, format string) Generator {
return IncrementalFormatWithStartAndStep(start, 1, format)
}
// IncrementalWithStep generates a name that is a series of integers, starting at 0, and increasing the specified number each time
func IncrementalWithStep(step int) Generator {
return IncrementalWithStartAndStep(0, step)
}
// IncrementalFormatWithStep generates a name that is a series of integers, starting at 0, and increasing the specified number each time,
// formatted with Printf
func IncrementalFormatWithStep(step int, format string) Generator {
return IncrementalFormatWithStartAndStep(0, step, format)
}
func incrementalHelper(start, step int, cb func(int) string) Generator {
next := start
return func(*Config) (string, error) {
out := cb(next)
next += step
return out, nil
}
}
// InrementalWithStartAndStep generates a name that is a series of integers, starting at the specified number,
// and increasing the specified step each time
func IncrementalWithStartAndStep(start, step int) Generator {
return incrementalHelper(start, step, strconv.Itoa)
}
// IncrementalFormatWithStartAndStep generates a name that is a series of integers, starting at the specified number,
// and increasing the specified step each time, formatted with Printf
func IncrementalFormatWithStartAndStep(start, step int, format string) Generator {
return incrementalHelper(start, step, func(i int) string {
return fmt.Sprintf(format, i)
})
}
// ErrMissingOriginal is the error returned by Slug if there is no filename
var ErrMissingOriginal = errors.New("missing original filename")
func getOriginal(c *Config) (string, error) {
if c.original == "" {
return "", ErrMissingOriginal
}
name := c.original
c.original = ""
return name, nil
}
// Slug generates a name from the original filename.
// When this is used, the original filename will be removed from the final filename.
func Slug() Generator {
return func(c *Config) (string, error) {
name, err := getOriginal(c)
return slug.Make(name), err
}
}
// SlugWithLang generates a name from the original filename, accounting for the given language.
// When this is used, the original filename will be removed from the final filename.
func SlugWithLang(lang string) Generator {
return func(c *Config) (string, error) {
name, err := getOriginal(c)
return slug.MakeLang(name, lang), err
}
}
// HashingFunc is a function that generates a hash.Hash
type HashingFunc func() hash.Hash
//go:generate stringer -type=HashType
// HashType represents a particular hashing algorithm
type HashType uint8
const (
MD5 HashType = iota + 1
SHA1
SHA256
)
// ErrInvalidHashType is returned by the Hash generator when an invalid HashType is passed
var ErrInvalidHashType = errors.New("invalid hash type")
var hashMap = map[HashType]HashingFunc{
MD5: md5.New,
SHA1: sha1.New,
SHA256: sha256.New,
}
// Hash generates a name from a hash of the filename.
// When this is used, the original filename will be removed from the final filename.
func Hash(t HashType) Generator {
f, ok := hashMap[t]
return func(c *Config) (string, error) {
if !ok {
return "", fmt.Errorf("%w: %s", ErrInvalidHashType, t)
}
name, err := getOriginal(c)
if err != nil {
return "", err
}
hs := f()
hs.Write([]byte(name))
return fmt.Sprintf("%x", hs.Sum(nil)), nil
}
}