You've already forked collage-maker
Minor refactor
* Move namegen to seperate file * Simplify Makefile
This commit is contained in:
35
namegen.go
Normal file
35
namegen.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type nameGen struct {
|
||||
prefix string
|
||||
counter atomic.Uint64
|
||||
}
|
||||
|
||||
func NewNameGen() *nameGen {
|
||||
currentTime := time.Now().Unix()
|
||||
randBytes := make([]byte, 8)
|
||||
_, err := rand.Read(randBytes)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
alpha := []byte("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
|
||||
uniqRunID := ""
|
||||
for _, b := range randBytes {
|
||||
uniqRunID += string(alpha[int(b)%len(alpha)])
|
||||
}
|
||||
return &nameGen{
|
||||
prefix: fmt.Sprintf("%d-%s", currentTime, uniqRunID),
|
||||
counter: atomic.Uint64{},
|
||||
}
|
||||
}
|
||||
|
||||
func (n *nameGen) Next() string {
|
||||
return fmt.Sprintf("%s-%d", n.prefix, n.counter.Add(1))
|
||||
}
|
||||
Reference in New Issue
Block a user