Documentation
¶
Index ¶
- Constants
- Variables
- func BuildCommandEscapedString(command string, args []string) string
- func BuildShellCommandString(shell string, args []string, command string) string
- func DetectShebang(script string) (string, []string, error)
- func EvalIntString(ctx context.Context, input string, opts ...EvalOption) (int, error)
- func EvalObject[T any](ctx context.Context, obj T, vars map[string]string) (T, error)
- func EvalString(ctx context.Context, input string, opts ...EvalOption) (string, error)
- func EvalStringFields[T any](ctx context.Context, obj T, opts ...EvalOption) (T, error)
- func ExpandReferences(ctx context.Context, input string, dataMap map[string]string) string
- func ExpandReferencesWithSteps(ctx context.Context, input string, dataMap map[string]string, ...) string
- func GetScriptExtension(shellCommand string) string
- func GetShellCommand(configuredShell string) string
- func IsCmdShell(shell string) bool
- func IsNixShell(shell string) bool
- func IsPowerShell(shell string) bool
- func IsUnixLikeShell(shell string) bool
- func JoinCommandArgs(cmd string, args []string) string
- func KillMultipleProcessGroups(cmds map[string]*exec.Cmd, sig os.Signal) error
- func KillProcessGroup(cmd *exec.Cmd, sig os.Signal) error
- func ParsePipedCommand(cmdString string) ([][]string, error)
- func SetupCommand(cmd *exec.Cmd)
- func ShellCommandFlag(shell string) string
- func ShellQuote(s string) string
- func ShellQuoteArgs(args []string) string
- func SplitCommand(cmd string) (string, []string, error)
- func SplitCommandArgs(cmdWithArgs string) (string, []string)
- func SplitCommandWithSub(cmd string) (string, []string, error)
- type EvalOption
- type EvalOptions
- type StepInfo
Constants ¶
const ArgsDelimiter = "∯ᓰ♨"
ArgsDelimiter is the delimiter used to separate command arguments
Variables ¶
var ErrCommandIsEmpty = fmt.Errorf("command is empty")
Functions ¶
func BuildCommandEscapedString ¶
BuildCommandEscapedString constructs a single shell-ready string from a command and its arguments. It assumes that the command and arguments are already escaped.
func BuildShellCommandString ¶ added in v1.30.0
BuildShellCommandString constructs a complete shell command string suitable for remote execution (e.g., via SSH). The command is properly quoted.
Example outputs:
- Unix: `/bin/bash -c 'echo hello'`
- PowerShell: `powershell -Command 'echo hello'`
- cmd.exe: `cmd.exe /c 'echo hello'`
func DetectShebang ¶
DetectShebang checks if the given script starts with a shebang (#!) line.
func EvalIntString ¶
EvalIntString substitutes environment variables and commands in the input string
func EvalObject ¶
EvalObject recursively evaluates the string fields of the given object
func EvalString ¶
EvalString substitutes environment variables and commands in the input string
func EvalStringFields ¶
func EvalStringFields[T any](ctx context.Context, obj T, opts ...EvalOption) (T, error)
EvalStringFields processes all string fields in a struct or map by expanding environment variables and substituting command outputs. It takes a struct or map value and returns a new modified struct or map value.
func ExpandReferences ¶
ExpandReferences finds all occurrences of ${NAME.foo.bar} in the input string, where "NAME" matches a key in the dataMap. The dataMap value is expected to be JSON. It then uses gojq to extract the .foo.bar sub-path from that JSON document. If successful, it replaces the original placeholder with the sub-path value.
If dataMap[name] is invalid JSON or the sub-path does not exist, the placeholder is left as-is (or you could handle it differently).
func ExpandReferencesWithSteps ¶
func ExpandReferencesWithSteps(ctx context.Context, input string, dataMap map[string]string, stepMap map[string]StepInfo) string
ExpandReferencesWithSteps is like ExpandReferences but also handles step ID property access like ${step_id.stdout}, ${step_id.stderr}, ${step_id.exit_code}
func GetScriptExtension ¶ added in v1.24.11
GetScriptExtension returns the appropriate file extension for the given shell. This is needed because some shells (like PowerShell) require specific file extensions.
func GetShellCommand ¶
GetShellCommand returns the shell to use for command execution
func IsCmdShell ¶ added in v1.30.0
IsCmdShell reports whether the shell is Windows cmd.exe.
func IsNixShell ¶ added in v1.30.0
IsNixShell reports whether the shell is nix-shell.
func IsPowerShell ¶ added in v1.30.0
IsPowerShell reports whether the shell is PowerShell or pwsh.
func IsUnixLikeShell ¶ added in v1.30.0
IsUnixLikeShell reports whether the shell supports -c and -e flags. Returns true for sh, bash, zsh, ksh, ash, dash (including .exe variants).
func JoinCommandArgs ¶
JoinCommandArgs joins a command and its arguments into a single string separated by ArgsDelimiter
func KillMultipleProcessGroups ¶
KillMultipleProcessGroups kills multiple process groups on Unix systems
func KillProcessGroup ¶
KillProcessGroup kills the process group on Unix systems
func ParsePipedCommand ¶
ParsePipedCommand splits a shell-style command string into a pipeline ([][]string). Each sub-slice represents a single command. Unquoted "|" tokens define the boundaries.
Example:
parsePipedCommand(`echo foo | grep foo | wc -l`) =>
[][]string{
{"echo", "foo"},
{"grep", "foo"},
{"wc", "-l"},
}
parsePipedCommand(`echo "hello|world"`) =>
[][]string{ {"echo", "hello|world"} } // single command
func SetupCommand ¶
SetupCommand configures Unix-specific command attributes
func ShellCommandFlag ¶ added in v1.30.0
ShellCommandFlag returns the flag used to pass a command string to the shell. Returns "-c" for Unix shells, "-Command" for PowerShell, "/c" for cmd.exe, "--run" for nix-shell, or "-c" for unknown shells (defaulting to Unix-style).
func ShellQuote ¶ added in v1.29.0
ShellQuote escapes a string for use in a shell command.
func ShellQuoteArgs ¶ added in v1.29.0
ShellQuoteArgs escapes a slice of strings for use in a shell command.
func SplitCommand ¶
SplitCommand splits a command string into a command and its arguments.
func SplitCommandArgs ¶
SplitCommandArgs splits a command and its arguments into a command and a slice of arguments
Types ¶
type EvalOption ¶
type EvalOption func(*EvalOptions)
func OnlyReplaceVars ¶
func OnlyReplaceVars() EvalOption
func WithStepMap ¶
func WithStepMap(stepMap map[string]StepInfo) EvalOption
func WithVariables ¶
func WithVariables(vars map[string]string) EvalOption
func WithoutExpandEnv ¶
func WithoutExpandEnv() EvalOption
func WithoutExpandShell ¶ added in v1.30.0
func WithoutExpandShell() EvalOption
func WithoutSubstitute ¶
func WithoutSubstitute() EvalOption
type EvalOptions ¶
type EvalOptions struct {
ExpandEnv bool
Substitute bool
Variables []map[string]string
StepMap map[string]StepInfo
ExpandShell bool // When false, skip shell-based variable expansion (e.g., for SSH commands)
}
func NewEvalOptions ¶
func NewEvalOptions() *EvalOptions