♻️ Export Generator, and switch to WithGenerator Option

This commit is contained in:
Dan Jones 2025-03-10 13:48:11 -05:00
commit ce5f823d64
3 changed files with 53 additions and 50 deletions

View file

@ -6,10 +6,16 @@ import (
"github.com/google/uuid"
)
type generator func() (string, error)
// 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() (string, error)
func setGenerator(c *config, g generator) {
c.generator = g
// WithGenerator sets the specified generator
func WithGenerator(g Generator) Option {
return func(c *config) {
c.generator = g
}
}
func uuidGen() (string, error) {
@ -20,51 +26,47 @@ func uuidGen() (string, error) {
return u.String(), nil
}
// WithUUID sets a generator that creates a UUIDv4
func WithUUID() Option {
return func(c *config) {
setGenerator(c, uuidGen)
}
// 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"
// WithTimestamp sets a generator that creates a date and time for the current time.
// The format is FileStamp
func WithTimestamp() Option {
return WithTimestampFormat(FileTimestamp)
// Timestamp generates a a date and time for the current time.
// It is formatted accourding to FileTimestamp
func Timestamp() Generator {
return TimestampWithFormat(FileTimestamp)
}
// WithTimestampFormat sets a generator the creates a date and time for the current time with the supplied format.
func WithTimestampFormat(f string) Option {
return WithFormattedTime(time.Now(), f)
// TimestampWithFormat generates a date and time for the current time with the supplied format.
func TimestampWithFormat(f string) Generator {
return FormattedTime(time.Now(), FileTimestamp)
}
// WithTime sets a generator that creates a date and time for the supplied time.
// The format is FileStamp
func WithTime(t time.Time) Option {
return WithFormattedTime(t, 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)
}
// WithFormattedTime sets a generator that creates a date and time for the supplied time with the supplied format.
func WithFormattedTime(t time.Time, f string) Option {
return func(c *config) {
setGenerator(c, func() (string, error) {
return t.Format(f), nil
})
// FormattedTime generates a date and time for the supplied time with the supplied format.
func FormattedTime(t time.Time, f string) Generator {
return func() (string, error) {
return t.Format(f), nil
}
}
// FileTimestamp is the default format for WithTimestampUTC and WithTimeUTC
const FileTimestampNoTZ string = "2006-01-02_03-05-06"
// WithTimestampUTC sets a generator the creates a date and time for the current time in UTC without a timezone in the format.
func WithTimestampUTC() Option {
return WithTimeUTC(time.Now())
// 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())
}
// WithTimeUTC sets a generate that creates a date and time for the supplied time in UTC without a timezone in the format.
func WithTimeUTC(t time.Time) Option {
return WithFormattedTime(t.UTC(), FileTimestampNoTZ)
// 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)
}