HQL 0.8.0: child processes, scripts, HyperMarkDown 0.1.0¶
TL;DR
- Child processes.
command("git", ["log"])names a program.sourcereads what it prints as values,sinkwrites to it, andrungives its outcome as aResult. No shell reads the command line, and a non-zero exit status is a failure. - Scripts. A file that starts with
#!/usr/bin/env hqlruns as a command. It reads its arguments withio.env.argv(), and its result sets the exit status the shell reads. - HyperMarkDown 0.1.0. A vault's config reads
location_path. In a document, onlyhql#evalandhql#queryfences run; a plainhqlfence is a code listing. - Five changes break existing programs or projects. Each has a one-line fix, listed under "What breaks".
HQL 0.8.0 is out. This post shows running programs for each of its three features. Every program and every output below was run with this release. Install has the binaries.
A child process in a pipeline¶
A child process is another program that an HQL program starts. HQL
0.8.0 starts one with command from the module io.process, and reads
and writes it with the operations a file already has.
Reading: source¶
This script counts the commits of each author in the repository it is run in:
#!/usr/bin/env hql
from io.process import *
source(command("git", ["log", "--format=%an"]), Lines)
| group(by = name => name)
| sort(by = g => g.key)
| map(g => {author: g.key, commits: g.items | size})
Run in a repository with three commits, two by Ada and one by Grace, it prints:
$ ./authors.hql
[{author: "Ada", commits: 2}, {author: "Grace", commits: 1}]
command(…) is a value and starts nothing. It is an endpoint, like a
file: source starts git, reads its standard output to the end, waits
for it to exit, and gives each line as a String. The rest is an ordinary
pipeline. tap reads the same output as a stream, one line at a time
while the child is still running.
Writing: sink¶
sink writes one line to the child's standard input for each value, and
closes it when the values end. What the child prints goes to the program's
standard output:
#!/usr/bin/env hql
from io.process import *
["The Hobbit", "Dune", "Frankenstein"] | sink(command("sort"), Lines)
$ ./sorted.hql
Dune
Frankenstein
The Hobbit
The three lines were printed by sort. target does the same with its
whole input as one document.
Handling a failure: run¶
run starts the child, waits for it, and gives the outcome as a Result:
ok(Output) when the child exits 0, and err(CommandFailure) with a
reason otherwise. git describe exits 128 in a repository that has no
tag, and this script handles that case with a match:
#!/usr/bin/env hql
from io.process import *
match command("git", ["describe", "--tags"]).run() {
ok(out) => out.stdout | trim,
err(e) given e.status == some(128) => "no tag yet",
err(e) => fmt("git failed: {}", e.reason),
}
$ ./tag.hql
"no tag yet"
$ git tag v1
$ ./tag.hql
"v1"
Three rules¶
Three rules differ from what a shell script or a subprocess call does:
- No shell reads the command line.
gitreceives its arguments exactly as written. No word is split and no*is expanded, so text from outside the program is always one argument and never a command. -
A non-zero exit status is a failure. Under
runit is anerr. Undersource,tap,sinkandtargetit stops the program. Run outside a repository,authors.hqlstops, and the diagnostic gives the child's own error as the cause:$ ./authors.hql fatal: not a git repository (or any of the parent directories): .git execute failure[io-failure]: command `git`: failed: exited, status 128 --> ./authors.hql:4:1 | 4 | source(command("git", ["log", "--format=%an"]), Lines) | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ = caused by: its standard error ended with: fatal: not a git repository (or any of the parent directories): .git -
A child does not outlive the program. When a read stops early, a
timeout_mspasses orhqlis interrupted,hqlterminates the child and every process the child started.
A project lists the programs its scripts may start with
commands = ["git"] in .hql/config.toml.
Child processes run on Linux, macOS and Windows. The book's page is Child processes.
A script¶
A program in a file now runs as a command. The first line names hql as
the interpreter, and the words after the path are the program's arguments:
#!/usr/bin/env hql
import io.env
type Book { title: String, status: String }
shelf = [
Book(title = "Dune", status = "reading"),
Book(title = "The Hobbit", status = "todo"),
Book(title = "Leaves of Grass", status = "todo"),
]
wanted = env.argv()[0] | unwrap_or("todo")
shelf | filter(b => b.status == wanted) | map(b => b.title) | sink(stdout, Text)
$ chmod +x todo.hql
$ ./todo.hql
The Hobbit
Leaves of Grass
$ ./todo.hql reading
Dune
hql todo.hql reading runs the same file, and hql -c '<program>' runs
program text. The exit status follows from the result: a failure, or a
result that is err(e), exits 1, and any other result exits 0. The
book has the details: Scripts.
A document vault under HyperMarkDown 0.1.0¶
HQL reads a folder of HyperMarkDown cards as a vault, and hql render
runs the queries a document holds. 0.8.0 follows
HyperMarkDown 0.1.0 in two places.
A vault's .hmd/config.toml states where its cards start with
location_path:
location_path = "."
In a document, only a fence marked hql#eval or hql#query is a cell. A
plain hql fence is a code listing. This is reading-list.hmd, in a vault
with two book cards:
# Reading list
```hql#query id=books
cards | filter(c => c.metadata.category == "book")
```
## Still to read
```hql#eval
books | filter(c => c.metadata.status == "todo") | map(c => c.title)
```
A listing is shown and not run:
```hql
books | size
```
$ hql render reading-list.hmd
# Reading list
## Still to read
- The Hobbit
A listing is shown and not run:
```hql
books | size
```
The hql#query cell ran and wrote nothing. The hql#eval cell read its
value by id and wrote its output. The listing stayed as written. The book
has more: Queries inside documents.
What breaks¶
HQL is before 1.0, and 0.8.0 makes five breaking changes.
| What changed | What you see | The fix |
|---|---|---|
A vault config that sets wiki is refused |
the vault is not loaded, and the message names both wiki and location_path |
rename the key to location_path in .hmd/config.toml |
A plain hql fence is a code listing |
hql render no longer runs the fence or writes its output |
mark each cell hql#eval, or hql#query for a cell that writes nothing |
| Every word after the program is the program's | hql '<program>' --to json passes --to json to the program |
write options first: hql --to json '<program>' |
A result that is err(e) exits 1 |
a script whose last value is an err now fails in && chains and CI |
handle the err, with match or unwrap_or, where it is not a failure |
Path.join keeps an absolute segment |
Path("/home/ada").join("/tmp") is /tmp, not /home/ada//tmp |
pass a relative segment to stay under the base |
Two smaller changes are in the same release. An hql#query cell writes no
output in any --emit mode. --to text writes a collection of strings as
one string per line. The
changelog lists
every change.
Next: 0.9.0¶
0.9.0 continues with processes. These are planned and not in 0.8.0:
- Signals. The program's own process id, the signals it receives, and sending a signal to a process.
- A child's pipes wired to channels. Reading a child's standard error as values, and feeding its standard input from a channel.
parallel. Branches that run at the same time, for a conversation with a child: one branch writes questions while another reads answers.- Watchdogs. A check that runs beside a child and terminates it when it stops being healthy.
- Named pipes.
fifo(name)as an endpoint, on Unix and Windows.
Questions and bug reports are welcome on r/hqlang.