Minor refactor

* Move namegen to seperate file
* Simplify Makefile
This commit is contained in:
2023-09-19 22:47:09 -04:00
parent acc9e87f2c
commit 9200bd16db
4 changed files with 48 additions and 42 deletions

35
namegen.go Normal file
View 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))
}