shellparse

package
v0.0.37 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package shellparse extracts individual commands from shell one-liners.

It uses mvdan.cc/sh/v3/syntax to parse shell scripts into an AST, then walks the tree to collect every simple command along with its redirects. Compound constructs (&&, ||, ;, |, for, while, if, case, subshells, command substitutions) are recursively decomposed so that every leaf command is reported individually.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ParseResult

type ParseResult struct {
	// Commands contains all individual commands found in the input.
	Commands []ParsedCommand
	// Original is the input string that was parsed.
	Original string
}

ParseResult holds the complete result of parsing a shell one-liner.

func Parse

func Parse(input string) *ParseResult

Parse decomposes a shell one-liner into individual commands.

It handles:

  • Command separators: &&, ||, ;, &, | (pipe)
  • Control structures: for, while, until, if/elif/else, case
  • Grouping: (...) subshells, {...} blocks
  • Command substitution: $(...) and backticks (recursively)
  • Redirections: >, >>, 2>, &>, 2>&1, <, etc.

On parse failure the entire input is returned as a single command (fallback).

type ParsedCommand

type ParsedCommand struct {
	// Raw is the reconstructed command string (e.g. "git status").
	Raw string
	// Executable is the command name (e.g. "git").
	Executable string
	// Args contains the arguments after the executable (e.g. ["status"]).
	Args []string
	// Redirects lists file redirections attached to this command.
	Redirects []Redirect
}

ParsedCommand represents a single simple command extracted from a one-liner.

type Redirect

type Redirect struct {
	// Op is the redirection operator (e.g. ">", ">>", "2>", "&>").
	Op string
	// Path is the target file path (e.g. "/dev/null", "./out.txt").
	Path string
}

Redirect describes a single I/O redirection.

Jump to

Keyboard shortcuts

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