mirror of
https://github.com/apricote/hcloud-upload-image.git
synced 2026-02-07 02:07:05 +00:00
feat: initial library code
This commit is contained in:
parent
b331ddba81
commit
4f57df5b66
10 changed files with 575 additions and 0 deletions
16
util/randomid/randomid.go
Normal file
16
util/randomid/randomid.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package randomid
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
func Generate() (string, error) {
|
||||
b := make([]byte, 4)
|
||||
_, err := rand.Read(b)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("failed to generate random string: %w", err)
|
||||
}
|
||||
return hex.EncodeToString(b), nil
|
||||
}
|
||||
18
util/randomid/randomid_test.go
Normal file
18
util/randomid/randomid_test.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package randomid
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestGenerateRandomID(t *testing.T) {
|
||||
found1, err := Generate()
|
||||
assert.NoError(t, err)
|
||||
found2, err := Generate()
|
||||
assert.NoError(t, err)
|
||||
|
||||
assert.Len(t, found1, 8)
|
||||
assert.Len(t, found2, 8)
|
||||
assert.NotEqual(t, found1, found2)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue