mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-09-05 18:40:59 +00:00
If that happens so much will go wrong that there is no point in continuing to do anything. It simplifies the requirements of the caller: it may be a function that is assumed to never error. <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - other - [PR](https://code.forgejo.org/forgejo/runner/pulls/853): <!--number 853 --><!--line 0 --><!--description Y2hvcmU6IHBhbmljIGlmIGEgcmFuZG9tIG5hbWUgY2Fubm90IGJlIGNyZWF0ZWQ=-->chore: panic if a random name cannot be created<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/853 Reviewed-by: Mathieu Fenniak <mfenniak@noreply.code.forgejo.org> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
25 lines
478 B
Go
25 lines
478 B
Go
// Copyright 2025 The Forgejo Authors
|
|
// SPDX-License-Identifier: MIT
|
|
package common
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
)
|
|
|
|
func randName(size int) (string, error) {
|
|
randBytes := make([]byte, size)
|
|
if _, err := rand.Read(randBytes); err != nil {
|
|
return "", err
|
|
}
|
|
return hex.EncodeToString(randBytes), nil
|
|
}
|
|
|
|
func MustRandName(size int) string {
|
|
name, err := randName(size)
|
|
if err != nil {
|
|
panic(fmt.Errorf("RandName(%d): %v", size, err))
|
|
}
|
|
return name
|
|
}
|