helpers

package
v1.4.3 Latest Latest
Warning

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

Go to latest
Published: May 5, 2025 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CaptureOutput

func CaptureOutput(f func()) string

func CheckContext

func CheckContext(ctx context.Context) error

CheckContext checks whether the provided context has been canceled or expired.

This function checks if the context has been canceled or the deadline exceeded. If the context is done, it returns an error indicating the cancellation or expiration. If the context is still active, it returns nil.

Parameters:

  • ctx (context.Context): The context to check.

Returns:

  • error: Returns an error if the context is done (canceled or expired), otherwise nil.

func CheckPath

func CheckPath(path string) error

CheckPath checks if a given path is valid and exists on the filesystem.

This function cleans the provided path and verifies its validity using the `IsValidPath` function. It then checks if the file or directory exists using `os.Stat`. If the file or directory does not exist or is not valid, an appropriate error is returned.

Parameters:

  • path (string): The path to be validated and checked for existence.

Returns:

  • error: An error if the path is invalid or does not exist, otherwise returns nil.

func CheckPaths

func CheckPaths(stage types.Stage, ch chan<- error)

CheckPaths checks whether the paths in the "From" field of a stage are valid.

This function iterates over the paths in the "From" field of the given stage and checks if each path is valid by calling the CheckPath function. If any path is invalid, it sends the error to the provided channel and exits early.

Parameters:

  • stage (Stage): The stage containing the "From" paths to check.
  • ch (chan<- error): A channel to send any errors encountered during the path checks.

The function does not return any value. If an error occurs during path validation, the error is sent to the provided channel.

func Choose

func Choose(items *[]string, value *string, title string) error

func Cleanup

func Cleanup(resource io.Closer, ch chan<- error)

Cleanup closes the provided resource and handles any errors that occur during closure.

If the resource is nil, the function returns immediately without taking any action. If resource is not nil, its Close() method is called. If Close() returns an error and the provided

channel ch is not nil, the error is sent to ch.

Parameters:

  • resource: an object that implements the io.Closer interface, representing the resource to be closed.
  • ch: a channel for reporting errors. If ch is nil, any errors from resource.Close() are ignored.

func DefaultYAML

func DefaultYAML() string

func GetModulesDir

func GetModulesDir() (string, error)

func IsDir

func IsDir(path string) (bool, error)

IsDir checks if the given path points to a directory.

This function first checks if the path is valid using the `CheckPath` function. Then, it retrieves the file information using `os.Stat` to determine whether the given path is a directory or not.

Parameters:

  • path (string): The path to be checked.

Returns:

  • bool: `true` if the path is a directory, `false` otherwise.
  • error: An error if the path is invalid or if there is an issue retrieving the file information.

func IsValidPath

func IsValidPath(filePath, basePath string) bool

IsValidPath checks if the given filePath is a valid path relative to the basePath.

This function determines whether the absolute path of filePath is within the directory specified by basePath. It ensures that the file path does not contain any ".." segments, which would indicate an attempt to traverse up the directory structure, and that the filePath is within the basePath directory.

Parameters:

  • filePath (string): The path to check for validity.
  • basePath (string): The base directory to check against.

Returns:

  • bool: Returns true if the filePath is valid (i.e., is within the basePath directory), otherwise returns false.

func ReplaceVariables

func ReplaceVariables(input string, variables map[string]string, depth int) (string, error)

ReplaceVariables recursively replaces variables in the input string with their corresponding values from the provided map of variables.

The function supports up to 5 levels of recursion (controlled by the `depth` parameter) to allow nested variable replacements. If the depth exceeds 5 or if the depth is less than 0, an error will be returned.

Parameters:

  • input (string): The input string containing variables in the format `{variableName}` to replace.
  • variables (map[string]string): A map containing variable names as keys and their replacement values as strings.
  • depth (int): The current recursion depth, which should start from 0. The function supports up to 5 levels of recursion.

Returns:

  • string: The updated string with variables replaced by their corresponding values, or an error if no replacement could be made.
  • error: An error is returned if the depth exceeds 5, if the depth is negative, or if the replacement results in an empty string.

func ResultMessage

func ResultMessage(format string, a ...any)

func UserInput added in v1.4.2

func UserInput(
	prompter interfaces.Prompter,
	variable *string,
	title string,
	validator func(string) error,
) error

UserInput displays an interactive input prompt with a title and validation, using the provided Prompter interface.

The entered value is stored in the given string pointer. If the input fails validation or an error occurs, the function returns an error.

Parameters:

  • prompter: an implementation of the Prompter interface that handles input rendering and retrieval.
  • variable: a pointer to a string where the resulting input will be stored.
  • title: the title or label shown to the user for the input prompt.
  • validator: a function used to validate the user input.

Returns:

  • error: an error if the input fails validation or the prompt fails to run; nil on success.

Types

type Cfg

type Cfg string
const RootDir Cfg = "root_dir"

Jump to

Keyboard shortcuts

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