cmdutil

package
v1.16.4 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2025 License: GPL-2.0, GPL-3.0 Imports: 13 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 EvalIntString

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

EvalIntString substitutes environment variables and commands in the input string

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 by expanding environment variables and substituting command outputs. It takes a struct value and returns a new modified struct 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 GetShellCommand

func GetShellCommand(configuredShell string) string

GetShellCommand returns the shell to use for command execution

func JoinCommandArgs

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

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

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 SplitCommand

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

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 WithVariables

func WithVariables(vars map[string]string) EvalOption

func WithoutExpandEnv

func WithoutExpandEnv() EvalOption

func WithoutSubstitute

func WithoutSubstitute() EvalOption

type EvalOptions

type EvalOptions struct {
	ExpandEnv  bool
	Substitute bool
	Variables  []map[string]string
}

Jump to

Keyboard shortcuts

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