Documentation
¶
Overview ¶
Package helpers provides shared filesystem, URL, and value helpers.
Index ¶
- Constants
- func CreateDirectory(path string, mode os.FileMode) error
- func CreateParentDirectory(destination string) error
- func CreatePathAndCopy(source, destination string) error
- func DoHostnamesMatch(first, second string) (bool, error)
- func ExtractBasePathFromURL(source string) (string, error)
- func GetAvailablePort() (int, error)
- func GetSHA256Hash(data io.Reader) (string, error)
- func GetSHA256OfFile(path string) (string, error)
- func InvalidPath(path string) bool
- func IsDir(path string) bool
- func IsNotZeroAndNotEqual[T any](given, equal T) bool
- func IsOCIURL(source string) bool
- func IsTextFile(path string) (bool, error)
- func IsURL(source string) bool
- func IsValidHostName() bool
- func MatchRegex(regex *regexp.Regexp, value string) (func(string) string, error)
- func MergeMapRecursive(first, second map[string]any) map[string]any
- func MergeNonZero[T any](original, overrides T) T
- func RandomString(length int) (string, error)
- func ReadFileByChunks(path string, chunkSize int) (chunks [][]byte, sha256sum string, err error)
- func RecursiveFileList(dir string, pattern *regexp.Regexp, skipHidden bool) (files []string, err error)
- func SHAsMatch(path, expected string) error
- func StringToSlice(value string) []string
- func TransformAndMergeMap[T any](first, second map[string]T, transform func(string) string) map[string]T
- func Truncate(text string, length int, invert bool) string
- func Unique[T comparable](values []T) (result []T)
Constants ¶
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 )
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 ¶
CreateDirectory creates a directory for the given path and file mode.
func CreateParentDirectory ¶
CreateParentDirectory creates the parent directory for the given file path.
func CreatePathAndCopy ¶
CreatePathAndCopy creates the parent directory for the given file path and copies the source file to the destination.
func DoHostnamesMatch ¶
DoHostnamesMatch reports whether the hostnames in first and second match.
func ExtractBasePathFromURL ¶
ExtractBasePathFromURL returns the final path component of source.
func GetAvailablePort ¶
GetAvailablePort listens on an ephemeral TCP port and returns its port number.
func GetSHA256Hash ¶
GetSHA256Hash returns the computed SHA256 Sum of a given file
func GetSHA256OfFile ¶
GetSHA256OfFile returns the SHA256 hash of the provided file.
func InvalidPath ¶
InvalidPath checks if the given path is valid (if it is a permissions error it is there we just don't have access)
func IsNotZeroAndNotEqual ¶
IsNotZeroAndNotEqual is used to test if a struct has zero values or is equal values with another struct
func IsTextFile ¶
IsTextFile returns true if path is a text file, false otherwise. It might return an error if the file cannot be read.
func IsValidHostName ¶
func IsValidHostName() bool
IsValidHostName reports whether the local hostname is valid and not localhost.
func MatchRegex ¶
MatchRegex wraps a get function around a substring match.
func MergeMapRecursive ¶
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 ¶
RandomString generates a secure random string of the specified length.
func ReadFileByChunks ¶
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 ¶
SHAsMatch returns an error if the SHA256 hash of the provided file does not match the expected hash.
func StringToSlice ¶
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 Unique ¶
func Unique[T comparable](values []T) (result []T)
Unique returns a new slice with only unique elements.
Types ¶
This section is empty.