utilio

package
v0.23.0 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckRedirectNoHTTPSDowngrade added in v0.2.0

func CheckRedirectNoHTTPSDowngrade(req *http.Request, via []*http.Request) error

CheckRedirectNoHTTPSDowngrade rejects redirects from an HTTPS source to an insecure destination while allowing redirects that preserve or improve the transport scheme.

func CleanDir

func CleanDir(path string) (retErr error)

CleanDir removes everything in a directory, but not the directory itself.

func ExtractTar added in v0.2.0

func ExtractTar(body io.Reader, destDir string) error

ExtractTar extracts a tar or gzip-compressed tar stream into destDir. Only directories and regular files are accepted.

func InstallFile

func InstallFile(filename string, r io.Reader, perm os.FileMode) error

InstallFile writes the content from the provided reader to a local file with specified permissions. It limits the size of the content to 1 GiB and returns an error if the limit is exceeded. It ensures that the target directory exists and handles the file writing atomically.

NOTE: we assume the filename is trusted and cleaned without path traversal characters.

func InstallFileWithLimitedSize added in v0.2.3

func InstallFileWithLimitedSize(filename string, r io.Reader, perm os.FileMode, maxBytes int64) error

InstallFileWithLimitedSize streams content to a local file with limited size and specified permissions. It ensures that the target directory exists and handles the file writing atomically.

NOTE: we assume the filename is trusted and cleaned without path traversal characters.

func IsDirEmpty

func IsDirEmpty(dir string) (bool, error)

IsDirEmpty reports whether dir is empty or does not exist.

func IsExecutable

func IsExecutable(filePath string) bool

func NearestExistingDir added in v0.9.0

func NearestExistingDir(stat func(string) (fs.FileInfo, error), dir string) string

NearestExistingDir returns dir when it is an existing directory, and otherwise the closest ancestor that is.

It exists so a caller can ask whether a path could be created without creating it. Probing the path itself answers a different question: a directory the agent has not made yet is reported as unusable even when its parent is perfectly writable, which is the normal state of an install directory before the first bootstrap.

func ProbeRemoteHTTPObject added in v0.1.20

func ProbeRemoteHTTPObject(ctx context.Context, source string) error

ProbeRemoteHTTPObject checks that an HTTP artifact object is reachable without downloading the full object. It first tries HEAD, then falls back to a ranged GET for servers that do not support or incorrectly reject HEAD.

func ProbeWritableDir added in v0.1.20

func ProbeWritableDir(dir string) error

ProbeWritableDir verifies that dir accepts file creation and removal without leaving durable state behind.

The directory must already exist. Use NearestExistingDir first to check a path the caller intends to create.

func RedactHTTPError added in v0.2.0

func RedactHTTPError(err error) error

RedactHTTPError removes query parameters from URL errors returned by the Go HTTP client while preserving the underlying transport error.

func RedactURLQuery added in v0.2.0

func RedactURLQuery(rawURL string) string

RedactURLQuery removes query parameters from rawURL before it is logged or included in an error message.

func RemoveAllIfExists added in v0.23.0

func RemoveAllIfExists(log *slog.Logger, path string) error

RemoveAllIfExists removes a path and everything below it. Removal failures are logged and returned so callers keep ownership of what is left.

func RemoveFileIfExists added in v0.23.0

func RemoveFileIfExists(log *slog.Logger, path string) error

RemoveFileIfExists removes a file, ignoring absence and logging and returning any other failure.

func URLWithoutQuery added in v0.2.0

func URLWithoutQuery(rawURL string) string

URLWithoutQuery removes query parameters from rawURL. It returns a redacted placeholder when rawURL cannot be parsed safely.

func UpdateSymlink(linkPath, targetPath string) error

UpdateSymlink atomically updates linkPath to point at targetPath.

func WriteFile

func WriteFile(filename string, content []byte, perm os.FileMode) error

WriteFile writes the provided content to a local file with specified permissions. It ensures that the target directory exists and handles the file writing atomically. The temp file is created in the same directory as the destination to preserve the correct SELinux label (avoiding the user_tmp_t label from /tmp).

NOTE: we assume the filename is trusted and cleaned without path traversal characters.

func WriteFileIfChanged added in v0.9.0

func WriteFileIfChanged(filename string, content []byte, perm os.FileMode) (bool, error)

WriteFileIfChanged writes content like WriteFile and reports whether the content differed beforehand.

It exists so a caller that reapplies configuration can tell an actual change from a no-op. Writing unconditionally would be correct on disk but would make every reapply look like a change, so callers that act on one, such as restarting the service that reads the file, would act every time.

Only content is compared. WriteFile preserves an existing file's permissions rather than resetting them to perm, so a drifted mode cannot be corrected here; reporting it as changed would make this return true on every call and restart the reader forever. perm therefore applies only when the file is being created.

Types

type TarFile

type TarFile struct {
	Name string
	Size int64
	Body io.Reader
}

type TarFileSeq added in v0.1.21

type TarFileSeq = iter.Seq2[*TarFile, error]

func DecompressTarGz added in v0.1.21

func DecompressTarGz(body io.Reader) TarFileSeq

DecompressTarGz returns an iterator that yields the files contained in a gzip-compressed tar stream.

func DecompressTarGzFromRemote

func DecompressTarGzFromRemote(ctx context.Context, url string) TarFileSeq

DecompressTarGzFromRemote returns an iterator that yields the files contained in a .tar.gz file located at the given HTTP(S) URL.

Jump to

Keyboard shortcuts

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