osutil

package
v1.23.13 Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const (
	PermissionDirectory      os.FileMode = 0755
	PermissionExecutableFile os.FileMode = 0755
	PermissionFile           os.FileMode = 0644

	PermissionDirectoryOwnerOnly os.FileMode = 0700
	PermissionFileOwnerOnly      os.FileMode = 0600

	PermissionMaskDirectoryExecute os.FileMode = 0100
)

Variables

This section is empty.

Functions

func DirExists

func DirExists(dirPath string) bool

DirExists checks if the given directory path exists. It returns true if the directory exists, false otherwise.

func FileExists

func FileExists(filePath string) bool

FileExists checks if the given file path exists and is a regular file. It returns true if the file exists and is regular, false otherwise.

func GetNewLineSeparator

func GetNewLineSeparator() string

func GetenvOrDefault

func GetenvOrDefault(key string, def string) string

GetenvOrDefault behaves like `os.Getenv`, except it returns a specified default value if the key is not present in the environment.

func IsDirEmpty

func IsDirEmpty(directoryPath string, treatMissingAsEmpty ...bool) (bool, error)

IsDirEmpty checks if the given directory is empty. If the directory does not exist, it can either treat it as an error or as an empty directory based on the input flag. By default, it treats a missing directory as an error.

func IsPathContained

func IsPathContained(basePath, targetPath string) bool

IsPathContained checks whether targetPath is contained within basePath after cleaning both paths. This is used to prevent path traversal attacks where a resolved path might escape its intended directory. Backslashes in targetPath are normalized to the OS separator before checking, so paths like "..\..\malicious" are caught on all platforms.

func Rename

func Rename(ctx context.Context, old, new string) error

Rename is like os.Rename in every way. The context is ignored.

func ResolveContainedPath

func ResolveContainedPath(roots []string, filePath string) (string, error)

ResolveContainedPath attempts to resolve filePath within one of the given root directories. It returns the first absolute path that exists and is contained within its root. If the path resolves outside all roots, it returns a path-traversal error. If the path is contained but does not exist in any root, it returns a not-found error.

func RunningFromPipeline

func RunningFromPipeline() bool

Types

type ExpandableMap

type ExpandableMap map[string]ExpandableString

ExpandableMap is a map of string keys to ExpandableString values. It provides convenient methods for expanding all values in the map.

func (ExpandableMap) Expand

func (em ExpandableMap) Expand(mapping func(string) string) (map[string]string, error)

Expand evaluates all ExpandableString values in the map, substituting variables as ExpandableString.Envsubst would. Returns a map[string]string with all values expanded, or an error if any expansion fails. The mapping parameter is a function that returns the value for a given variable name.

func (ExpandableMap) MustExpand

func (em ExpandableMap) MustExpand(mapping func(string) string) map[string]string

MustExpand evaluates all ExpandableString values in the map and panics if any expansion fails. This is useful when you know the expansion should succeed or want to fail fast.

type ExpandableString

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

ExpandableString is a string that has ${foo} style references inside which can be evaluated.

func NewExpandableString

func NewExpandableString(template string) ExpandableString

func (ExpandableString) Empty

func (e ExpandableString) Empty() bool

Empty returns true if the template is empty.

func (ExpandableString) Envsubst

func (e ExpandableString) Envsubst(mapping func(string) string) (string, error)

Envsubst evaluates the template, substituting values as envsubst.Eval would.

func (ExpandableString) MarshalYAML

func (e ExpandableString) MarshalYAML() (any, error)

func (ExpandableString) MustEnvsubst

func (e ExpandableString) MustEnvsubst(mapping func(string) string) string

MustEnvsubst evaluates the template, substituting values as envsubst.Eval would and panics if there is an error (for example, the string is malformed).

func (*ExpandableString) UnmarshalYAML

func (e *ExpandableString) UnmarshalYAML(unmarshal func(any) error) error

type LazyRetryInit

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

LazyRetryInit provides a thread-safe initialization pattern that caches success but allows retries on failure. Unlike sync.Once, failed calls are not cached, so subsequent calls can retry the initialization. Uses atomic.Bool for a lock-free fast path after successful initialization.

func (*LazyRetryInit) Do

func (l *LazyRetryInit) Do(f func() error) error

Do calls f if initialization has not yet succeeded. If f returns nil, the result is cached and subsequent calls return nil immediately (lock-free fast path). If f returns an error, the error is returned and f will be called again on the next invocation.

type RetryStrategy

type RetryStrategy struct {
	// The maximum number of retries before failing
	MaxRetries uint64
	// The time between each retry attempt
	RetryBackoff time.Duration
}

func NewRetryStrategy

func NewRetryStrategy(maxRetries uint64, retryBackoff time.Duration) *RetryStrategy

Creates a new retry strategy that also reduces the time for when running in test

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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