Archives and network

Extract an archive

Extract a compressed tar archive into a directory.

archive.extract "/home/user/dl/data.tar.gz" into: "/home/user/data"
/home/user/data
tar -xzf /home/user/dl/data.tar.gz -C /home/user/data

List the content of an archive

List the entries of an archive without extracting it.

archive.list "/home/user/dl/data.zip"
data

Extract only some entries

Extract the entries that match a pattern.

archive.extract "/home/user/dl/data.zip" into: "/home/user/data" only: "**/*.csv"
/home/user/data

Create an archive

Create a ZIP archive of a directory.

find "/home/user/app/dist" |> archive.create "/home/user/out/dist.zip" base: "/home/user/app"
/home/user/out/dist.zip
cd /home/user/app && zip -r /home/user/out/dist.zip dist

Compress a file

Compress a file with gzip and keep the original.

compress "/home/user/app/app.log"
app.log

Decompress a file

Decompress a gzip file.

decompress "/home/user/app/app.log.gz"
app.log

Send an HTTP request

Send a GET request and require one response.

http.request "https://example.com/health" |> assert count: 1
200
curl -fsS https://example.com/health

Post JSON

Send a JSON body with a POST request.

http.request "https://example.com/api/items" method: post json: {"name": "item"}
200

Send a request with a header

Send a request with an authorization header.

http.request "https://example.com/api/me" headers: {"Accept": "application/json"}
200

Read a page as Markdown

Read the main text of a web page as Markdown.

http.request "https://example.com/docs" as: markdown
example

Download a file and verify its hash

Download a file and verify its SHA-256 hash.

http.download "https://example.com/a.tar.gz" "/home/user/dl/a.tar.gz" hash: "sha256:9d3c"
/home/user/dl/a.tar.gz
curl -fLo a.tar.gz https://example.com/a.tar.gz && sha256sum a.tar.gz

Check that a host answers

Send four echo requests to a host.

network.ping "example.com" count: 4
example.com

Resolve a name

Resolve the IPv6 addresses of a name.

network.resolve "example.com" type: aaaa
example.com

List the network interfaces

List the network interfaces of the host.

network.interfaces
interface

Extract an archive without its top directory

Extract a source archive and drop the first path component of every entry.

archive.extract "/home/user/dl/src.tar.gz" into: "/home/user/src" strip_components: 1
/home/user/src
tar -xzf /home/user/dl/src.tar.gz -C /home/user/src --strip-components=1

Extract without overwriting files

Extract an archive and keep the files that already exist.

archive.extract "/home/user/dl/data.zip" into: "/home/user/data" on_conflict: skip
/home/user/data

List the entries that match a pattern

List only the JSON entries of an archive.

archive.list "/home/user/dl/data.zip" glob: "**/*.json"
json
unzip -l /home/user/dl/data.zip '*.json'

Create a Zstandard tar archive

Pack a source directory into a tar archive compressed with Zstandard.

find "/home/user/app/src" |> archive.create "/home/user/out/src.tar.zst" format: tar_zst base: "/home/user/app"
/home/user/out/src.tar.zst
tar --zstd -cf /home/user/out/src.tar.zst -C /home/user/app src

Compress a file with a chosen algorithm

Compress a log with Zstandard at level 9 and remove the original.

compress "/home/user/app/app.log" algorithm: zstd level: 9 keep: false
app.log
zstd -9 --rm /home/user/app/app.log

Compress the old logs

Compress the log files that are older than seven days.

find "/home/user/logs" glob: "*.log" depth: 1 |> filter modified < ago(7d) |> compress
nothing_matched

Request only the headers

Send a HEAD request to read the headers of a resource.

http.request "https://example.com/a.tar.gz" method: head
200
curl -fsSI https://example.com/a.tar.gz

Save a response to a file

Save the body of a response to a file.

http.request "https://example.com/data.json" save: "/home/user/dl/data.json"
/home/user/dl/data.json

List the links of a page that lead to the same host.

http.request "https://example.com/docs" extract: links same_host: true
example.com

Limit the time and retry a request

Fail a request after five seconds and retry it up to three times.

http.request "https://example.com/health" timeout: 5s retries: 3
200
curl -fsS --max-time 5 --retry 3 https://example.com/health

Resolve a mail exchanger

Resolve the mail exchangers of a domain through a chosen DNS server.

network.resolve "example.com" type: mx server: "9.9.9.9"
example.com
dig @9.9.9.9 example.com MX

Ping with a short timeout

Send one echo request and wait at most one second.

network.ping "example.com" count: 1 timeout: 1s
example.com

Source file on GitHub: docs/cookbook/archives-and-network.md