utils

package
v0.24.10 Latest Latest
Warning

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

Go to latest
Published: Sep 23, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package utils holds small, dependency-free helpers shared across pkg/ and internal/. It must never import internal/ (pkg/ stays internal-free), so it carries no 2dph domain types.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DoJSON

func DoJSON(ctx context.Context, client *http.Client, method, url string, body, dest any, opts ...func(*http.Request)) error

DoJSON performs an HTTP request with an optional JSON body, checks for a 2xx status, and decodes the JSON response into dest (a pointer). A nil body sends no payload; a nil dest skips decoding. opts let callers set headers (e.g. Basic auth). The error carries the HTTP status and a short body snippet so callers can surface remote errors verbatim.

func GetJSON

func GetJSON(ctx context.Context, client *http.Client, url string, dest any, opts ...func(*http.Request)) error

GetJSON is DoJSON with the GET method and no request body.

func Or

func Or(s, def string) string

Or returns def when s is empty, otherwise s.

func Root

func Root() string

Root locates the 2dph project root: the KB_ROOT env var, or walking up from the working directory for a `.git` or `var` marker. It returns "." if nothing is found. This is the single source of truth for repo-root resolution so pkg/repo and internal/chat share one implementation.

func SafeSegment

func SafeSegment(s string) string

SafeSegment turns an arbitrary id/name into a safe single path segment: path separators and control characters are replaced, runs collapsed, and the result is truncated. It never returns an empty string for non-empty input, so callers can use it directly as a directory or file component.

func Snippet

func Snippet(s string, n int) string

Snippet truncates s to at most n runes-equivalent bytes, appending an ellipsis when it is cut. It is used for short error contexts.

Types

This section is empty.

Jump to

Keyboard shortcuts

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