Skip to content

The cross-harness, self-improving memory layer for AI agents. Join our community: https://discord.com/invite/zdNChS2fBu

pkg/asymptoteobserve: shared shell command reader (shellcmd) (#1095)

kyle-elliott-asymptoteba870aeb2ef3
diff --git a/cli/beacon-hooks/go.mod b/cli/beacon-hooks/go.mod
index c2d7d30e..c476f920 100644
--- a/cli/beacon-hooks/go.mod
+++ b/cli/beacon-hooks/go.mod
@@ -14,5 +14,5 @@ replace github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve => ../../pkg
require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
- golang.org/x/sys v0.44.0 // indirect
+ golang.org/x/sys v0.47.0 // indirect
)
diff --git a/cli/beacon-hooks/go.sum b/cli/beacon-hooks/go.sum
index 46da5e45..874fb7ae 100644
--- a/cli/beacon-hooks/go.sum
+++ b/cli/beacon-hooks/go.sum
@@ -6,7 +6,7 @@ github.com/spf13/cobra v1.8.1 h1:e5/vxKd/rZsfSJMUX1agtjeTDf+qv1/JdBF8gg5k9ZM=
github.com/spf13/cobra v1.8.1/go.mod h1:wHxEcudfqmLYa8iTfL+OuZPbBZkmvliBWKIezN3kD9Y=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
-golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
-golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
+golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
+golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/cli/beacon/go.mod b/cli/beacon/go.mod
index 5db9f7e0..dbc4d851 100644
--- a/cli/beacon/go.mod
+++ b/cli/beacon/go.mod
@@ -14,7 +14,6 @@ require (
golang.org/x/term v0.45.0
gopkg.in/yaml.v3 v3.0.1
modernc.org/sqlite v1.59.0
- mvdan.cc/sh/v3 v3.14.1
)
replace github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve => ../../pkg/asymptoteobserve
@@ -56,4 +55,5 @@ require (
modernc.org/libc v1.75.7 // indirect
modernc.org/mathutil v1.7.1 // indirect
modernc.org/memory v1.12.1 // indirect
+ mvdan.cc/sh/v3 v3.14.1 // indirect
)
diff --git a/cli/beacon/internal/endpoint/dashboard/share_activity.go b/cli/beacon/internal/endpoint/dashboard/share_activity.go
index 03de9c01..62bf6dd1 100644
--- a/cli/beacon/internal/endpoint/dashboard/share_activity.go
+++ b/cli/beacon/internal/endpoint/dashboard/share_activity.go
@@ -5,6 +5,7 @@ import (
"github.com/asymptote-labs/agent-beacon/cli/beacon/internal/endpoint/schema"
"github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/shellcmd"
)
// shareActivity decides which of a session's events count as activity that makes its shared trace
@@ -33,89 +34,34 @@ type shareActivity struct {
}
// isBeaconShareCommand reports whether a shell command runs `beacon share` (any subcommand or
-// flags): `beacon` must be in command position -- the start of the command or of a segment after
-// `;`, `&&`, `||`, `|`, `(` or a newline outside quotes -- optionally after environment assignments,
-// `sudo`, `exec` or `env`, and optionally with a path (`/opt/beacon/bin/beacon`, `./beacon`,
-// `beacon.exe`), followed by `share`. A mention inside an argument (`rg "beacon share"`,
-// `git commit -m 'beacon share ...'`, `echo beacon share`) is not a share call.
+// flags): a command of the line (shellcmd.Parse: after `;`, `&&`, `||`, `|`, a newline, inside a
+// subshell or a substitution), after the wrappers shellcmd.Command.Unwrapped skips (environment
+// assignments, sudo, env, exec, command, nohup, time), is `beacon` -- optionally with a path
+// (`/opt/beacon/bin/beacon`, `./beacon`, `C:\Tools\beacon.exe`) -- followed by `share`. A mention
+// inside an argument (`rg "beacon share"`, `git commit -m 'beacon share ...'`, `echo beacon
+// share`) is not a share call. A capture cut short (an unclosed quote or here-document) is read
+// up to the cut, so a share call before it still counts; any other command line that does not
+// parse as bash (PowerShell's `& "C:\...\beacon.exe" share`) is not a share call.
func isBeaconShareCommand(command string) bool {
- if command == "" {
- return false
- }
- for _, segment := range shellSegments(command) {
- words := strings.Fields(segment)
- i := 0
- for i < len(words) && (isEnvAssignment(words[i]) || words[i] == "sudo" || words[i] == "exec" || words[i] == "env") {
- i++
- }
- if i+1 < len(words) && isBeaconExecutable(words[i]) && words[i+1] == "share" {
+ commands, _ := shellcmd.Parse(command)
+ for _, c := range commands {
+ words, _ := c.Unwrapped()
+ if len(words) > 1 && isBeaconExecutable(words[0].Raw) && words[1].Known && words[1].Text == "share" {
return true
}
}
return false
}
-// shellSegments splits a command at the separators that start a new command (`;`, `&`, `|`, `(`,
-// `)`, newline), outside single and double quotes. Quoted text stays inside its segment, so a
-// separator in an argument does not start one.
-func shellSegments(command string) []string {
- var segments []string
- var current strings.Builder
- var quote rune
- escaped := false
- flush := func() {
- if strings.TrimSpace(current.String()) != "" {
- segments = append(segments, current.String())
- }
- current.Reset()
- }
- for _, r := range command {
- switch {
- case escaped:
- escaped = false
- current.WriteRune(r)
- case r == '\\' && quote != '\'':
- escaped = true
- current.WriteRune(r)
- case quote != 0:
- if r == quote {
- quote = 0
- }
- current.WriteRune(r)
- case r == '\'' || r == '"':
- quote = r
- current.WriteRune(r)
- case r == ';' || r == '&' || r == '|' || r == '(' || r == ')' || r == '\n':
- flush()
- default:
- current.WriteRune(r)
- }
- }
- flush()
- return segments
-}
-
-func isEnvAssignment(word string) bool {
- name, _, ok := strings.Cut(word, "=")
- if !ok || name == "" {
- return false
- }
- for i, r := range name {
- if !(r == '_' || r >= 'A' && r <= 'Z' || r >= 'a' && r <= 'z' || i > 0 && r >= '0' && r <= '9') {
- return false
- }
- }
- return true
-}
-
-// isBeaconExecutable is the word `beacon`, or a path to it, as a command names it.
-func isBeaconExecutable(word string) bool {
- word = strings.Trim(word, `"'`)
- if i := strings.LastIndexAny(word, `/\`); i >= 0 {
- word = word[i+1:]
- }
- word = strings.ToLower(word)
- return word == "beacon" || word == "beacon.exe"
+// isBeaconExecutable is the word `beacon`, or a path to it, as a command names it. It reads the
+// word's source text, so a Windows path keeps the backslashes that bash would remove.
+func isBeaconExecutable(raw string) bool {
+ raw = strings.Trim(raw, `"'`)
+ if i := strings.LastIndexAny(raw, `/\`); i >= 0 {
+ raw = raw[i+1:]
+ }
+ raw = strings.ToLower(raw)
+ return raw == "beacon" || raw == "beacon.exe"
}
// counts reports whether event is activity that makes a share stale, and updates the state.
diff --git a/cli/beacon/internal/endpoint/dashboard/sharing_view_test.go b/cli/beacon/internal/endpoint/dashboard/sharing_view_test.go
index 2337055d..e590ed5d 100644
--- a/cli/beacon/internal/endpoint/dashboard/sharing_view_test.go
+++ b/cli/beacon/internal/endpoint/dashboard/sharing_view_test.go
@@ -359,14 +359,19 @@ func TestOpenCodeShareFromInsideTheSessionIsNotStale(t *testing.T) {
func TestIsBeaconShareCommand(t *testing.T) {
for command, want := range map[string]bool{
- `beacon share --current`: true,
- `cd x && beacon share refresh --harness claude_code`: true,
- `/opt/beacon/bin/beacon share`: true,
- `BEACON_X=1 beacon share`: true,
- `./beacon share --json`: true,
- `C:\Tools\beacon.exe share`: true,
- `sudo beacon share --session abc`: true,
- "go test ./...\nbeacon share": true,
+ `beacon share --current`: true,
+ `cd x && beacon share refresh --harness claude_code`: true,
+ `/opt/beacon/bin/beacon share`: true,
+ `BEACON_X=1 beacon share`: true,
+ `./beacon share --json`: true,
+ `C:\Tools\beacon.exe share`: true,
+ `sudo beacon share --session abc`: true,
+ "go test ./...\nbeacon share": true,
+ // Captures cut short: the share call before the cut still counts.
+ "beacon share --current --json 'unterminated": true,
+ "beacon share --current && cat <<EOF\nlong text": true,
+ "git commit -m 'beacon share": false,
+ `& "C:\Tools\beacon.exe" share`: false, // PowerShell, not bash
`rg "beacon share"`: false,
`git commit -m 'beacon share now records shared_through'`: false,
`git commit -m 'fix; beacon share'`: false,
diff --git a/cli/beacon/internal/gitlink/shell.go b/cli/beacon/internal/gitlink/shell.go
index 1e9b83d7..84ca729f 100644
--- a/cli/beacon/internal/gitlink/shell.go
+++ b/cli/beacon/internal/gitlink/shell.go
@@ -2,444 +2,34 @@ package gitlink
import (
"path/filepath"
- "slices"
"strings"
- "mvdan.cc/sh/v3/expand"
- "mvdan.cc/sh/v3/syntax"
+ "github.com/asymptote-labs/agent-beacon/pkg/asymptoteobserve/shellcmd"
)
// ShellWrittenPaths returns the files a shell command line evidently writes: the operands of rm,
// touch, truncate, mv, cp, install, ln, tee, `git mv`/`git rm`, in-place sed/perl, and the targets
-// of output redirections. It is deliberately shallow. Anything it cannot read plainly -- variables,
-// command substitutions, globs, a script that writes files itself -- contributes nothing, because
-// a wrong path would link a session to a commit it had no part in, and a missed one only leaves a
-// link out. A command line that does not parse was captured incomplete and contributes nothing.
+// of output redirections (shellcmd.FileEffects). It is deliberately shallow. Anything it cannot
+// read plainly -- variables, command substitutions, globs, a script that writes files itself --
+// contributes nothing, because a wrong path would link a session to a commit it had no part in,
+// and a missed one only leaves a link out. A command line that does not parse was captured
+// incomplete and contributes nothing.
func ShellWrittenPaths(command string) []string {
- file, err := syntax.NewParser(syntax.Variant(syntax.LangBash)).Parse(strings.NewReader(command), "")
+ commands, err := shellcmd.Parse(command)
if err != nil {
return nil
}
var paths []string
- moved := false
- syntax.Walk(file, func(node syntax.Node) bool {
- stmt, ok := node.(*syntax.Stmt)
- if !ok {
- return true
- }
- var found []string
- for _, r := range stmt.Redirs {
- if target, ok := redirectTarget(r); ok {
- found = append(found, target)
- }
- }
- call, _ := stmt.Cmd.(*syntax.CallExpr)
- var args []arg
- if call != nil {
- args = callArgs(call)
- found = append(found, commandTargets(args)...)
- }
- if moved {
- // After a cd, a relative path is relative to somewhere this parser does not track.
- found = absoluteOnly(found)
- }
- paths = append(paths, found...)
- if command := unwrapped(args); len(command) > 0 && command[0].known {
- switch command[0].text {
- case "cd", "pushd", "popd":
- moved = true
- }
- }
- return true
- })
- return dedupe(paths)
-}
-
-// arg is one word of a simple command after brace expansion. known is false when its value
-// depends on something this parser does not evaluate: a parameter, a command or arithmetic
-// substitution, a glob, or a leading tilde. An unknown word is never reported as a path.
-type arg struct {
- text string
- known bool
-}
-
-func callArgs(call *syntax.CallExpr) []arg {
- var args []arg
- for _, word := range call.Args {
- // SplitBraces rewrites the word's parts, so it works on a copy: the tree is still being walked.
- split := &syntax.Word{Parts: slices.Clone(word.Parts)}
- if !syntax.SplitBraces(split) {
- args = append(args, wordArg(word))
- continue
- }
- for _, expanded := range expand.Braces(split) {
- args = append(args, wordArg(expanded))
- }
- }
- return args
-}
-
-func wordArg(word *syntax.Word) arg {
- var b strings.Builder
- for i, part := range word.Parts {
- switch p := part.(type) {
- case *syntax.Lit:
- if i == 0 && strings.HasPrefix(p.Value, "~") {
- return arg{}
- }
- if !unescape(&b, p.Value, false) {
- return arg{}
- }
- case *syntax.SglQuoted:
- if p.Dollar {
- return arg{}
- }
- b.WriteString(p.Value)
- case *syntax.DblQuoted:
- if p.Dollar {
- return arg{}
- }
- for _, inner := range p.Parts {
- lit, ok := inner.(*syntax.Lit)
- if !ok {
- return arg{}
- }
- unescape(&b, lit.Value, true)
- }
- default:
- return arg{}
- }
- }
- return arg{text: b.String(), known: true}
-}
-
-// unescape writes a literal's value as the shell passes it on, removing the backslashes it
-// removes. Outside quotes it reports false for an unescaped glob character, whose value depends on
-// the file system.
-func unescape(b *strings.Builder, s string, quoted bool) bool {
- for i := 0; i < len(s); i++ {
- c := s[i]
- if c == '\\' && i+1 < len(s) && (!quoted || strings.IndexByte("$`\"\\\n", s[i+1]) >= 0) {
- i++
- if s[i] != '\n' {
- b.WriteByte(s[i])
- }
- continue
- }
- if !quoted && (c == '*' || c == '?' || c == '[') {
- return false
- }
- b.WriteByte(c)
- }
- return true
-}
-
-// redirectTarget returns the file an output redirection writes. Input redirections, here-documents
-// and here-strings write nothing, and neither does a duplicated descriptor (2>&1) or a device.
-func redirectTarget(r *syntax.Redirect) (string, bool) {
- switch r.Op {
- case syntax.RdrOut, syntax.AppOut, syntax.ClbOut, syntax.RdrAll, syntax.AppAll:
- case syntax.DplOut:
- // >&word writes a file unless word names a descriptor.
- if target := wordArg(r.Word); !target.known || target.text == "-" || strings.Trim(target.text, "0123456789") == "" {
- return "", false
- }
- default:
- return "", false
- }
- target := wordArg(r.Word)
- if !target.known || target.text == "" || strings.HasPrefix(target.text, "/dev/") {
- return "", false
- }
- return target.text, true
-}
-
-// unwrapped skips the wrappers that run the real command in the same directory. A wrapper option
-// that changes the directory or the command itself (sudo -i, env -C, env -S) leaves no command.
-func unwrapped(args []arg) []arg {
- for len(args) > 0 && args[0].known {
- switch args[0].text {
- case "command", "builtin", "exec", "nohup", "time":
- args = args[1:]
- case "sudo":
- args = afterAssignments(afterOptions(args[1:], wrapperSudo))
- case "env":
- args = afterAssignments(afterOptions(args[1:], wrapperEnv))
- default:
- return args
- }
- }
- return args
-}
-
-type wrapperSpec struct {
- optionSpec
- blocking string // short options that move or replace the command
- blockingLong []string // long forms of the same
-}
-
-var (
- wrapperSudo = wrapperSpec{
- optionSpec: optionSpec{short: "CghpRTtUu", long: []string{"--close-from", "--group", "--host", "--prompt", "--role", "--command-timeout", "--type", "--other-user", "--user"}},
- blocking: "DRi",
- blockingLong: []string{"--chdir", "--chroot", "--login"},
- }
- wrapperEnv = wrapperSpec{
- optionSpec: optionSpec{short: "u", long: []string{"--unset"}},
- blocking: "CS",
- blockingLong: []string{"--chdir", "--split-string"},
- }
-)
-
-// afterOptions returns the words after a wrapper's own options, or nothing when one of them
-// moves or replaces the command.
-func afterOptions(args []arg, spec wrapperSpec) []arg {
- for i := 0; i < len(args); i++ {
- a := args[i]
- switch {
- case !a.known || a.text == "-" || !strings.HasPrefix(a.text, "-"):
- return args[i:]
- case a.text == "--":
- return args[i+1:]
- case strings.HasPrefix(a.text, "--"):
- name, _, attached := strings.Cut(a.text, "=")
- if slices.Contains(spec.blockingLong, name) {
- return nil
- }
- if slices.Contains(spec.long, name) && !attached {
- i++
- }
- default:
- for j := 1; j < len(a.text); j++ {
- if strings.IndexByte(spec.blocking, a.text[j]) >= 0 {
- return nil
- }
- if strings.IndexByte(spec.short, a.text[j]) >= 0 {
- if j+1 == len(a.text) {
- i++
- }
- break
- }
- }
- }
- }
- return nil
-}
-
-func afterAssignments(args []arg) []arg {
- for len(args) > 0 && args[0].known {
- name, _, ok := strings.Cut(args[0].text, "=")
- if !ok || !isShellName(name) {
- break
- }
- args = args[1:]
- }
- return args
-}
-
-func isShellName(name string) bool {
- if name == "" {
- return false
- }
- for i, c := range name {
- if c != '_' && !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(i > 0 && c >= '0' && c <= '9') {
- return false
- }
- }
- return true
-}
-
-// optionSpec names a command's options that take a value: a short one takes the rest of its
-// cluster or the next word, a long one what follows `=` or the next word.
-type optionSpec struct {
- short string
- long []string
-}
-
-var valueOptions = map[string]optionSpec{
- "touch": {short: "dtr", long: []string{"--date", "--reference"}},
- "truncate": {short: "rs", long: []string{"--reference", "--size"}},
- "cp": {short: "St", long: []string{"--suffix", "--target-directory"}},
- "mv": {short: "St", long: []string{"--suffix", "--target-directory"}},
- "ln": {short: "St", long: []string{"--suffix", "--target-directory"}},
- "install": {short: "gmoSt", long: []string{"--group", "--mode", "--owner", "--suffix", "--target-directory"}},
-}
-
-// parseOptions splits args into operands and the values of spec's options, keyed by the short
-// letter or the long name. Everything after `--` is an operand, and so is an unknown word.
-func parseOptions(args []arg, spec optionSpec) (operands []arg, values map[string]arg) {
- values = map[string]arg{}
- for i := 0; i < len(args); i++ {
- a := args[i]
- switch {
- case !a.known || a.text == "-" || !strings.HasPrefix(a.text, "-"):
- operands = append(operands, a)
- case a.text == "--":
- return append(operands, args[i+1:]...), values
- case strings.HasPrefix(a.text, "--"):
- name, value, attached := strings.Cut(a.text, "=")
- if !slices.Contains(spec.long, name) {
+ for _, c := range commands {
+ for _, effect := range c.FileEffects() {
+ // After a cd, a relative path is relative to somewhere the parser does not track.
+ if c.AfterCD && !filepath.IsAbs(effect.Path) {
continue
}
- switch {
- case attached:
- values[name] = arg{text: value, known: true}
- case i+1 < len(args):
- i++
- values[name] = args[i]
- default:
- values[name] = arg{}
- }
- default:
- for j := 1; j < len(a.text); j++ {
- c := a.text[j]
- if strings.IndexByte(spec.short, c) < 0 {
- continue
- }
- switch {
- case j+1 < len(a.text):
- values[string(c)] = arg{text: a.text[j+1:], known: true}
- case i+1 < len(args):
- i++
- values[string(c)] = args[i]
- default:
- values[string(c)] = arg{}
- }
- break
- }
+ paths = append(paths, effect.Path)
}
}
- return operands, values
-}
-
-func commandTargets(args []arg) []string {
- args = unwrapped(args)
- if len(args) == 0 || !args[0].known {
- return nil
- }
- name := filepath.Base(args[0].text)
- args = args[1:]
- if name == "git" && len(args) > 0 && args[0].known && (args[0].text == "mv" || args[0].text == "rm") {
- name, args = "git-"+args[0].text, args[1:]
- }
- switch name {
- case "rm", "unlink", "touch", "truncate", "git-rm", "tee":
- operands, _ := parseOptions(args, valueOptions[name])
- return knownTexts(operands)
- case "mv", "git-mv", "cp", "install", "ln":
- operands, values := parseOptions(args, valueOptions[name])
- // mv's sources stop existing, so they are written too.
- moves := name == "mv" || name == "git-mv"
- dir, ok := values["t"]
- if !ok {
- dir, ok = values["--target-directory"]
- }
- if ok {
- // -t DIR makes every operand a source, written as DIR/<base name>.
- var written []string
- if moves {
- written = knownTexts(operands)
- }
- if !dir.known {
- return written
- }
- for _, src := range operands {
- if src.known {
- written = append(written, filepath.Join(dir.text, filepath.Base(src.text)))
- }
- }
- return written
- }
- if moves {
- return knownTexts(operands)
- }
- if len(operands) >= 2 {
- return knownTexts(operands[len(operands)-1:])
- }
- case "sed", "perl", "gsed":
- return inPlaceFiles(name, args)
- }
- return nil
-}
-
-// inPlaceFiles returns the files sed or perl edits in place, and nothing without -i/--in-place.
-// They are every operand when the script came in with -e/-f (attached, separate, or
-// --expression/--file), and every operand after the first, the script, otherwise.
-func inPlaceFiles(name string, args []arg) []string {
- // A script switch brings the script, attached or as the next word. -i and a valued switch take
- // the rest of the cluster as their value, so an i or e after one (-i.bak, perl -Mwarnings,
- // -Ilib) is part of that value.
- script, valued := "ef", "l"
- if name == "perl" {
- script, valued = "eE", "CDFIMdmx0"
- }
- inPlace, scripted := false, false
- var operands []arg
- for i := 0; i < len(args); i++ {
- a := args[i]
- switch {
- case !a.known || a.text == "-" || !strings.HasPrefix(a.text, "-"):
- operands = append(operands, a)
- case a.text == "--":
- operands = append(operands, args[i+1:]...)
- i = len(args)
- case a.text == "--in-place" || strings.HasPrefix(a.text, "--in-place="):
- inPlace = true
- case a.text == "--expression" || a.text == "--file":
- scripted = true
- i++
- case strings.HasPrefix(a.text, "--expression=") || strings.HasPrefix(a.text, "--file="):
- scripted = true
- case a.text == "--line-length":
- i++
- case strings.HasPrefix(a.text, "--"):
- default:
- for j := 1; j < len(a.text); j++ {
- c := a.text[j]
- if c == 'i' {
- inPlace = true
- // BSD sed reads its backup suffix from the next word; macOS agents pass ''.
- if name == "sed" && j+1 == len(a.text) && i+1 < len(args) && args[i+1].known && args[i+1].text == "" {
- i++
- }
- break
- }
- if strings.IndexByte(script, c) >= 0 {
- scripted = true
- if j+1 == len(a.text) {
- i++ // the script is the next word
- }
- break
- }
- if strings.IndexByte(valued, c) >= 0 {
- if name != "perl" && j+1 == len(a.text) {
- i++ // sed -l N
- }
- break
- }
- }
- }
- }
- if !inPlace {
- return nil
- }
- if !scripted {
- if len(operands) < 2 {
- return nil
- }
- operands = operands[1:]
- }
- return knownTexts(operands)
-}
-
-func knownTexts(args []arg) []string {
- var out []string
- for _, a := range args {
- if a.known {
- out = append(out, a.text)
- }
- }
- return out
+ return dedupe(paths)
}
// PatchPaths returns the files an apply_patch envelope touches: Add, Update, Delete, and the
@@ -459,16 +49,6 @@ func PatchPaths(patch string) []string {
return dedupe(paths)
}
-func absoluteOnly(paths []string) []string {
- var out []string
- for _, p := range paths {
- if filepath.IsAbs(p) {
- out = append(out, p)
- }
- }
- return out
-}
-
func dedupe(values []string) []string {
seen := map[string]bool{}
out := values[:0]
diff --git a/cli/beacon/internal/gitlink/shell_test.go b/cli/beacon/internal/gitlink/shell_test.go
index 64844b76..d74c08e9 100644
--- a/cli/beacon/internal/gitlink/shell_test.go
+++ b/cli/beacon/internal/gitlink/shell_test.go
@@ -72,7 +72,8 @@ func TestShellWrittenPaths(t *testing.T) {
{"echo $((1<<3)) > n.txt\nrm old.txt", []string{"n.txt", "old.txt"}},
{"grep x <<< \"$v\"\nrm a.txt", []string{"a.txt"}},
{"git commit -m \"$(cat <<'EOF'\nrm -rf dist\nEOF\n)\"", nil},
- {"rm a.txt 'unterminated", nil}, // a command that does not parse was captured incomplete
+ {"rm a.txt 'unterminated", nil}, // a command that does not parse was captured incomplete
+ {"rm a.txt && cat <<EOF\ncut short", nil}, // nor does one that ends inside a here-document
{"FOO=1 sudo rm build/out", []string{"build/out"}},
{`rm "file with spaces.txt"`, []string{"file with spaces.txt"}},
{`rm file\ two.txt`, []string{"file two.txt"}},
diff --git a/pkg/asymptoteobserve/go.mod b/pkg/asymptoteobserve/go.mod
index c89ca32b..a1c20583 100644
--- a/pkg/asymptoteobserve/go.mod
+++ b/pkg/asymptoteobserve/go.mod
@@ -6,8 +6,9 @@ toolchain go1.27.3
require (
github.com/google/cel-go v0.30.0
- golang.org/x/sys v0.44.0
+ golang.org/x/sys v0.47.0
gopkg.in/yaml.v3 v3.0.1
+ mvdan.cc/sh/v3 v3.14.1
)
require (
diff --git a/pkg/asymptoteobserve/go.sum b/pkg/asymptoteobserve/go.sum
index f515900e..324b158b 100644
--- a/pkg/asymptoteobserve/go.sum
+++ b/pkg/asymptoteobserve/go.sum
@@ -2,16 +2,24 @@ cel.dev/expr v0.25.1 h1:1KrZg61W6TWSxuNZ37Xy49ps13NUovb66QLprthtwi4=
cel.dev/expr v0.25.1/go.mod h1:hrXvqGP6G6gyx8UAHSHJ5RGk//1Oj5nXQ2NI02Nrsg4=
github.com/antlr4-go/antlr/v4 v4.13.1 h1:SqQKkuVZ+zWkMMNkjy5FZe5mr5WURWnlpmOuzYWrPrQ=
github.com/antlr4-go/antlr/v4 v4.13.1/go.mod h1:GKmUxMtwp6ZgGwZSva4eWPC5mS6vUAmOABFgjdkM7Nw=
+github.com/go-quicktest/qt v1.102.0 h1:HSQxCeh5YZH3EL3W39ixjtyaEhcWSXQHtHnMBzSs474=
+github.com/go-quicktest/qt v1.102.0/go.mod h1:p4lGIVX+8Wa6ZPNDvqcxq36XpUDLh42FLetFU7odllI=
github.com/google/cel-go v0.30.0 h1:ll54AkzKunWkBn9wSoiUXbFZXYZTkdJGNXTBXUoolGo=
github.com/google/cel-go v0.30.0/go.mod h1:X0bD6iVNR8pkROSOoHVdgTkzmRcosof7WQqCD6wcMc8=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
+github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
+github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
+github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
+github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
+github.com/rogpeppe/go-internal v1.15.0 h1:D0RCU5rMAp+SpgkiNdrjfJ+LX4J1M32V2NeCY7EJ6hc=
+github.com/rogpeppe/go-internal v1.15.0/go.mod h1:DrUVZyrJU+txYW5/1kwtXQSMFio52ZOxX7yM1VHvnxs=
go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc=
go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 h1:kx6Ds3MlpiUHKj7syVnbp57++8WpuKPcR5yjLBjvLEA=
golang.org/x/exp v0.0.0-20240823005443-9b4947da3948/go.mod h1:akd2r19cwCdwSwWeIdzYQGa/EZZyqcOdwWiwj5L5eKQ=
-golang.org/x/sys v0.44.0 h1:ildZl3J4uzeKP07r2F++Op7E9B29JRUy+a27EibtBTQ=
-golang.org/x/sys v0.44.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
+golang.org/x/sys v0.47.0 h1:o7XGOvZQCADBQQ4Y7VNq2dRWQR7JmOUW8Kxx4ZsNgWs=
+golang.org/x/sys v0.47.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/text v0.41.0 h1:vz/seA0lnX87Othu2f/0L24RcgrXD9/YFTSuGjj3rH8=
golang.org/x/text v0.41.0/go.mod h1:jvf1O8ajNzZqhSrQBPbutR/EB83Cc0CFrezNQIwbb5M=
google.golang.org/genproto/googleapis/api v0.0.0-20240826202546-f6391c0de4c7 h1:YcyjlL1PRr2Q17/I0dPk2JmYS5CDXfcdb2Z3YRioEbw=
@@ -24,3 +32,5 @@ gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+mvdan.cc/sh/v3 v3.14.1 h1:bXkhQWNHCs0KZEChF8hYS6FC+T2N9mUZLbQv9blditI=
+mvdan.cc/sh/v3 v3.14.1/go.mod h1:syYCoFET8w9tvevxiXUtY8/ICrU+l26jHmhJDra3Vwo=
diff --git a/pkg/asymptoteobserve/shellcmd/command.go b/pkg/asymptoteobserve/shellcmd/command.go
new file mode 100644
index 00000000..9c2ae079
--- /dev/null
+++ b/pkg/asymptoteobserve/shellcmd/command.go
@@ -0,0 +1,139 @@
+package shellcmd
+
+import (
+ "path/filepath"
+ "slices"
+ "strings"
+)
+
+// Unwrapped returns the command's words after the wrappers that run the real command in the same
+// directory: command, builtin, exec, nohup and time, and sudo and env with their options and
+// NAME=value words. ok is false, with no words, when a wrapper option moves or replaces the
+// command (sudo -i/-D/-R, env -C/-S and their long forms). It stops at the first unknown word.
+func (c Command) Unwrapped() (words []Word, ok bool) {
+ words = c.Argv
+ for len(words) > 0 && words[0].Known {
+ switch words[0].Text {
+ case "command", "builtin", "exec", "nohup", "time":
+ words = words[1:]
+ case "sudo":
+ if words, ok = afterOptions(words[1:], wrapperSudo); !ok {
+ return nil, false
+ }
+ words = afterAssignments(words)
+ case "env":
+ if words, ok = afterOptions(words[1:], wrapperEnv); !ok {
+ return nil, false
+ }
+ words = afterAssignments(words)
+ default:
+ return words, true
+ }
+ }
+ return words, true
+}
+
+// Name is the base name of the unwrapped command (rm for /bin/rm), or "" when there is none or
+// it is unknown. The base name is the host's, as filepath.Base reads it.
+func (c Command) Name() string {
+ words, _ := c.Unwrapped()
+ if len(words) == 0 || !words[0].Known || words[0].Text == "" {
+ return ""
+ }
+ return filepath.Base(words[0].Text)
+}
+
+// Is reports whether the unwrapped command is name, followed by the known words sub
+// (Is("git", "mv") for git mv a b).
+func (c Command) Is(name string, sub ...string) bool {
+ if c.Name() != name {
+ return false
+ }
+ words, _ := c.Unwrapped()
+ if len(words) < 1+len(sub) {
+ return false
+ }
+ for i, s := range sub {
+ if w := words[1+i]; !w.Known || w.Text != s {
+ return false
+ }
+ }
+ return true
+}
+
+type wrapperSpec struct {
+ optionSpec
+ blocking string // short options that move or replace the command
+ blockingLong []string // long forms of the same
+}
+
+var (
+ wrapperSudo = wrapperSpec{
+ optionSpec: optionSpec{short: "CghpRTtUu", long: []string{"--close-from", "--group", "--host", "--prompt", "--role", "--command-timeout", "--type", "--other-user", "--user"}},
+ blocking: "DRi",
+ blockingLong: []string{"--chdir", "--chroot", "--login"},
+ }
+ wrapperEnv = wrapperSpec{
+ optionSpec: optionSpec{short: "u", long: []string{"--unset"}},
+ blocking: "CS",
+ blockingLong: []string{"--chdir", "--split-string"},
+ }
+)
+
+// afterOptions returns the words after a wrapper's own options. ok is false when one of them
+// moves or replaces the command.
+func afterOptions(words []Word, spec wrapperSpec) ([]Word, bool) {
+ for i := 0; i < len(words); i++ {
+ w := words[i]
+ switch {
+ case !w.Known || w.Text == "-" || !strings.HasPrefix(w.Text, "-"):
+ return words[i:], true
+ case w.Text == "--":
+ return words[i+1:], true
+ case strings.HasPrefix(w.Text, "--"):
+ name, _, attached := strings.Cut(w.Text, "=")
+ if slices.Contains(spec.blockingLong, name) {
+ return nil, false
+ }
+ if slices.Contains(spec.long, name) && !attached {
+ i++
+ }
+ default:
+ for j := 1; j < len(w.Text); j++ {
+ if strings.IndexByte(spec.blocking, w.Text[j]) >= 0 {
+ return nil, false
+ }
+ if strings.IndexByte(spec.short, w.Text[j]) >= 0 {
+ if j+1 == len(w.Text) {
+ i++
+ }
+ break
+ }
+ }
+ }
+ }
+ return nil, true
+}
+
+func afterAssignments(words []Word) []Word {
+ for len(words) > 0 && words[0].Known {
+ name, _, ok := strings.Cut(words[0].Text, "=")
+ if !ok || !isShellName(name) {
+ break
+ }
+ words = words[1:]
+ }
+ return words
+}
+
+func isShellName(name string) bool {
+ if name == "" {
+ return false
+ }
+ for i, c := range name {
+ if c != '_' && !(c >= 'a' && c <= 'z') && !(c >= 'A' && c <= 'Z') && !(i > 0 && c >= '0' && c <= '9') {
+ return false
+ }
+ }
+ return true
+}
diff --git a/pkg/asymptoteobserve/shellcmd/effects.go b/pkg/asymptoteobserve/shellcmd/effects.go
new file mode 100644
index 00000000..9704f6e7
--- /dev/null
+++ b/pkg/asymptoteobserve/shellcmd/effects.go
@@ -0,0 +1,329 @@
+package shellcmd
+
+import (
+ "path"
+ "slices"
+ "strings"
+)
+
+// Op is what a command evidently does to a file.
+type Op string
+
+const (
+ // OpWrite creates or replaces a file's content: >, >|, &>, >&file, tee, the destination of cp
+ // and install.
+ OpWrite Op = "write"
+ // OpAppend adds to a file: >>, &>>, tee -a.
+ OpAppend Op = "append"
+ // OpTouch creates a file or updates its times (touch).
+ OpTouch Op = "touch"
+ // OpTruncate sets a file's size (truncate).
+ OpTruncate Op = "truncate"
+ // OpEdit edits a file in place (sed -i, perl -i).
+ OpEdit Op = "edit"
+ // OpDelete removes a file (rm, unlink, git rm).
+ OpDelete Op = "delete"
+ // OpMoveFrom is a source of mv or git mv, which stops existing.
+ OpMoveFrom Op = "move_from"
+ // OpMoveTo is the destination of mv or git mv.
+ OpMoveTo Op = "move_to"
+ // OpLink is a link ln creates.
+ OpLink Op = "link"
+)
+
+// FileEffect is a file a command evidently writes, and how.
+type FileEffect struct {
+ Path string
+ Op Op
+}
+
+// FileEffects returns the file effects of every command, in order.
+func FileEffects(commands []Command) []FileEffect {
+ var effects []FileEffect
+ for _, c := range commands {
+ effects = append(effects, c.FileEffects()...)
+ }
+ return effects
+}
+
+// FileEffects returns the files the command evidently writes: the targets of its output
+// redirections, then the operands of rm, unlink, touch, truncate, mv, cp, install, ln, tee,
+// `git mv`/`git rm` and in-place sed/perl. Anything it cannot read plainly -- an unknown word, a
+// command that writes files itself -- contributes nothing, and neither does a wrapper that moves
+// the command (sudo -i). Paths are as written: relative to the directory the command runs in,
+// which after a cd (AfterCD) is not the one the command line started in.
+func (c Command) FileEffects() []FileEffect {
+ var effects []FileEffect
+ for _, r := range c.Redirects {
+ if path, op, ok := redirectTarget(r); ok {
+ effects = append(effects, FileEffect{Path: path, Op: op})
+ }
+ }
+ for _, e := range c.commandTargets() {
+ if e.Path != "" {
+ effects = append(effects, e)
+ }
+ }
+ return effects
+}
+
+// redirectTarget returns the file an output redirection writes. Input redirections, here-documents
+// and here-strings write nothing, and neither does a duplicated descriptor (2>&1) or a device.
+func redirectTarget(r Redirect) (string, Op, bool) {
+ op := OpWrite
+ switch r.Op {
+ case ">", ">|", "&>":
+ case ">>", "&>>":
+ op = OpAppend
+ case ">&":
+ // >&word writes a file unless word names a descriptor.
+ if t := r.Target; !t.Known || t.Text == "-" || strings.Trim(t.Text, "0123456789") == "" {
+ return "", "", false
+ }
+ default:
+ return "", "", false
+ }
+ t := r.Target
+ if !t.Known || t.Text == "" || strings.HasPrefix(t.Text, "/dev/") {
+ return "", "", false
+ }
+ return t.Text, op, true
+}
+
+// optionSpec names a command's options that take a value: a short one takes the rest of its
+// cluster or the next word, a long one what follows `=` or the next word.
+type optionSpec struct {
+ short string
+ long []string
+}
+
+var valueOptions = map[string]optionSpec{
+ "touch": {short: "dtr", long: []string{"--date", "--reference"}},
+ "truncate": {short: "rs", long: []string{"--reference", "--size"}},
+ "cp": {short: "St", long: []string{"--suffix", "--target-directory"}},
+ "mv": {short: "St", long: []string{"--suffix", "--target-directory"}},
+ "ln": {short: "St", long: []string{"--suffix", "--target-directory"}},
+ "install": {short: "gmoSt", long: []string{"--group", "--mode", "--owner", "--suffix", "--target-directory"}},
+}
+
+// flagOptions are the value-less options that change what a command does to its operands.
+var flagOptions = map[string]optionSpec{
+ "tee": {short: "a", long: []string{"--append"}},
+}
+
+// parsedOptions are a command's operands, the values of its value options keyed by the short
+// letter or the long name, and the flag options it was given.
+type parsedOptions struct {
+ operands []Word
+ values map[string]Word
+ flags map[string]bool
+}
+
+// parseOptions splits words into operands, the values of spec's options and the flags of flags.
+// Everything after `--` is an operand, and so is an unknown word.
+func parseOptions(words []Word, spec, flags optionSpec) parsedOptions {
+ p := parsedOptions{values: map[string]Word{}, flags: map[string]bool{}}
+ for i := 0; i < len(words); i++ {
+ w := words[i]
+ switch {
+ case !w.Known || w.Text == "-" || !strings.HasPrefix(w.Text, "-"):
+ p.operands = append(p.operands, w)
+ case w.Text == "--":
+ p.operands = append(p.operands, words[i+1:]...)
+ return p
+ case strings.HasPrefix(w.Text, "--"):
+ name, value, attached := strings.Cut(w.Text, "=")
+ if slices.Contains(flags.long, name) {
+ p.flags[name] = true
+ }
+ if !slices.Contains(spec.long, name) {
+ continue
+ }
+ switch {
+ case attached:
+ p.values[name] = Word{Text: value, Known: true}
+ case i+1 < len(words):
+ i++
+ p.values[name] = words[i]
+ default:
+ p.values[name] = Word{}
+ }
+ default:
+ for j := 1; j < len(w.Text); j++ {
+ c := w.Text[j]
+ if strings.IndexByte(flags.short, c) >= 0 {
+ p.flags[string(c)] = true
+ }
+ if strings.IndexByte(spec.short, c) < 0 {
+ continue
+ }
+ switch {
+ case j+1 < len(w.Text):
+ p.values[string(c)] = Word{Text: w.Text[j+1:], Known: true}
+ case i+1 < len(words):
+ i++
+ p.values[string(c)] = words[i]
+ default:
+ p.values[string(c)] = Word{}
+ }
+ break
+ }
+ }
+ }
+ return p
+}
+
+func (c Command) commandTargets() []FileEffect {
+ name := c.Name()
+ if name == "" {
+ return nil
+ }
+ words, _ := c.Unwrapped()
+ args := words[1:]
+ if name == "git" && (c.Is("git", "mv") || c.Is("git", "rm")) {
+ name, args = "git-"+args[0].Text, args[1:]
+ }
+ switch name {
+ case "rm", "unlink", "git-rm":
+ return effectsOf(parseOptions(args, valueOptions[name], optionSpec{}).operands, OpDelete)
+ case "touch":
+ return effectsOf(parseOptions(args, valueOptions[name], optionSpec{}).operands, OpTouch)
+ case "truncate":
+ return effectsOf(parseOptions(args, valueOptions[name], optionSpec{}).operands, OpTruncate)
+ case "tee":
+ opts := parseOptions(args, valueOptions[name], flagOptions[name])
+ if opts.flags["a"] || opts.flags["--append"] {
+ return effectsOf(opts.operands, OpAppend)
+ }
+ return effectsOf(opts.operands, OpWrite)
+ case "mv", "git-mv", "cp", "install", "ln":
+ opts := parseOptions(args, valueOptions[name], optionSpec{})
+ // mv's sources stop existing, so they are written too.
+ moves := name == "mv" || name == "git-mv"
+ dest := OpWrite
+ switch {
+ case moves:
+ dest = OpMoveTo
+ case name == "ln":
+ dest = OpLink
+ }
+ dir, ok := opts.values["t"]
+ if !ok {
+ dir, ok = opts.values["--target-directory"]
+ }
+ if ok {
+ // -t DIR makes every operand a source, written as DIR/<base name>.
+ var written []FileEffect
+ if moves {
+ written = effectsOf(opts.operands, OpMoveFrom)
+ }
+ if !dir.Known {
+ return written
+ }
+ // The command line is shell syntax, so its paths join with "/" on every host; gitlink's
+ // RelPath converts them for the local filesystem.
+ for _, src := range opts.operands {
+ if src.Known {
+ written = append(written, FileEffect{Path: path.Join(dir.Text, path.Base(src.Text)), Op: dest})
+ }
+ }
+ return written
+ }
+ if moves {
+ // Every operand but the last is a source. When the last is unknown, so is the
+ // destination, and every known operand is a source.
+ effects := effectsOf(opts.operands, OpMoveFrom)
+ if n := len(opts.operands); n >= 2 && opts.operands[n-1].Known {
+ effects[len(effects)-1].Op = OpMoveTo
+ }
+ return effects
+ }
+ if len(opts.operands) >= 2 {
+ return effectsOf(opts.operands[len(opts.operands)-1:], dest)
+ }
+ case "sed", "perl", "gsed":
+ return effectsOf(inPlaceFiles(name, args), OpEdit)
+ }
+ return nil
+}
+
+// inPlaceFiles returns the files sed or perl edits in place, and nothing without -i/--in-place.
+// They are every operand when the script came in with -e/-f (attached, separate, or
+// --expression/--file), and every operand after the first, the script, otherwise.
+func inPlaceFiles(name string, args []Word) []Word {
+ // A script switch brings the script, attached or as the next word. -i and a valued switch take
+ // the rest of the cluster as their value, so an i or e after one (-i.bak, perl -Mwarnings,
+ // -Ilib) is part of that value.
+ script, valued := "ef", "l"
+ if name == "perl" {
+ script, valued = "eE", "CDFIMdmx0"
+ }
+ inPlace, scripted := false, false
+ var operands []Word
+ for i := 0; i < len(args); i++ {
+ a := args[i]
+ switch {
+ case !a.Known || a.Text == "-" || !strings.HasPrefix(a.Text, "-"):
+ operands = append(operands, a)
+ case a.Text == "--":
+ operands = append(operands, args[i+1:]...)
+ i = len(args)
+ case a.Text == "--in-place" || strings.HasPrefix(a.Text, "--in-place="):
+ inPlace = true
+ case a.Text == "--expression" || a.Text == "--file":
+ scripted = true
+ i++
+ case strings.HasPrefix(a.Text, "--expression=") || strings.HasPrefix(a.Text, "--file="):
+ scripted = true
+ case a.Text == "--line-length":
+ i++
+ case strings.HasPrefix(a.Text, "--"):
+ default:
+ for j := 1; j < len(a.Text); j++ {
+ c := a.Text[j]
+ if c == 'i' {
+ inPlace = true
+ // BSD sed reads its backup suffix from the next word; macOS agents pass ''.
+ if name == "sed" && j+1 == len(a.Text) && i+1 < len(args) && args[i+1].Known && args[i+1].Text == "" {
+ i++
+ }
+ break
+ }
+ if strings.IndexByte(script, c) >= 0 {
+ scripted = true
+ if j+1 == len(a.Text) {
+ i++ // the script is the next word
+ }
+ break
+ }
+ if strings.IndexByte(valued, c) >= 0 {
+ if name != "perl" && j+1 == len(a.Text) {
+ i++ // sed -l N
+ }
+ break
+ }
+ }
+ }
+ }
+ if !inPlace {
+ return nil
+ }
+ if !scripted {
+ if len(operands) < 2 {
+ return nil
+ }
+ operands = operands[1:]
+ }
+ return operands
+}
+
+// effectsOf are the known words of words, each with op.
+func effectsOf(words []Word, op Op) []FileEffect {
+ var out []FileEffect
+ for _, w := range words {
+ if w.Known {
+ out = append(out, FileEffect{Path: w.Text, Op: op})
+ }
+ }
+ return out
+}
diff --git a/pkg/asymptoteobserve/shellcmd/shellcmd.go b/pkg/asymptoteobserve/shellcmd/shellcmd.go
new file mode 100644
index 00000000..7e81c2a5
--- /dev/null
+++ b/pkg/asymptoteobserve/shellcmd/shellcmd.go
@@ -0,0 +1,320 @@
+// Package shellcmd reads a captured shell command line as bash does: the simple commands it runs,
+// their words, environment prefixes and redirections, and the files those commands evidently
+// write. It is deliberately shallow. A word whose value depends on something it does not evaluate
+// -- a parameter, a command or arithmetic substitution, a glob, a leading tilde -- is marked
+// unknown rather than guessed, and a command line that does not parse is an error, because it was
+// most likely captured incomplete.
+//
+// It lives apart from the root package so that the binaries that import only the root package do
+// not link the shell parser.
+package shellcmd
+
+import (
+ "errors"
+ "slices"
+ "strings"
+
+ "mvdan.cc/sh/v3/expand"
+ "mvdan.cc/sh/v3/syntax"
+)
+
+// Word is one word of a command after brace expansion.
+type Word struct {
+ // Text is the value as the shell passes it on, quotes and escaping backslashes removed. It is
+ // empty when Known is false.
+ Text string
+ // Raw is the word's source text, quotes and backslashes included. Every word a brace
+ // expansion produces carries the source of the whole unexpanded word. Raw keeps what bash
+ // would remove, such as the backslashes of a Windows path (C:\Tools\beacon.exe, whose Text
+ // is C:Toolsbeacon.exe).
+ Raw string
+ // Known is false when the value depends on a parameter, a command or arithmetic substitution,
+ // an ANSI-C ($'..') or locale ($"..") string, an unquoted glob character, or a leading tilde.
+ Known bool
+}
+
+// Assign is an environment assignment before a command (NAME=value cmd).
+type Assign struct {
+ Name string
+ // Value is unknown for an append (+=), an indexed or an array assignment.
+ Value Word
+}
+
+// Redirect is one redirection of a command.
+type Redirect struct {
+ // Op is the operator as written: ">", ">>", ">|", "&>", "&>>", ">&", "<", "<>", "<&", "<<",
+ // "<<-" or "<<<".
+ Op string
+ // Fd is the descriptor written before the operator (the 2 of 2>), or "".
+ Fd string
+ // Target is the word after the operator: a file, a descriptor for >& and <&, the delimiter
+ // of a here-document, or the string of a here-string. A here-document's body is not read.
+ Target Word
+}
+
+// Command is one statement of a command line: a simple command with its words, or a compound
+// one (a subshell, block, if, loop) that has redirections of its own, in which case Argv is empty.
+type Command struct {
+ // Env are the assignments before the command.
+ Env []Assign
+ // Argv are the command's words, wrappers (sudo, env, exec, ...) included. Unwrapped skips them.
+ Argv []Word
+ Redirects []Redirect
+ // Offset is the byte offset of the statement in the command line.
+ Offset int
+ // Pipeline identifies the pipeline the statement is a stage of: the stages of one pipeline
+ // share it, and a statement outside a pipeline has one of its own. Stage is its 0-based
+ // position in that pipeline (curl ... | sh: sh is stage 1). A command inside a compound stage
+ // ({ a; b; } | c) is not itself a stage of the outer pipeline.
+ Pipeline int
+ Stage int
+ // Nested is true inside a command substitution ($(...), `...`) or a process substitution.
+ Nested bool
+ // AfterCD is true when a cd, pushd or popd comes earlier in the command line, so a relative
+ // path no longer names the file it would at the start. The directory itself is not tracked.
+ AfterCD bool
+}
+
+// Parse parses command as bash and returns its statements in walk order: each statement, then the
+// statements inside it (an if's body, a command substitution in its words). A statement that
+// carries nothing -- no simple command and no redirection, such as the `a && b` around its two
+// sides -- is left out.
+//
+// A command line that does not parse is an error. When the input merely ended early (an unclosed
+// quote, here-document, substitution or compound command: a capture cut short), Parse also
+// returns the commands of the part before the point where it became incomplete, parsed on its
+// own; the last of them may be missing words that the cut removed. Any other parse error returns
+// no commands. A caller that must not act on a partial command line ignores the commands
+// whenever the error is not nil.
+func Parse(command string) ([]Command, error) {
+ file, err := parseBash(command)
+ if err == nil {
+ return walk(command, file), nil
+ }
+ // The prefix can itself end inside an earlier construct (`a "$(b` cut again at `$(`); each
+ // retry is strictly shorter, so this ends.
+ prefix, perr := command, err
+ for {
+ var incomplete syntax.ParseError
+ if !errors.As(perr, &incomplete) || !incomplete.Incomplete || int(incomplete.Pos.Offset()) >= len(prefix) {
+ return nil, err
+ }
+ prefix = prefix[:incomplete.Pos.Offset()]
+ if file, perr = parseBash(prefix); perr == nil {
+ return walk(prefix, file), err
+ }
+ }
+}
+
+func parseBash(command string) (*syntax.File, error) {
+ return syntax.NewParser(syntax.Variant(syntax.LangBash)).Parse(strings.NewReader(command), "")
+}
+
+func walk(source string, file *syntax.File) []Command {
+ p := parser{source: source, stages: map[*syntax.Stmt]stage{}, containers: map[*syntax.Stmt]bool{}}
+ syntax.Walk(file, p.visit)
+ return p.commands
+}
+
+type stage struct{ pipeline, index int }
+
+type parser struct {
+ source string
+ commands []Command
+ stages map[*syntax.Stmt]stage
+ containers map[*syntax.Stmt]bool // statements that only join pipeline stages
+ pipelines int
+ nested [][2]uint // byte ranges of command and process substitutions
+ moved bool
+}
+
+func (p *parser) visit(node syntax.Node) bool {
+ switch n := node.(type) {
+ case *syntax.CmdSubst:
+ p.nested = append(p.nested, [2]uint{n.Left.Offset(), n.Right.Offset()})
+ case *syntax.ProcSubst:
+ p.nested = append(p.nested, [2]uint{n.OpPos.Offset(), n.Rparen.Offset()})
+ case *syntax.Stmt:
+ p.stmt(n)
+ }
+ return true
+}
+
+func (p *parser) stmt(stmt *syntax.Stmt) {
+ if p.containers[stmt] {
+ return
+ }
+ if isPipe(stmt) {
+ p.pipelines++
+ for i, s := range p.flatten(stmt, nil) {
+ p.stages[s] = stage{pipeline: p.pipelines, index: i}
+ }
+ return
+ }
+ call, _ := stmt.Cmd.(*syntax.CallExpr)
+ if call == nil && len(stmt.Redirs) == 0 {
+ return
+ }
+ st, ok := p.stages[stmt]
+ if !ok {
+ p.pipelines++
+ st = stage{pipeline: p.pipelines}
+ }
+ offset := stmt.Pos().Offset()
+ c := Command{
+ Offset: int(offset),
+ Pipeline: st.pipeline,
+ Stage: st.index,
+ AfterCD: p.moved,
+ }
+ for _, r := range p.nested {
+ if offset > r[0] && offset < r[1] {
+ c.Nested = true
+ break
+ }
+ }
+ if call != nil {
+ for _, as := range call.Assigns {
+ c.Env = append(c.Env, p.assign(as))
+ }
+ c.Argv = p.callArgs(call)
+ }
+ for _, r := range stmt.Redirs {
+ c.Redirects = append(c.Redirects, p.redirect(r))
+ }
+ p.commands = append(p.commands, c)
+ // After a cd, a relative path is relative to somewhere this parser does not track.
+ if words, _ := c.Unwrapped(); len(words) > 0 && words[0].Known {
+ switch words[0].Text {
+ case "cd", "pushd", "popd":
+ p.moved = true
+ }
+ }
+}
+
+func isPipe(stmt *syntax.Stmt) bool {
+ bin, ok := stmt.Cmd.(*syntax.BinaryCmd)
+ return ok && (bin.Op == syntax.Pipe || bin.Op == syntax.PipeAll)
+}
+
+// flatten returns the stages of the pipeline stmt, marking the statements that only join them.
+func (p *parser) flatten(stmt *syntax.Stmt, stages []*syntax.Stmt) []*syntax.Stmt {
+ if !isPipe(stmt) {
+ return append(stages, stmt)
+ }
+ p.containers[stmt] = true
+ bin := stmt.Cmd.(*syntax.BinaryCmd)
+ return p.flatten(bin.Y, p.flatten(bin.X, stages))
+}
+
+func (p *parser) raw(node syntax.Node) string {
+ start, end := node.Pos().Offset(), node.End().Offset()
+ if start > end || end > uint(len(p.source)) {
+ return ""
+ }
+ return p.source[start:end]
+}
+
+func (p *parser) assign(as *syntax.Assign) Assign {
+ a := Assign{Name: as.Name.Value}
+ switch {
+ case as.Append || as.Index != nil || as.Array != nil:
+ if as.Value != nil {
+ a.Value.Raw = p.raw(as.Value)
+ }
+ case as.Value == nil:
+ a.Value.Known = true
+ default:
+ a.Value = p.word(as.Value, as.Value)
+ }
+ return a
+}
+
+func (p *parser) redirect(r *syntax.Redirect) Redirect {
+ out := Redirect{Op: r.Op.String()}
+ if r.N != nil {
+ out.Fd = r.N.Value
+ }
+ if r.Word != nil {
+ out.Target = p.word(r.Word, r.Word)
+ }
+ return out
+}
+
+func (p *parser) callArgs(call *syntax.CallExpr) []Word {
+ var words []Word
+ for _, word := range call.Args {
+ // SplitBraces rewrites the word's parts, so it works on a copy: the tree is still being walked.
+ split := &syntax.Word{Parts: slices.Clone(word.Parts)}
+ if !syntax.SplitBraces(split) {
+ words = append(words, p.word(word, word))
+ continue
+ }
+ for _, expanded := range expand.Braces(split) {
+ words = append(words, p.word(expanded, word))
+ }
+ }
+ return words
+}
+
+// word reads word's value; source is the word as written, for Raw.
+func (p *parser) word(word, source *syntax.Word) Word {
+ w := Word{Raw: p.raw(source)}
+ w.Text, w.Known = wordText(word)
+ return w
+}
+
+func wordText(word *syntax.Word) (string, bool) {
+ var b strings.Builder
+ for i, part := range word.Parts {
+ switch p := part.(type) {
+ case *syntax.Lit:
+ if i == 0 && strings.HasPrefix(p.Value, "~") {
+ return "", false
+ }
+ if !unescape(&b, p.Value, false) {
+ return "", false
+ }
+ case *syntax.SglQuoted:
+ if p.Dollar {
+ return "", false
+ }
+ b.WriteString(p.Value)
+ case *syntax.DblQuoted:
+ if p.Dollar {
+ return "", false
+ }
+ for _, inner := range p.Parts {
+ lit, ok := inner.(*syntax.Lit)
+ if !ok {
+ return "", false
+ }
+ unescape(&b, lit.Value, true)
+ }
+ default:
+ return "", false
+ }
+ }
+ return b.String(), true
+}
+
+// unescape writes a literal's value as the shell passes it on, removing the backslashes it
+// removes. Outside quotes it reports false for an unescaped glob character, whose value depends on
+// the file system.
+func unescape(b *strings.Builder, s string, quoted bool) bool {
+ for i := 0; i < len(s); i++ {
+ c := s[i]
+ if c == '\\' && i+1 < len(s) && (!quoted || strings.IndexByte("$`\"\\\n", s[i+1]) >= 0) {
+ i++
+ if s[i] != '\n' {
+ b.WriteByte(s[i])
+ }
+ continue
+ }
+ if !quoted && (c == '*' || c == '?' || c == '[') {
+ return false
+ }
+ b.WriteByte(c)
+ }
+ return true
+}
diff --git a/pkg/asymptoteobserve/shellcmd/shellcmd_test.go b/pkg/asymptoteobserve/shellcmd/shellcmd_test.go
new file mode 100644
index 00000000..09fddcdb
--- /dev/null
+++ b/pkg/asymptoteobserve/shellcmd/shellcmd_test.go
@@ -0,0 +1,305 @@
+package shellcmd
+
+import (
+ "reflect"
+ "strings"
+ "testing"
+)
+
+// TestFileEffects pins the file rules gitlink.ShellWrittenPaths reads. An effect of a command
+// after a cd is written "cd <op> <path>".
+func TestFileEffects(t *testing.T) {
+ for _, tc := range []struct {
+ command string
+ want []string
+ }{
+ {"rm -f a.txt b.txt", []string{"delete a.txt", "delete b.txt"}},
+ {"rm -rf -- -weird", []string{"delete -weird"}},
+ {"touch new.go && go test ./...", []string{"touch new.go"}},
+ {"mv old.go new.go", []string{"move_from old.go", "move_to new.go"}},
+ {"git mv src/a.go src/b.go", []string{"move_from src/a.go", "move_to src/b.go"}},
+ {"git rm --cached secret.env", []string{"delete secret.env"}},
+ {"cp -r template out/dir", []string{"write out/dir"}},
+ {"cp -t out a.txt src/b.txt", []string{"write out/a.txt", "write out/b.txt"}},
+ {"cp --target-directory=out a.txt", []string{"write out/a.txt"}},
+ {"install -Dt bin tool", []string{"write bin/tool"}},
+ {"ln -s -t links ../real.txt", []string{"link links/real.txt"}},
+ {"install -m 0755 -t /usr/local/bin build/tool", []string{"write /usr/local/bin/tool"}},
+ {"install -o root -g wheel -t /etc/app conf.yaml", []string{"write /etc/app/conf.yaml"}},
+ {"install -m 0644 conf.yaml /etc/app/conf.yaml", []string{"write /etc/app/conf.yaml"}},
+ {`cp -t "$DEST" a.txt`, nil},
+ {`cp -t out "$SRC" a.txt`, []string{"write out/a.txt"}},
+ {"mv -t archive a.txt b.txt", []string{"move_from a.txt", "move_from b.txt", "move_to archive/a.txt", "move_to archive/b.txt"}},
+ {"truncate -s 0 app.log", []string{"truncate app.log"}},
+ {"touch -d '2 hours ago' stamp", []string{"touch stamp"}},
+ {"echo hi > out.txt", []string{"write out.txt"}},
+ {"echo hi >out.txt 2>&1", []string{"write out.txt"}},
+ {"printf x >> log.txt; cat log.txt", []string{"append log.txt"}},
+ {"cmd >| forced.txt", []string{"write forced.txt"}},
+ {"go build &> build.log", []string{"write build.log"}},
+ {"make 2>/dev/null", nil},
+ {"cat a.txt | tee copy.txt", []string{"write copy.txt"}},
+ {"sed -i 's/a/b/' x.go y.go", []string{"edit x.go", "edit y.go"}},
+ {"sed -i.bak -e 's/a/b/' x.go", []string{"edit x.go"}},
+ {"sed 's/a/b/' x.go", nil}, // not in place: prints, writes nothing
+ {"perl -pi -e 's/a/b/' z.pl", []string{"edit z.pl"}},
+ {"perl -Mwarnings -ne 'print if /x/' z.pl", nil}, // -M's module name is not -i
+ {"perl -Ilib -ne 'print' z.pl", nil},
+ {"perl -MList::Util=sum -lane 'print sum @F' data.txt", nil},
+ {"perl -Mstrict -pi -e 's/a/b/' z.pl", []string{"edit z.pl"}},
+ {"perl -0777 -pi -e 's/a/b/' z.pl", []string{"edit z.pl"}},
+ {"perl -lpi -e 's/a/b/' z.pl", []string{"edit z.pl"}},
+ {"sed -n -es/i/j/p x.go y.go", nil}, // -e's script is not -i
+ {"perl -pi -e's/a/b/' z.pl", []string{"edit z.pl"}},
+ {"sed -i -e's/a/b/' x.go", []string{"edit x.go"}},
+ {"sed -i --expression=s/a/b/ x.go", []string{"edit x.go"}},
+ {"sed -i -e 's/a/b/' -e 's/c/d/' x.go", []string{"edit x.go"}},
+ {"sed -i -f fix.sed x.go", []string{"edit x.go"}},
+ {"sed -ie 's/a/b/' x.go", []string{"edit x.go"}}, // GNU: the suffix is "e"
+ {"sed -E -i 's/a/b/' x.go", []string{"edit x.go"}},
+ {"sed -i 's/x/y/' f", []string{"edit f"}},
+ {"sed -i '' 's/x/y/' f", []string{"edit f"}}, // BSD sed: '' is the backup suffix
+ {"sed -i '' 's/x/y/' f g", []string{"edit f", "edit g"}},
+ {"sed -i '' -e 's/a/b/' a.go", []string{"edit a.go"}},
+ {`sed -i "s/$old/$new/" f`, []string{"edit f"}}, // an unknown script is not a suffix
+ {`sed -i "s/$a/$b/" x y`, []string{"edit x", "edit y"}},
+ {"sudo -u deploy rm /srv/app/old.txt", []string{"delete /srv/app/old.txt"}},
+ {"sudo -E tee /etc/app.conf < in.txt", []string{"write /etc/app.conf"}},
+ {"sudo -i rm old.txt", nil}, // a login shell starts in the target user's home
+ {"env FOO=1 rm a.txt", []string{"delete a.txt"}},
+ {"env -C sub rm a.txt", nil},
+ {"rm a.txt # cleanup b.txt", []string{"delete a.txt"}},
+ {"rm {a,b}.txt", []string{"delete a.txt", "delete b.txt"}},
+ {"if true; then rm a.txt; fi", []string{"delete a.txt"}},
+ {"for f in *.tmp; do rm \"$f\"; done", nil},
+ {"echo $((1<<3)) > n.txt\nrm old.txt", []string{"write n.txt", "delete old.txt"}},
+ {"grep x <<< \"$v\"\nrm a.txt", []string{"delete a.txt"}},
+ {"git commit -m \"$(cat <<'EOF'\nrm -rf dist\nEOF\n)\"", nil},
+ {"FOO=1 sudo rm build/out", []string{"delete build/out"}},
+ {`rm "file with spaces.txt"`, []string{"delete file with spaces.txt"}},
+ {`rm file\ two.txt`, []string{"delete file two.txt"}},
+ {"echo '> not a redirect' ; ls", nil},
+ {"rm $TARGET", nil}, // unknown value
+ {`"$TOOL" rm victim.txt`, nil},
+ {`command "$TOOL" rm victim.txt`, nil},
+ {`cp first.txt second.txt "$DEST"`, nil},
+ {`cp "$SOURCE" output.txt`, []string{"write output.txt"}},
+ {`rm known.txt "$UNKNOWN"`, []string{"delete known.txt"}},
+ {`"$TOOL" rm victim.txt > known.log`, []string{"write known.log"}},
+ {`ROOT="$DIR" cd sub && rm relative.txt`, []string{"cd delete relative.txt"}},
+ {`ROOT="$DIR" rm known.txt`, []string{"delete known.txt"}},
+ {`"ROOT=$DIR" rm victim.txt`, nil},
+ {`"$TOOL=x" rm victim.txt`, nil},
+ {`"ROOT"=$DIR rm victim.txt`, nil},
+ {`ROOT\=$DIR rm victim.txt`, nil},
+ {`ROOT=$DIR rm known.txt`, []string{"delete known.txt"}},
+ {"rm *.log", nil}, // glob
+ {"rm \"$(pwd)/x\"", nil}, // expansion inside quotes
+ {"(cd sub && rm x)", []string{"cd delete x"}}, // x is sub/x, which this parser does not follow
+ {"cd /tmp && rm /abs/path.txt && rm rel.txt", []string{"cd delete /abs/path.txt", "cd delete rel.txt"}},
+ {"rm before.txt; cd sub; rm after.txt", []string{"delete before.txt", "cd delete after.txt"}},
+ {"command cd sub && rm after.txt", []string{"cd delete after.txt"}},
+ {"ROOT=sub cd sub && touch after.txt", []string{"cd touch after.txt"}},
+ {"builtin cd sub && rm after.txt", []string{"cd delete after.txt"}},
+ {"command cd sub && rm /abs/path.txt", []string{"cd delete /abs/path.txt"}},
+ {"touch before.txt; command pushd sub; rm after.txt", []string{"touch before.txt", "cd delete after.txt"}},
+ {"command popd; rm after.txt", []string{"cd delete after.txt"}},
+ {"ls -la; go test ./...", nil},
+ {"tee -a log.txt < output.txt", []string{"append log.txt"}},
+ {"tee log.txt <output.txt", []string{"write log.txt"}},
+ {"tee copy.txt <<< hello", []string{"write copy.txt"}},
+ {"echo '<' > lt.txt", []string{"write lt.txt"}},
+ {"tee '<' out.txt", []string{"write <", "write out.txt"}},
+ {"cp a.txt b.txt < /dev/null", []string{"write b.txt"}},
+ {"sudo tee /etc/app.conf > /dev/null <<'EOF'\nkey: value\nEOF", []string{"write /etc/app.conf"}},
+ {"cat <<EOF > gen.txt\nhello\nEOF", []string{"write gen.txt"}},
+ {"cat > deploy.sh <<'EOF'\nrm -rf dist\necho done > status.txt\nEOF\nchmod +x deploy.sh", []string{"write deploy.sh"}},
+ {"cat <<-END > a.txt\n\trm b.txt\n\tEND\nrm c.txt", []string{"write a.txt", "delete c.txt"}},
+ {"cat <<A <<\"B\" > out.txt\ntouch x.txt\nA\ntouch y.txt\nB\ntouch z.txt", []string{"write out.txt", "touch z.txt"}},
+ {"cat > a.md <<'EOF'\nDon't touch\nEOF\nrm b.txt", []string{"write a.md", "delete b.txt"}},
+ {"grep -c x <<< 'rm here.txt'", nil},
+ {"tee x.txt <<<\"hello\"\nrm y.txt", []string{"write x.txt", "delete y.txt"}},
+ {"mv a.txt \"$DEST\"", []string{"move_from a.txt"}},
+ {"ln -s ../real.txt link.txt", []string{"link link.txt"}},
+ {"tee --append log.txt", []string{"append log.txt"}},
+ {"go test &>> test.log", []string{"append test.log"}},
+ {"make >&build.log", []string{"write build.log"}},
+ } {
+ commands, err := Parse(tc.command)
+ if err != nil {
+ t.Errorf("Parse(%q): %v", tc.command, err)
+ continue
+ }
+ var got []string
+ for _, c := range commands {
+ for _, e := range c.FileEffects() {
+ s := string(e.Op) + " " + e.Path
+ if c.AfterCD {
+ s = "cd " + s
+ }
+ got = append(got, s)
+ }
+ }
+ if !reflect.DeepEqual(got, tc.want) {
+ t.Errorf("FileEffects(%q) = %q, want %q", tc.command, got, tc.want)
+ }
+ if all := FileEffects(commands); len(all) != len(got) {
+ t.Errorf("FileEffects(Parse(%q)) has %d effects, want %d", tc.command, len(all), len(got))
+ }
+ }
+}
+
+// A command line that ended early (a capture cut short) is an error that comes with the commands
+// of the part before the cut; any other parse error comes with none.
+func TestParseIncomplete(t *testing.T) {
+ for _, tc := range []struct {
+ command string
+ want []string // each command's words, as written
+ }{
+ {"rm a.txt 'unterminated", []string{"rm a.txt"}},
+ {"beacon share --current && cat <<EOF\nlong text", []string{"beacon share --current", "cat"}},
+ {"echo $(rm x", []string{"echo"}},
+ {"go test ./...\nbeacon share \"$(date", []string{"go test ./...", "beacon share"}},
+ {"if true; then rm a.txt", nil},
+ {`& "C:\Tools\beacon.exe" share`, nil}, // PowerShell: not bash, not a cut
+ {"if ($x) { beacon share }", nil},
+ } {
+ commands, err := Parse(tc.command)
+ if err == nil {
+ t.Errorf("Parse(%q): no error", tc.command)
+ }
+ var got []string
+ for _, c := range commands {
+ var raw []string
+ for _, w := range c.Argv {
+ raw = append(raw, w.Raw)
+ }
+ got = append(got, strings.Join(raw, " "))
+ }
+ if !reflect.DeepEqual(got, tc.want) {
+ t.Errorf("Parse(%q) = %q, want %q", tc.command, got, tc.want)
+ }
+ }
+}
+
+func TestParse(t *testing.T) {
+ commands, err := Parse("FOO=1 BAR=\"$X\" sudo -u deploy /opt/bin/beacon share 2>&1 | tee -a out.log | sh\ncd sub; echo `rm x` > {a,b}.txt")
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := []Command{
+ {
+ Env: []Assign{
+ {Name: "FOO", Value: Word{Text: "1", Raw: "1", Known: true}},
+ {Name: "BAR", Value: Word{Raw: `"$X"`}},
+ },
+ Argv: []Word{
+ {Text: "sudo", Raw: "sudo", Known: true},
+ {Text: "-u", Raw: "-u", Known: true},
+ {Text: "deploy", Raw: "deploy", Known: true},
+ {Text: "/opt/bin/beacon", Raw: "/opt/bin/beacon", Known: true},
+ {Text: "share", Raw: "share", Known: true},
+ },
+ Redirects: []Redirect{{Op: ">&", Fd: "2", Target: Word{Text: "1", Raw: "1", Known: true}}},
+ Pipeline: 1,
+ },
+ {
+ Argv: []Word{
+ {Text: "tee", Raw: "tee", Known: true},
+ {Text: "-a", Raw: "-a", Known: true},
+ {Text: "out.log", Raw: "out.log", Known: true},
+ },
+ Offset: 59,
+ Pipeline: 1,
+ Stage: 1,
+ },
+ {
+ Argv: []Word{{Text: "sh", Raw: "sh", Known: true}},
+ Offset: 76,
+ Pipeline: 1,
+ Stage: 2,
+ },
+ {
+ Argv: []Word{{Text: "cd", Raw: "cd", Known: true}, {Text: "sub", Raw: "sub", Known: true}},
+ Offset: 79,
+ Pipeline: 2,
+ },
+ {
+ Argv: []Word{{Text: "echo", Raw: "echo", Known: true}, {Raw: "`rm x`"}},
+ Redirects: []Redirect{{Op: ">", Target: Word{Raw: "{a,b}.txt", Text: "{a,b}.txt", Known: true}}},
+ Offset: 87,
+ Pipeline: 3,
+ AfterCD: true,
+ },
+ {
+ Argv: []Word{{Text: "rm", Raw: "rm", Known: true}, {Text: "x", Raw: "x", Known: true}},
+ Offset: 93,
+ Pipeline: 4,
+ Nested: true,
+ AfterCD: true,
+ },
+ }
+ if !reflect.DeepEqual(commands, want) {
+ t.Fatalf("Parse:\n got %+v\nwant %+v", commands, want)
+ }
+}
+
+func TestWords(t *testing.T) {
+ commands, err := Parse(`rm {a,b}.txt ~/x 'q t' "d $v" C:\Tools\x.exe file\ two *.log`)
+ if err != nil {
+ t.Fatal(err)
+ }
+ want := []Word{
+ {Text: "rm", Raw: "rm", Known: true},
+ {Text: "a.txt", Raw: "{a,b}.txt", Known: true},
+ {Text: "b.txt", Raw: "{a,b}.txt", Known: true},
+ {Raw: "~/x"},
+ {Text: "q t", Raw: "'q t'", Known: true},
+ {Raw: `"d $v"`},
+ {Text: "C:Toolsx.exe", Raw: `C:\Tools\x.exe`, Known: true},
+ {Text: "file two", Raw: `file\ two`, Known: true},
+ {Raw: "*.log"},
+ }
+ if len(commands) != 1 || !reflect.DeepEqual(commands[0].Argv, want) {
+ t.Fatalf("Argv = %+v, want %+v", commands, want)
+ }
+}
+
+func TestUnwrapped(t *testing.T) {
+ for _, tc := range []struct {
+ command string
+ name string
+ ok bool
+ is []string
+ }{
+ {"beacon share --current", "beacon", true, []string{"beacon", "share"}},
+ {"/opt/beacon/bin/beacon share", "beacon", true, []string{"beacon", "share"}},
+ {"sudo -u deploy env -u HOME FOO=1 nohup time exec command git mv a b", "git", true, []string{"git", "mv"}},
+ {"git -C sub mv a b", "git", true, []string{"git"}},
+ {"sudo -i rm x", "", false, nil},
+ {"sudo --login rm x", "", false, nil},
+ {"env -C sub rm x", "", false, nil},
+ {"env --split-string='rm x'", "", false, nil},
+ {`"$TOOL" share`, "", true, nil},
+ {"sudo", "", true, nil},
+ {"FOO=1", "", true, nil},
+ } {
+ commands, err := Parse(tc.command)
+ if err != nil || len(commands) != 1 {
+ t.Fatalf("Parse(%q) = %+v, %v", tc.command, commands, err)
+ }
+ c := commands[0]
+ if _, ok := c.Unwrapped(); ok != tc.ok {
+ t.Errorf("Unwrapped(%q) ok = %v, want %v", tc.command, ok, tc.ok)
+ }
+ if got := c.Name(); got != tc.name {
+ t.Errorf("Name(%q) = %q, want %q", tc.command, got, tc.name)
+ }
+ if tc.is != nil && !c.Is(tc.is[0], tc.is[1:]...) {
+ t.Errorf("Is(%q, %q) = false", tc.command, tc.is)
+ }
+ if c.Is("git", "rm") || c.Is("beacon", "shares") {
+ t.Errorf("%q: Is matched a command it is not", tc.command)
+ }
+ }
+}