31 lines
728 B
Go
31 lines
728 B
Go
package artifacts
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/hex"
|
|
"fmt"
|
|
"io"
|
|
"time"
|
|
)
|
|
|
|
// NewRunID returns a run ID in format: YYYYMMDDTHHMMSSZ-xxxxxxxx.
|
|
func NewRunID() (string, error) {
|
|
return NewRunIDWith(time.Now().UTC(), rand.Reader)
|
|
}
|
|
|
|
// NewRunIDWith returns a run ID in format: YYYYMMDDTHHMMSSZ-xxxxxxxx
|
|
// using an injected timestamp and randomness source.
|
|
func NewRunIDWith(now time.Time, random io.Reader) (string, error) {
|
|
if random == nil {
|
|
random = rand.Reader
|
|
}
|
|
|
|
var suffix [4]byte
|
|
if _, err := io.ReadFull(random, suffix[:]); err != nil {
|
|
return "", fmt.Errorf("generate run id random suffix: %w", err)
|
|
}
|
|
|
|
ts := now.UTC().Format("20060102T150405Z")
|
|
return ts + "-" + hex.EncodeToString(suffix[:]), nil
|
|
}
|