Sync accepted GitHop state and prompt records
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
package ids
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/base32"
|
||||
"time"
|
||||
)
|
||||
|
||||
var encoding = base32.NewEncoding("0123456789ABCDEFGHJKMNPQRSTVWXYZ").WithPadding(base32.NoPadding)
|
||||
|
||||
func Repository() (string, error) {
|
||||
return newID("RP_")
|
||||
}
|
||||
|
||||
func Prompt() (string, error) {
|
||||
return newID("PM_")
|
||||
}
|
||||
|
||||
func newID(prefix string) (string, error) {
|
||||
value := make([]byte, 16)
|
||||
milliseconds := uint64(time.Now().UnixMilli())
|
||||
value[0] = byte(milliseconds >> 40)
|
||||
value[1] = byte(milliseconds >> 32)
|
||||
value[2] = byte(milliseconds >> 24)
|
||||
value[3] = byte(milliseconds >> 16)
|
||||
value[4] = byte(milliseconds >> 8)
|
||||
value[5] = byte(milliseconds)
|
||||
if _, err := rand.Read(value[6:]); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return prefix + encoding.EncodeToString(value), nil
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package ids
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRepository(t *testing.T) {
|
||||
first, err := Repository()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
second, err := Repository()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(first, "RP_") || len(first) != 29 {
|
||||
t.Fatalf("unexpected repository ID %q", first)
|
||||
}
|
||||
if first == second {
|
||||
t.Fatal("generated duplicate IDs")
|
||||
}
|
||||
}
|
||||
|
||||
func TestPrompt(t *testing.T) {
|
||||
id, err := Prompt()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if !strings.HasPrefix(id, "PM_") || len(id) != 29 {
|
||||
t.Fatalf("unexpected prompt ID %q", id)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user