Repository: hetznercloud/hcloud-go
Pull request #580 (2025-01-14 19:41:21)

feat(exp): add `randutil.GenerateID` helper
Handy helper used in multiple of our projects. 

codecov[bot]: ## [Codecov](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/580?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) Report
Attention: Patch coverage is `66.66667%` with `2 lines` in your changes missing coverage. Please review.
> Project coverage is 67.71%. Comparing base [(`6524772`)](https://app.codecov.io/gh/hetznercloud/hcloud-go/commit/652477298520c9995bc1703099e9e6ffe9f7c058?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) to head [(`993bf6a`)](https://app.codecov.io/gh/hetznercloud/hcloud-go/commit/993bf6a8b2ac9afae63fc6c86374ae2c795c1584?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).
> Report is 6 commits behind head on main.

| [Files with missing lines](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/580?dropdown=coverage&src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) | Patch % | Lines |
|---|---|---|
| [hcloud/exp/kit/randutil/id.go](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/580?src=pr&el=tree&filepath=hcloud%2Fexp%2Fkit%2Frandutil%2Fid.go&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud#diff-aGNsb3VkL2V4cC9raXQvcmFuZHV0aWwvaWQuZ28=) | 66.66% | [1 Missing and 1 partial :warning: ](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/580?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud) |

<details><summary>Additional details and impacted files</summary>


```diff
@@            Coverage Diff             @@
##             main     #580      +/-   ##
==========================================
- Coverage   67.85%   67.71%   -0.15%     
==========================================
  Files          48       49       +1     
  Lines        4996     5002       +6     
==========================================
- Hits         3390     3387       -3     
- Misses       1189     1195       +6     
- Partials      417      420       +3     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/hetznercloud/hcloud-go/pull/580?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=hetznercloud).


Patch:
diff --git a/hcloud/exp/kit/randutil/id.go b/hcloud/exp/kit/randutil/id.go
new file mode 100644
index 0000000..75dab5f
--- /dev/null
+++ b/hcloud/exp/kit/randutil/id.go
@@ -0,0 +1,19 @@
+package randutil
+
+import (
+	"crypto/rand"
+	"encoding/hex"
+	"fmt"
+)
+
+// GenerateID returns a hex encoded random string with a len of 8 chars similar to
+// "2873fce7".
+func GenerateID() string {
+	b := make([]byte, 4)
+	_, err := rand.Read(b)
+	if err != nil {
+		// Should never happen as of go1.24: https://github.com/golang/go/issues/66821
+		panic(fmt.Errorf("failed to generate random string: %w", err))
+	}
+	return hex.EncodeToString(b)
+}


Test patch:
diff --git a/hcloud/exp/kit/randutil/id_test.go b/hcloud/exp/kit/randutil/id_test.go
new file mode 100644
index 0000000..58116f3
--- /dev/null
+++ b/hcloud/exp/kit/randutil/id_test.go
@@ -0,0 +1,16 @@
+package randutil
+
+import (
+	"testing"
+
+	"github.com/stretchr/testify/assert"
+)
+
+func TestGenerateRandomID(t *testing.T) {
+	found1 := GenerateID()
+	found2 := GenerateID()
+
+	assert.Len(t, found1, 8)
+	assert.Len(t, found2, 8)
+	assert.NotEqual(t, found1, found2)
+}