Build a pipeline
This script prints the two addresses that appear most often in an access log, by running cut | sort | uniq -c | sort -rn | head the way a shell's | does. Save it as top.ant and run it with ant run top.ant access.log:
if Os.args().isEmpty() {
eprintln("usage: top <log>")
Os.exit(2)
}
let top = Cmd.pipe([
Cmd.new("cut", ["-d", " ", "-f", "1", Os.args()[0]]),
Cmd.new("sort"),
Cmd.new("uniq", ["-c"]),
Cmd.new("sort", ["-rn"]),
Cmd.new("head", ["-n", "2"]),
])
eprintln("running ${top.string()}")
let out = try Cmd.read(top)
for line in out.lines() {
let fields = line.fields()
println("${fields[1]}: ${fields[0]} requests")
}10.0.0.2: 3 requests
10.0.0.1: 2 requestsBuild each stage with Cmd.new: the program, then its arguments. No shell runs the stages, so give each argument as it is, without quotes.
Join the stages with Cmd.pipe. Each stage's output is the next one's input. The stages run at once.
Log the pipeline with Cmd.string, which quotes it as a shell would:
cut -d ' ' -f 1 access.log | sort | uniq -c | sort -rn | head -n 2.Run it with Cmd.read, which returns what the last stage prints, without its final newline. When a stage fails,
tryends the script with the stage's error and status 1:sh$ ant run top.ant missing.log running cut -d ' ' -f 1 missing.log | sort | uniq -c | sort -rn | head -n 2 cut: exit status 1: cut: missing.log: No such file or directoryWhen two stages run the same program, the error names the stage too, counting from 1:
sort (stage 4): exit status 2: ....Split the output with String.lines and each line with String.fields, which drops the padding
uniq -cputs before the count.
Feed the pipeline from the program
Pass text as input:. The first stage reads it:
let log = "GET /\nPOST /login\nGET /about\nGET /\n"
let paths = Cmd.pipe([
Cmd.new("grep", ["^GET"]),
Cmd.new("cut", ["-d", " ", "-f", "2"]),
Cmd.new("sort", ["-u"]),
])
println(try Cmd.read(paths, input: log))/
/aboutCheck the status of each stage
The pipeline fails when any stage fails, as under bash's pipefail. grep exits with 1 when no line matches, so Cmd.read returns an Err for an empty result:
let errors = Cmd.pipe([Cmd.new("grep", ["ERROR"]), Cmd.new("sort", ["-u"])])
println(Cmd.read(errors, input: "INFO start\n"))Err{ error: Error{ message: "grep: exit status 1" } }To tell "no match" from a real failure, run the pipeline with Cmd.capture. It returns Ok whatever the status, with the pipeline's status in status and each stage's own in statuses:
func errorLines(log: string): Result[Array[string], Error] {
let errors = Cmd.pipe([Cmd.new("grep", ["ERROR"]), Cmd.new("sort", ["-u"])])
let out = try Cmd.capture(errors, input: log)
if out.statuses == [1, 0] {
return Result.ok([])
}
if out.status != 0 {
return Result.fail("${errors.string()}: exit status ${out.status}: ${out.stderr.trim()}")
}
Result.ok(out.stdout.lines())
}
println(try errorLines("INFO start\nERROR disk full\nERROR disk full\n"))
println(try errorLines("INFO start\n"))["ERROR disk full"]
[]A stage that SIGPIPE ends because a later stage stopped reading does not fail the pipeline: in yes | head -n 1, head exits after one line, the broken pipe ends yes, and the pipeline's status is 0, where bash's pipefail gives 141. The stage's own entry in statuses is still 141. On Windows, which has no SIGPIPE, every nonzero stage counts.
Show the output on the terminal
Run the pipeline with Cmd.run to give the last stage the program's standard output, and the first stage its standard input. It returns Ok of the pipeline's status, nonzero included:
let words = Cmd.pipe([Cmd.new("printf", ["pear\nfig\npear\n"]), Cmd.new("sort", ["-u"])])
let status = try Cmd.run(words)
println("status ${status}")fig
pear
status 0To read the output line by line while the pipeline runs, start it with Cmd.start; see Stream a command's output.
Build the stages from data
A pipeline is an array of Cmd values, so build it with Array.map. This one keeps the lines that contain every word. Cmd.pipe panics on an empty array, so check the words first:
func matchingAll(words: Array[string]): Cmd.Pipeline {
if words.isEmpty() {
return Cmd.pipe([Cmd.new("cat")])
}
Cmd.pipe(words.map({ Cmd.new("grep", ["-F", x]) }))
}
let notes = "red apple\ngreen apple\nred cherry\n"
println(try Cmd.read(matchingAll(["red", "apple"]), input: notes))
println(try Cmd.read(matchingAll([]), input: notes))red apple
red apple
green apple
red cherrySee also
Cmd.new · Cmd.pipe · Cmd.string · Cmd.read · Cmd.capture · Cmd.run · Cmd.start · Cmd.Pipeline · Os.args · String.lines · String.fields · Run a command · Stream a command's output · Stop or time out a process
