utils

package
v0.25.0-rc1 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: Apache-2.0 Imports: 47 Imported by: 0

Documentation

Overview

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Package utils provides generic helper functions

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Package utils provides generic helper functions.

Index

Constants

View Source
const SGETProtocol = "sget://"

SGETProtocol is the protocol URI scheme for SGET.

Variables

This section is empty.

Functions

func ByteFormat

func ByteFormat(inputNum float64, precision int) string

ByteFormat formats a number of bytes into a human readable string.

func ColorPrintYAML

func ColorPrintYAML(data any)

ColorPrintYAML pretty prints a yaml file to the console.

func CreateDirectory

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

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

func CreateFilePath

func CreateFilePath(destination string) error

CreateFilePath creates the parent directory for the given file path.

func CreatePathAndCopy

func CreatePathAndCopy(source string, destination string) error

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

func DoHostnamesMatch added in v0.23.2

func DoHostnamesMatch(url1 string, url2 string) (bool, error)

DoHostnamesMatch returns a boolean indicating if the hostname of two different URLs are the same.

func DownloadToFile

func DownloadToFile(url string, target string, cosignKeyPath string)

DownloadToFile downloads a given URL to the target filepath (including the cosign key if necessary).

func Fetch

func Fetch(url string) io.ReadCloser

Fetch fetches the response body from a given URL.

func Filter added in v0.24.0

func Filter[T any](ss []T, test func(T) bool) (r []T)

Filter returns a new slice with only the elements that pass the test.

func Find added in v0.24.0

func Find[T any](ss []T, test func(T) bool) (r T)

Find returns the first element that passes the test.

func FindYamlTemplates

func FindYamlTemplates(config any, prefix string, suffix string) (map[string]string, error)

FindYamlTemplates finds strings with a given prefix in a config.

func GetAvailablePort

func GetAvailablePort() (int, error)

GetAvailablePort retrieves an available port on the host machine. This delegates the port selection to the golang net library by starting a server and then checking the port that the server is using.

func GetCRCHash added in v0.25.0

func GetCRCHash(text string) uint32

GetCRCHash returns the computed CRC32 Sum of a given string

func GetCryptoHash added in v0.24.0

func GetCryptoHash(path string, hashName crypto.Hash) (string, error)

GetCryptoHash returns the computed SHA256 Sum of a given file

func GetFinalExecutablePath

func GetFinalExecutablePath() (string, error)

GetFinalExecutablePath returns the absolute path to the Zarf executable, following any symlinks along the way.

func GetHtpasswdString

func GetHtpasswdString(username string, password string) (string, error)

GetHtpasswdString converts a username and password to a properly formatted and hashed format for `htpasswd`.

func Insert added in v0.25.0

func Insert[T any](slice []T, index int, element T) []T

Insert returns a new slice with the element inserted at the given index.

func InvalidPath

func InvalidPath(path string) bool

InvalidPath checks if the given path exists.

func IsTextFile added in v0.23.3

func IsTextFile(path string) (bool, error)

IsTextFile returns true if the given file is a text file.

func IsURL added in v0.23.2

func IsURL(source string) bool

IsURL is a helper function to check if a URL is valid.

func IsValidHostName

func IsValidHostName() bool

IsValidHostName returns a boolean indicating if the hostname of the host machine is valid according to https://www.ietf.org/rfc/rfc1123.txt.

func ListDirectories

func ListDirectories(directory string) ([]string, error)

ListDirectories returns a list of directories in the given directory.

func MakeTempDir

func MakeTempDir(tmpDir string) (string, error)

MakeTempDir creates a temp directory with the given prefix.

func MatchRegex added in v0.25.0

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

MatchRegex wraps a get function around a substring match.

func MergeMap added in v0.24.0

func MergeMap[T any](m1 map[string]T, m2 map[string]T) (r map[string]T)

MergeMap merges map m2 with m1 overwriting common values with m2's values.

func PrintComponentCredential added in v0.24.0

func PrintComponentCredential(state types.ZarfState, componentName string)

PrintComponentCredential displays credentials for a single component

func PrintCredentialTable added in v0.24.0

func PrintCredentialTable(state types.ZarfState, componentsToDeploy []types.DeployedComponent)

PrintCredentialTable displays credentials in a table

func RandomString

func RandomString(length int) string

RandomString generates a secure random string of the specified length.

func ReadYaml

func ReadYaml(path string, destConfig any) error

ReadYaml reads a yaml file and unmarshals it into a given config.

func RecursiveFileList

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

RecursiveFileList walks a path with an optional regex pattern and returns a slice of file paths.

