util

package
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultLockFiles = []string{
	"go.sum",
	"package-lock.json",
	"yarn.lock",
	"pnpm-lock.yaml",
	"bun.lockb",
	"deno.lock",
	"Cargo.lock",
	"poetry.lock",
	"uv.lock",
	"Gemfile.lock",
	"composer.lock",
	"pubspec.lock",
	"mix.lock",
	"flake.lock",
	"MODULE.bazel.lock",
}

DefaultLockFiles is a list of common lockfile names used by various package managers across different ecosystems.

Functions

func AppendGitExcludeArgs

func AppendGitExcludeArgs(args []string, ignoredLockFiles []string) []string

AppendGitExcludeArgs appends git pathspec exclude arguments for each entry in ignoredLockFiles to the provided args slice and returns the updated slice. For example, if "go.sum" is in the list, it appends ":(exclude)*go.sum".

func IsLockFile

func IsLockFile(path string, ignoredLockFiles []string) bool

IsLockFile reports whether path names one of the ignored lockfiles — either at the repository root (path == name) or in any subdirectory (path ends in "/name").

func OpenInRoot

func OpenInRoot(root, path string) (*os.File, error)

OpenInRoot validates the path is within the root (if root is not empty) and opens the file.

func ReadBoundedFile

func ReadBoundedFile(ctx context.Context, root, path string, opts ReadFileOptions) (string, error)

ReadBoundedFile reads a file from the root according to the provided options. It handles line ranges, tailing, and memory bounds to ensure safe reading.

func ReadLimitedLine

func ReadLimitedLine(ctx context.Context, r *bufio.Reader, limit int) ([]byte, error)

ReadLimitedLine reads a single line from r, up to limit bytes. It respects context cancellation.

func RunGit

func RunGit(ctx context.Context, dir string, args ...string) ([]byte, error)

RunGit invokes `git <args>` in the given working directory (or the current directory when dir is empty) and returns combined stdout+stderr. Callers wrap the returned error with their own context; RunGit itself does not format the error so callers can inspect the exit code via errors.As (*exec.ExitError) where relevant.

func SafeRel

func SafeRel(base, target string) string

SafeRel returns the relative path from base to target. If an error occurs, it returns the original target path.

func SanitizeFileName

func SanitizeFileName(s string) string

SanitizeFileName replaces any non-alphanumeric character with an underscore, ensuring the string is safe for use as a filesystem component.

func SanitizeFileNameWithDefault

func SanitizeFileNameWithDefault(s, defaultName string) string

SanitizeFileNameWithDefault sanitizes s and returns defaultName if the result is empty or consists entirely of underscores.

func TruncateLines

func TruncateLines(lines []string, maxBytes int) string

TruncateLines joins lines into a single string, ensuring the result is no longer than maxBytes. It appends the suffix "\n... (truncated)" if the result is truncated. It ensures the truncation point is at a valid UTF-8 rune boundary.

func TruncateString

func TruncateString(s string, maxBytes int) string

TruncateString ensures s is no longer than maxBytes. If it is longer, it is truncated and the suffix "\n... (truncated)" is appended. The total length will not exceed maxBytes. It ensures the truncation point is at a valid UTF-8 rune boundary.

func ValidateAndRel

func ValidateAndRel(root, target string) (string, error)

ValidateAndRel validates that target is within root and returns its relative path from root.

func ValidatePathInRoot

func ValidatePathInRoot(root, path string) (string, error)

ValidatePathInRoot ensures that the given path is within the root directory. It resolves symlinks to prevent escaping the root, including cases where multiple non-existent components follow a symlink.

func WriteFileWithDirs

func WriteFileWithDirs(path string, data []byte) error

WriteFileWithDirs creates any missing parent directories before writing the file with 0o644 permissions. It uses 0o755 for any created directories.

Types

type ReadFileOptions

type ReadFileOptions struct {
	// LineStart is the 1-based line number to start reading from.
	LineStart int
	// LineEnd is the 1-based line number to stop reading at (inclusive).
	LineEnd int
	// TailLines is the number of lines to read from the end of the file.
	// If set, LineStart and LineEnd are ignored.
	TailLines int
	// MaxOutputBytes is the maximum number of bytes to return.
	MaxOutputBytes int
	// MaxLineLength is the maximum length of a single line.
	MaxLineLength int
	// MaxTailBufferBytes is the maximum memory to use for the tail buffer.
	MaxTailBufferBytes int
	// FailIfTooLarge, when true, causes ReadBoundedFile to return an error if
	// the file content (when reading whole file) exceeds MaxWholeFileReadBytes.
	FailIfTooLarge bool
	// MaxWholeFileReadBytes is the maximum bytes allowed for a whole file read.
	MaxWholeFileReadBytes int
}

ReadFileOptions configures how a file should be read by ReadBoundedFile.

type TailBuffer

type TailBuffer struct {
	// contains filtered or unexported fields
}

TailBuffer maintains a fixed number of lines with a maximum byte size.

func NewTailBuffer

func NewTailBuffer(limit, maxBytes int) *TailBuffer

NewTailBuffer creates a new TailBuffer.

func (*TailBuffer) Add

func (b *TailBuffer) Add(line []byte)

Add appends a line to the buffer, potentially removing the oldest line(s).

func (*TailBuffer) Lines

func (b *TailBuffer) Lines() [][]byte

Lines returns the lines currently in the buffer in order.

Jump to

Keyboard shortcuts

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