util

package
v0.2.0-alpha.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package util contains small, cross-layer helpers with no business ownership.

Keep package contents generic and dependency-light. Helpers that belong to a specific subsystem should live with that subsystem instead of being added here.

Index

Constants

View Source
const (

	// PublicIDLen is the canonical text length.
	PublicIDLen = 20
)

A public ID is the only handle a server entity is allowed to show the outside world. It is 96 bits of crypto-random data rendered as 20 lowercase base32 characters, and that text form is also what the database stores:

gsyt7at6cjfr33d73mta

The alphabet is deliberately narrower than base64url. A public ID reaches Kubernetes Job names, object-storage paths on case-insensitive filesystems, URLs, and people retyping it, and base32 is the widest encoding that survives all four unchanged. Storing the text rather than the raw bytes costs 8 bytes per value and makes every direct database query readable; the raw-byte form died with that trade. See docs/design/entity-identity.md §4.2.

Variables

This section is empty.

Functions

func CanonicalPublicID

func CanonicalPublicID(s string) (string, bool)

CanonicalPublicID reports whether s is a public ID and returns its one canonical text form.

Input is accepted in either case, so an ID retyped from a title-cased document still resolves. The text is decoded and re-encoded to prove it was canonical apart from case: 20 base32 characters carry 100 bits, and the 4 bits past the value must be zero. Without that check, several texts would name one row.

func ClipRunes

func ClipRunes(s string, maxRunes int) string

ClipRunes returns at most the first maxRunes runes of s without adding a suffix. If maxRunes is non-positive, it returns the empty string.

func FormatDuration

func FormatDuration(d time.Duration) string

FormatDuration formats a duration in a compact human-readable way.

func FormatMinute

func FormatMinute(t time.Time) string

FormatMinute formats an instant as YYYY-MM-DD HH:MM in local time.

The reader is a person — a tool result or a listing — so local time is the useful rendering. Stored and transported instants stay UTC.

func NewPublicID

func NewPublicID() (string, error)

NewPublicID returns a fresh public ID in canonical text form.

It returns an error rather than panicking on entropy failure: that must surface as one failed create, not as a process abort inside a request.

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to v. Useful for filling optional pointer fields.

func ResolvePath

func ResolvePath(root, userPath string) (string, error)

ResolvePath resolves a user-supplied path relative to root, ensuring the result stays under root. Returns the absolute, cleaned path. Includes a Windows-safe prefix check (filepath.Rel can return an absolute path when roots differ on different drives). Does NOT stat the path — callers handle existence and type checks.

func ResolveWorkspaceRoot

func ResolveWorkspaceRoot(dir string) (string, error)

ResolveWorkspaceRoot resolves and absolutizes a workspace root directory. If dir is empty, the current working directory is used.

func TruncateRunes

func TruncateRunes(s string, maxRunes int) string

TruncateRunes truncates s to at most maxRunes runes and appends an ellipsis when truncation happens. If maxRunes is non-positive, it returns the empty string.

func WithEnvVar

func WithEnvVar(envKey, value string, fn func() error) error

WithEnvVar sets envKey to value for the duration of fn, then restores the previous process env state.

func WorkerJobNameForTaskRun

func WorkerJobNameForTaskRun(taskRunID string) string

WorkerJobNameForTaskRun returns a DNS-1123-compatible worker Job name for a task run id.

func WorkerJobNameForTaskRunAt

func WorkerJobNameForTaskRunAt(taskRunID string, now time.Time) string

WorkerJobNameForTaskRunAt returns a DNS-1123-compatible worker Job name for a task run id and timestamp. Total length is kept <= 63 characters.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL