nomino/generators.go

60 lines
1 KiB
Go
Raw Normal View History

2025-03-07 17:00:38 -06:00
package nomino
import (
2025-03-10 11:47:01 -05:00
"time"
2025-03-07 17:00:38 -06:00
"github.com/google/uuid"
)
2025-03-10 11:47:01 -05:00
type generator func() (string, error)
2025-03-07 17:00:38 -06:00
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)
}
}
2025-03-10 11:47:01 -05:00
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
})
}
}
2025-03-10 11:52:55 -05:00
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)
}