cmdutil

package
v1.30.3 Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2026 License: GPL-3.0 Imports: 24 Imported by: 0

Documentation

Index

Constants

View Source
const ArgsDelimiter = "∯ᓰ♨"

ArgsDelimiter is the delimiter used to separate command arguments

Variables

View Source
var ErrCommandIsEmpty = fmt.Errorf("command is empty")

Functions

func BuildCommandEscapedString

func BuildCommandEscapedString(command string, args []string) string

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

func BuildShellCommandString(shell string, args []string, command string) string

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

func DetectShebang(script string) (string, []string, error)

DetectShebang checks if the given script starts with a shebang (#!) line.

func EvalIntString

func EvalIntString(ctx context.Context, input string, opts ...EvalOption) (int, error)

EvalIntString substitutes environment variables and commands in the input string

func EvalObject

func EvalObject[T any](ctx context.Context, obj T, vars map[string]string) (T, error)

EvalObject recursively evaluates the string fields of the given object

func EvalString

func EvalString(ctx context.Context, input string, opts ...EvalOption) (string, error)

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

func ExpandReferences(ctx context.Context, input string, dataMap map[string]string) string

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

func GetScriptExtension(shellCommand string) string

GetScriptExtension returns the appropriate file extension for the given shell. This is needed because some shells (like PowerShell) require specific file extensions.

func GetShellCommand

func GetShellCommand(configuredShell string) string

GetShellCommand returns the shell to use for command execution

func IsCmdShell added in v1.30.0

func IsCmdShell(shell string) bool

IsCmdShell reports whether the shell is Windows cmd.exe.

func IsNixShell added in v1.30.0

func IsNixShell(shell string) bool

IsNixShell reports whether the shell is nix-shell.

func IsPowerShell added in v1.30.0

func IsPowerShell(shell string) bool

IsPowerShell reports whether the shell is PowerShell or pwsh.

func IsUnixLikeShell added in v1.30.0

func IsUnixLikeShell(shell string) bool

IsUnixLikeShell reports whether the shell supports -c and -e flags. Returns true for sh, bash, zsh, ksh, ash, dash (including .exe variants).

func JoinCommandArgs

func JoinCommandArgs(cmd string, args []string) string

JoinCommandArgs joins a command and its arguments into a single string separated by ArgsDelimiter

func KillMultipleProcessGroups

func KillMultipleProcessGroups(cmds map[string]*exec.Cmd, sig os.Signal) error

KillMultipleProcessGroups kills multiple process groups on Unix systems

func KillProcessGroup

func KillProcessGroup(cmd *exec.Cmd, sig os.Signal) error

KillProcessGroup kills the process group on Unix systems

func ParsePipedCommand

func ParsePipedCommand(cmdString string) ([][]string, error)

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

func SetupCommand(cmd *exec.Cmd)

SetupCommand configures Unix-specific command attributes

func ShellCommandFlag added in v1.30.0

func ShellCommandFlag(shell string) string

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

func ShellQuote(s string) string

ShellQuote escapes a string for use in a shell command.

func ShellQuoteArgs added in v1.29.0

func ShellQuoteArgs(args []string) string

ShellQuoteArgs escapes a slice of strings for use in a shell command.

func SplitCommand

func SplitCommand(cmd string) (string, []string, error)

SplitCommand splits a command string into a command and its arguments.

func SplitCommandArgs

func SplitCommandArgs(cmdWithArgs string) (string, []string)

SplitCommandArgs splits a command and its arguments into a command and a slice of arguments

func SplitCommandWithSub

func SplitCommandWithSub(cmd string) (string, []string, error)

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

type StepInfo

type StepInfo struct {
	Stdout   string
	Stderr   string
	ExitCode string
}

StepInfo contains metadata about a step that can be accessed via property syntax

Jump to

Keyboard shortcuts

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