helpers

package
v0.87.0 Latest Latest
Warning

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

Go to latest
Published: Oct 1, 2026 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package helpers provides shared filesystem, URL, and value helpers.

Index

Constants

View Source
const (
	// ReadUser is used for any internal file to be read only
	ReadUser = 0400
	// ReadWriteUser is used for any internal file not normally used by the end user or containing sensitive data
	ReadWriteUser = 0600
	// ReadAllWriteUser is used for any non sensitive file intended to be consumed by the end user
	ReadAllWriteUser = 0644
	// ReadWriteExecuteUser is used for any directory or executable not normally used by the end user or containing sensitive data
	ReadWriteExecuteUser = 0700
	// ReadExecuteAllWriteUser is used for any non sensitive directory or executable intended to be consumed by the end user
	ReadExecuteAllWriteUser = 0755
)
View Source
const (
	// OCIURLPrefix is the scheme prefix for OCI registry URLs.
	OCIURLPrefix = "oci://"
	// IPV4Localhost is the IPv4 loopback address.
	IPV4Localhost = "127.0.0.1"
)

Variables

This section is empty.

Functions

func CreateDirectory

func CreateDirectory(path string, mode os.FileMode) error

CreateDirectory creates a directory for the given path and file mode.

func CreateParentDirectory

func CreateParentDirectory(destination string) error

CreateParentDirectory creates the parent directory for the given file path.

func CreatePathAndCopy

func CreatePathAndCopy(source, destination string) error

CreatePathAndCopy creates the parent directory for the given file path and copies the source file to the destination.

func DoHostnamesMatch

func DoHostnamesMatch(first, second string) (bool, error)

DoHostnamesMatch reports whether the hostnames in first and second match.

func ExtractBasePathFromURL

func ExtractBasePathFromURL(source string) (string, error)

ExtractBasePathFromURL returns the final path component of source.

func GetAvailablePort

func GetAvailablePort() (int, error)

GetAvailablePort listens on an ephemeral TCP port and returns its port number.

func GetSHA256Hash

func GetSHA256Hash(data io.Reader) (string, error)

GetSHA256Hash returns the computed SHA256 Sum of a given file

func GetSHA256OfFile

func GetSHA256OfFile(path string) (string, error)

GetSHA256OfFile returns the SHA256 hash of the provided file.

func InvalidPath

func InvalidPath(path string) bool

InvalidPath checks if the given path is valid (if it is a permissions error it is there we just don't have access)

func IsDir

func IsDir(path string) bool

IsDir returns true if the given path is a directory.

func IsNotZeroAndNotEqual

func IsNotZeroAndNotEqual[T any](given, equal T) bool

IsNotZeroAndNotEqual is used to test if a struct has zero values or is equal values with another struct

func IsOCIURL

func IsOCIURL(source string) bool

IsOCIURL reports whether source uses the OCI URL scheme.

func IsTextFile

func IsTextFile(path string) (bool, error)

IsTextFile returns true if path is a text file, false otherwise. It might return an error if the file cannot be read.

func IsURL

func IsURL(source string) bool

IsURL reports whether source is a URL with a scheme and host.

func IsValidHostName

func IsValidHostName() bool

IsValidHostName reports whether the local hostname is valid and not localhost.

func MatchRegex

func MatchRegex(regex *regexp.Regexp, value string) (func(string) string, error)

MatchRegex wraps a get function around a substring match.

func MergeMapRecursive

func MergeMapRecursive(first, second map[string]any) map[string]any

MergeMapRecursive recursively (nestedly) merges map second with first overwriting common values with second's values.

func MergeNonZero

func MergeNonZero[T any](original, overrides T) T

MergeNonZero is used to merge non-zero overrides from one struct into another of the same type

func RandomString

func RandomString(length int) (string, error)

RandomString generates a secure random string of the specified length.

func ReadFileByChunks

func ReadFileByChunks(path string, chunkSize int) (chunks [][]byte, sha256sum string, err error)

ReadFileByChunks reads a file into multiple chunks by the given size.

func RecursiveFileList

func RecursiveFileList(dir string, pattern *regexp.Regexp, skipHidden bool) (files []string, err error)

RecursiveFileList walks a path with an optional regex pattern and returns a slice of file paths. If skipHidden is true, hidden directories will be skipped.

func SHAsMatch

func SHAsMatch(path, expected string) error

SHAsMatch returns an error if the SHA256 hash of the provided file does not match the expected hash.

func StringToSlice

func StringToSlice(value string) []string

StringToSlice converts a comma-separated string to a slice of lowercase strings.

func TransformAndMergeMap

func TransformAndMergeMap[T any](first, second map[string]T, transform func(string) string) map[string]T

TransformAndMergeMap transforms keys in both maps then merges map m2 with m1 overwriting common values with m2's values.

func Truncate

func Truncate(text string, length int, invert bool) string

Truncate truncates provided text to the requested length

func Unique

func Unique[T comparable](values []T) (result []T)

Unique returns a new slice with only unique elements.

Types

This section is empty.

Jump to

Keyboard shortcuts

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