| 
									
										
										
										
											2025-03-07 17:00:38 -06:00
										 |  |  | package nomino | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2025-03-10 14:25:00 -05:00
										 |  |  | 	"errors" | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-07 17:00:38 -06:00
										 |  |  | 	"github.com/google/uuid" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // 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) | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // WithGenerator sets the specified generator | 
					
						
							|  |  |  | func WithGenerator(g Generator) Option { | 
					
						
							| 
									
										
										
										
											2025-03-10 14:52:50 -05:00
										 |  |  | 	return func(c *Config) { | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | 		c.generator = g | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2025-03-07 17:00:38 -06:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 14:25:00 -05:00
										 |  |  | // ErrMissingGenerators is returned by a multi-generator if no generators are supplied. | 
					
						
							|  |  |  | var ErrMissingGenerators = errors.New("no generators supplied") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func missingGen() (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() (string, error) { | 
					
						
							|  |  |  | 		st, err := gens[idx]() | 
					
						
							|  |  |  | 		idx = (idx + 1) % len(gens) | 
					
						
							|  |  |  | 		return st, err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-07 17:00:38 -06:00
										 |  |  | func uuidGen() (string, error) { | 
					
						
							|  |  |  | 	u, err := uuid.NewRandom() | 
					
						
							|  |  |  | 	if err != nil { | 
					
						
							|  |  |  | 		return "", err | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	return u.String(), nil | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // UUID generates a UUIDv4. | 
					
						
							|  |  |  | func UUID() Generator { | 
					
						
							|  |  |  | 	return uuidGen | 
					
						
							| 
									
										
										
										
											2025-03-07 17:00:38 -06:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:46:13 -05:00
										 |  |  | // FileTimestamp is the default format for WithTimestamp and WithTime | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | const FileTimestamp string = "2006-01-02_03-05-06-0700" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // Timestamp generates a a date and time for the current time. | 
					
						
							|  |  |  | // It is formatted accourding to FileTimestamp | 
					
						
							|  |  |  | func Timestamp() Generator { | 
					
						
							|  |  |  | 	return TimestampWithFormat(FileTimestamp) | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // TimestampWithFormat generates a date and time for the current time with the supplied format. | 
					
						
							|  |  |  | func TimestampWithFormat(f string) Generator { | 
					
						
							|  |  |  | 	return FormattedTime(time.Now(), FileTimestamp) | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // 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) | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // 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 | 
					
						
							| 
									
										
										
										
											2025-03-10 11:47:01 -05:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2025-03-10 11:52:55 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:46:13 -05:00
										 |  |  | // FileTimestamp is the default format for WithTimestampUTC and WithTimeUTC | 
					
						
							| 
									
										
										
										
											2025-03-10 11:52:55 -05:00
										 |  |  | const FileTimestampNoTZ string = "2006-01-02_03-05-06" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // 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()) | 
					
						
							| 
									
										
										
										
											2025-03-10 11:52:55 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-10 13:48:11 -05:00
										 |  |  | // 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) | 
					
						
							| 
									
										
										
										
											2025-03-10 11:52:55 -05:00
										 |  |  | } |