func ReloadYamlTemplate

func ReloadYamlTemplate(config any, mappings map[string]string) error

ReloadYamlTemplate marshals a given config, replaces strings and unmarshals it back.

func RemoveMatches added in v0.24.0

func RemoveMatches[T any](ss []T, test func(T) bool) (r []T)

RemoveMatches removes the given element from the slice that matches the test.

func ReplaceTextTemplate

func ReplaceTextTemplate(path string, mappings map[string]string, deprecations map[string]string)

ReplaceTextTemplate loads a file from a given path, replaces text in it and writes it back in place.

func Retry

func Retry(fn func() error, retries int, delay time.Duration) (err error)

Retry will retry a function until it succeeds or the timeout is reached, timeout == retries * delay.

func Reverse added in v0.24.0

func Reverse[T any](s []T) (r []T)

Reverse returns a new slice with the elements in reverse order.

func RoundUp

func RoundUp(input float64, places int) (newVal float64)

RoundUp rounds a float64 to the given number of decimal places.

func Sget

func Sget(ctx context.Context, image, key string, out io.Writer) error

Sget performs a cosign signature verification on a given image using the specified public key.

func SliceContains added in v0.24.0

func SliceContains[T comparable](s []T, e T) bool

SliceContains returns true if the given element is in the slice.

func SortDependencies added in v0.25.0

func SortDependencies(data []DependsOn) ([]string, error)

SortDependencies performs a topological sort on a dependency graph and returns a slice of the nodes in order of their precedence. The input data is a map of nodes to a slice of its dependencies.

E.g: A depends on B & C, B depends on C and C has no dependencies: {"A": ["B", "C"], "B": ["C"], "C": []string{}}

Note sort order is dependent on the slice order of the input data for nodes with the same in-degree (i.e. the same number of dependencies).

func SplitFile

func SplitFile(path string, chunkSizeBytes int) (chunks [][]byte, sha256sum string, err error)

SplitFile splits a file into multiple parts by the given size.

func SplitYAML

func SplitYAML(yamlData []byte) ([]*unstructured.Unstructured, error)

SplitYAML splits a YAML file into unstructured objects. Returns list of all unstructured objects found in the yaml. If an error occurs, returns objects that have been parsed so far too. Source: https://github.com/argoproj/gitops-engine/blob/v0.5.2/pkg/utils/kube/kube.go#L286.

func SplitYAMLToString added in v0.25.0

func SplitYAMLToString(yamlData []byte) ([]string, error)

SplitYAMLToString splits a YAML file into strings. Returns list of yamls found in the yaml. If an error occurs, returns objects that have been parsed so far too. Source: https://github.com/argoproj/gitops-engine/blob/v0.5.2/pkg/utils/kube/kube.go#L304.

func SwapHost

func SwapHost(src string, targetHost string) (string, error)

SwapHost Perform base url replacement and adds a crc32 of the original url to the end of the src.

func SwapHostWithoutChecksum

func SwapHostWithoutChecksum(src string, targetHost string) (string, error)

SwapHostWithoutChecksum Perform base url replacement but avoids adding a checksum of the original url.

func TransformMapKeys added in v0.24.0

func TransformMapKeys[T any](m map[string]T, transform func(string) string) (r map[string]T)

TransformMapKeys takes a map and transforms its keys using the provided function.

func Unique

func Unique[T comparable](s []T) (r []T)

Unique returns a new slice with only unique elements.

func VerifyBinary

func VerifyBinary(binary string) bool

VerifyBinary returns true if binary is available.

func WriteFile

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

WriteFile writes the given data to the given path.

func WriteYaml

func WriteYaml(path string, srcConfig any, perm fs.FileMode) error

WriteYaml writes a given config to a yaml file on disk.

Types

type Credential

type Credential struct {
	Path string
	Auth http.BasicAuth
}

Credential represents authentication for a given host.

func FindAuthForHost

func FindAuthForHost(baseURL string) *Credential

FindAuthForHost finds the authentication scheme for a given host using .git-credentials then .netrc.

type DependsOn

type DependsOn struct {
	Name         string
	Dependencies []string
}

DependsOn is a struct that represents a node in a list of dependencies.

type Image

type Image struct {
	Host        string
	Name        string
	Path        string
	Tag         string
	Digest      string
	Reference   string
	TagOrDigest string
}

Image represents a config for an OCI image.

Directories

Path Synopsis
Package exec provides a wrapper around the os/exec package
Package exec provides a wrapper around the os/exec package

Jump to

Keyboard shortcuts

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