nomino/generators.go

59 lines
1 KiB
Go

package nomino
import (
"time"
"github.com/google/uuid"
)
type generator func() (string, error)
func setGenerator(c *config, g generator) {
c.generator = g
}
func uuidGen() (string, error) {
u, err := uuid.NewRandom()
if err != nil {
return "", err
}
return u.String(), nil
}
func WithUUID() Option {
return func(c *config) {
setGenerator(c, uuidGen)
}
}
const FileTimestamp string = "2006-01-02_03-05-06-0700"
func WithTimestamp() Option {
return WithTimestampFormat(FileTimestamp)
}
func WithTimestampFormat(f string) Option {
return WithFormattedTime(time.Now(), f)
}
func WithTime(t time.Time) Option {
return WithFormattedTime(t, FileTimestamp)
}
func WithFormattedTime(t time.Time, f string) Option {
return func(c *config) {
setGenerator(c, func() (string, error) {
return t.Format(f), nil
})
}
}
const FileTimestampNoTZ string = "2006-01-02_03-05-06"
func WithTimestampUTC() Option {
return WithTimeUTC(time.Now())
}
func WithTimeUTC(t time.Time) Option {
return WithFormattedTime(t.UTC(), FileTimestampNoTZ)
}