Add time-based generators

This commit is contained in:
Dan Jones 2025-03-10 11:47:01 -05:00
commit ee627547a8
3 changed files with 45 additions and 2 deletions

View file

@ -1,9 +1,13 @@
package nomino
import (
"time"
"github.com/google/uuid"
)
type generator func() (string, error)
func setGenerator(c *config, g generator) {
c.generator = g
}
@ -21,3 +25,25 @@ func WithUUID() Option {
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
})
}
}