Pitcrew Documentation

Built-in functions and standard library reference

Built-in Functions

print

print(args ...any)

Prints values to stdout, separated by spaces, followed by a newline.

len

len(value any) int

Returns the length of a string (in Unicode codepoints) or array.

split

split(s string, sep string) [string]

Splits a string by a separator and returns an array of substrings.

join

join(arr any, sep string) string

Joins array elements into a string with the given separator.

trim

trim(s string) string

Removes leading and trailing whitespace from a string.

replace

replace(s string, old string, new string) string

Replaces all occurrences of old with new in a string.

contains

contains(haystack any, needle any) bool

Returns true if a string contains a substring, or an array contains a value.

starts_with

starts_with(s string, prefix string) bool

Returns true if the string starts with the given prefix.

ends_with

ends_with(s string, suffix string) bool

Returns true if the string ends with the given suffix.

to_int

to_int(s string) string! int

Parses a string as an integer. Returns an error if the string is not a valid number.

map

map(arr [T], block) [U]

Transforms each element of an array using a block, returning a new array.

resolve

resolve(promise T?) T

Waits for an async promise to complete and returns its value.

assert

assert(condition bool, msg: string?) string! void

Asserts that a condition is true. Raises an error with the given message on failure.

assert_eq

assert_eq(a any, b any, msg: string?) string! void

Asserts that two values are equal. Raises an error with the given message on failure.

pwd

pwd() string

Returns the current working directory.

aws

Basic AWS CLI wrappers for common operations. Requires the aws CLI to be installed and configured.

whoami

whoami() string! string

Returns the current AWS caller identity.

s3_list

s3_list() string! string

Lists S3 buckets.

s3_copy

s3_copy(src string, dst string) string!

Copies a file to/from S3.

s3_sync

s3_sync(src string, dst string) string!

Syncs a local directory to/from S3.

ssm_get

ssm_get(name string) string! string

Gets an SSM parameter value.

ssm_set

ssm_set(name string, value string) string!

Sets an SSM parameter value.

ec2_list

ec2_list() string! string

Lists EC2 instances.

region

region() string! string

Gets the current region.

azure

Basic Azure CLI wrappers for common operations. Requires the az CLI to be installed and configured.

account

account() string! string

Returns the current account info.

subscription

subscription() string! string

Returns the current subscription ID.

resource_groups

resource_groups() string! string

Lists resource groups.

storage_accounts

storage_accounts() string! string

Lists storage accounts.

vms

vms() string! string

Lists VMs.

blob_upload

blob_upload(account string, container string, file string) string!

Uploads a blob to storage.

blob_download

blob_download(account string, container string, name string, dest string) string!

Downloads a blob from storage.

base64

encode

encode(data [byte]) string

decode

decode(s string) string! [byte]

base64.test

test_encode_decode_roundtrip

test_encode_decode_roundtrip() string!

test_encode_known_value

test_encode_known_value() string!

test_decode_known_value

test_decode_known_value() string!

test_encode_empty

test_encode_empty() string!

test_decode_empty

test_decode_empty() string!

test_decode_invalid

test_decode_invalid() string!

crypto

random_bytes

random_bytes(n int) string! [byte]

uuidv4

uuidv4() string! string

aes_keygen

aes_keygen() string! [byte]

aes_encrypt

aes_encrypt(plaintext [byte], key [byte]) string! [byte]

aes_decrypt

aes_decrypt(ciphertext [byte], key [byte]) string! [byte]

rsa_keygen

rsa_keygen() string! RsaKeyPair

rsa_encrypt

rsa_encrypt(plaintext [byte], public_key [byte]) string! [byte]

rsa_decrypt

rsa_decrypt(ciphertext [byte], private_key [byte]) string! [byte]

crypto.test

test_random_bytes_length

test_random_bytes_length() string!

test_random_bytes_different

test_random_bytes_different() string!

test_uuidv4_format

test_uuidv4_format() string!

test_uuidv4_unique

test_uuidv4_unique() string!

test_aes_roundtrip

test_aes_roundtrip() string!

test_aes_different_ciphertexts

test_aes_different_ciphertexts() string!

test_rsa_roundtrip

test_rsa_roundtrip() string!

env

Environment variable management.

get

get(name string) string?

Returns the value of an environment variable, or nil if not set.

set

set(name string, value string) string!

Sets an environment variable.

unset

unset(name string) string!

Unsets an environment variable.

list

list() [EnvVar]

Returns all environment variables as an array of {name, value} structs.

env.test

test_set_and_get

test_set_and_get() string!

test_get_nonexistent

test_get_nonexistent() string!

test_unset

test_unset() string!

test_list

test_list() string!

fs

list

list(path string) [FileInfo]

read

read(path string) string! string

write

write(path string, content string) string!

stat

stat(path string) string! FileInfo

exists

exists(path string) bool

is_dir

is_dir(path string) bool

copy

copy(src string, dst string) string!

move

move(src string, dst string) string!

chmod

chmod(path string, mode int) string!

chown

chown(path string, uid int, gid int) string!

mkdir

mkdir(path string) string!

Creates a directory and all parent directories.

symlink

symlink(target string, link string) string!

Creates a symbolic link pointing to target.

chown_name

chown_name(path string, owner string, group string, recursive: bool?) string!

Changes ownership by user/group name. Optionally recursive.

append

append(path string, content string) string!

Appends content to a file.

copy_r

copy_r(src string, dst string) string!

Recursively copies a file or directory.

remove_r

remove_r(path string) string!

Recursively removes a file or directory.

fs.test

test_write_and_read

test_write_and_read() string!

test_stat

test_stat() string!

test_exists

test_exists() string!

test_is_dir

test_is_dir() string!

test_copy

test_copy() string!

test_move

test_move() string!

test_read_nonexistent

test_read_nonexistent() string!

gcp

Basic Google Cloud CLI wrappers for common operations. Requires the gcloud CLI to be installed and configured.

project

project() string! string

Returns the current project ID.

account

account() string! string

Returns the current authenticated account.

gcs_list

gcs_list() string! string

Lists GCS buckets.

gcs_copy

gcs_copy(src string, dst string) string!

Copies a file to/from GCS.

gcs_sync

gcs_sync(src string, dst string) string!

Syncs a local directory to/from GCS.

instances

instances() string! string

Lists compute instances.

region

region() string! string

Gets the current region.

git

Basic git CLI wrappers for common operations. Requires git to be installed. Directory handled by caller via cd blocks.

clone

clone(url string, branch: string?) string!

Clones a repository. Optionally specify a branch.

fetch

fetch() string!

Fetches from all remotes.

pull

pull() string!

Pulls the current branch.

checkout

checkout(ref string) string!

Checks out a branch, tag, or commit.

rev

rev() string!

Returns the current commit hash.

branch

branch() string!

Returns the current branch name.

hex

encode

encode(data [byte]) string

decode

decode(s string) string! [byte]

hex.test

test_encode_decode_roundtrip

test_encode_decode_roundtrip() string!

test_encode_known_value

test_encode_known_value() string!

test_decode_known_value

test_decode_known_value() string!

test_encode_empty

test_encode_empty() string!

test_decode_empty

test_decode_empty() string!

test_decode_invalid

test_decode_invalid() string!

json

JSON parsing and emitting.

parse

parse(s string) string! any

Parses a JSON string into a pitcrew value. Objects become structs, arrays become arrays, strings/numbers/bools map directly, null becomes nil.

emit

emit(val any, indent: bool?) string! string

Converts a pitcrew value to a JSON string. Supports structs, arrays, strings, numbers, bools, and nil. Set indent: true for pretty-printed output.

keys

keys(val any) string! [string]

Returns the keys of a JSON object (struct) as an array of strings.

typeof

typeof(val any) string

Returns the type of a value as a string: "string", "number", "bool", "array", "object", "null", or "unknown".

json.test

test_parse_object

test_parse_object() string!

test_parse_array

test_parse_array() string!

test_parse_nested

test_parse_nested() string!

test_parse_null

test_parse_null() string!

test_parse_bool

test_parse_bool() string!

test_emit_string

test_emit_string() string!

test_emit_number

test_emit_number() string!

test_emit_array

test_emit_array() string!

test_round_trip

test_round_trip() string!

test_keys

test_keys() string!

test_typeof

test_typeof() string!

test_emit_indent

test_emit_indent() string!

test_parse_invalid

test_parse_invalid() string!

net

HTTP requests, DNS lookups, and port checks.

get

get(url string) string! HttpResponse

Performs an HTTP GET request. Returns {status: int, body: string}.

post

post(url string, body string, content_type: string?) string! HttpResponse

Performs an HTTP POST request. Returns {status: int, body: string}.

dns_lookup

dns_lookup(host string) string! [string]

Resolves a hostname to a list of IP addresses.

port_open

port_open(host string, port int) bool

Returns true if a TCP connection can be made to host:port.

net.test

test_dns_lookup

test_dns_lookup() string!

test_port_open_closed

test_port_open_closed() string!

test_dns_lookup_bad_host

test_dns_lookup_bad_host() string!

npm

npm package management.

ls

ls() string!

Lists installed packages (depth=0).

outdated

outdated() string!

Shows packages needing updates.

version

version() string!

Returns the npm version string.

install

install(production: bool?) string!

Installs dependencies. Optionally production-only.

ci

ci(production: bool?) string!

Clean installs dependencies from lockfile. Optionally production-only.

run

run(script string) string!

Runs a named npm script.

test

test() string!

Runs npm test.

build

build() string!

Runs npm run build.

pkg

Unified package management across apt, yum/dnf, and brew. Detects the system's package manager automatically.

manager

manager() string

Returns the detected package manager: "apt", "dnf", "yum", "brew", or "unknown".

install

install(name string, version: string?) string!

Installs a package. Optionally specify a version.

remove

remove(name string) string!

Removes a package.

installed

installed(name string) string! bool

Returns true if the given package is installed.

update

update() string!

Updates the package list/index.

upgrade

upgrade(name: string?) string!

Upgrades a specific package, or all packages if no name given.

pkg.test

test_manager_returns_string

test_manager_returns_string() string!

ps

start

start(cmd string) string! int

stop

stop(pid int) string!

signal

signal(pid int, sig string) string!

status

status(pid int) string! ProcessInfo

pidfile

pidfile(path string) string! int

alive

alive(pid int) bool

ps.test

test_alive_nonexistent

test_alive_nonexistent() string!

test_status_init

test_status_init() string!

test_status_nonexistent

test_status_nonexistent() string!

test_pidfile

test_pidfile() string!

test_pidfile_notfound

test_pidfile_notfound() string!

test_start_and_stop

test_start_and_stop() string!

test_signal

test_signal() string!

regex

Regular expression matching and manipulation.

match

match(pattern regex, text string) bool

Returns true if the regex pattern matches the text.

find

find(pattern regex, text string) string?

Returns the first match of the pattern in the text, or nil if no match.

find_all

find_all(pattern regex, text string) [string]

Returns all matches of the pattern in the text as an array of strings.

replace

replace(pattern regex, text string, replacement string) string

Replaces all matches of the pattern in the text with the replacement string.

regex.test

test_match_true

test_match_true() string!

test_match_false

test_match_false() string!

test_match_digits

test_match_digits() string!

test_find_first

test_find_first() string!

test_find_no_match

test_find_no_match() string!

test_find_all

test_find_all() string!

test_find_all_no_match

test_find_all_no_match() string!

test_replace

test_replace() string!

test_replace_no_match

test_replace_no_match() string!

test_word_boundary

test_word_boundary() string!

test_regex_with_groups

test_regex_with_groups() string!

svc

Service management for systemd and launchd.

start

start(name string) string!

Starts a service.

stop

stop(name string) string!

Stops a service.

restart

restart(name string) string!

Restarts a service.

enable

enable(name string) string!

Enables a service to start on boot.

disable

disable(name string) string!

Disables a service from starting on boot.

running

running(name string) string! bool

Returns true if a service is currently running.

daemon_reload

daemon_reload() string!

Reloads the systemd daemon configuration.

manager

manager() string

Returns the detected service manager: "systemd", "launchd", or "unknown".

svc.test

test_manager_returns_string

test_manager_returns_string() string!

sync

limiter

limiter(n int) limit

time

sleep

sleep(seconds int)

Pauses execution for the given number of seconds.

tpl

Simple string templating for config file generation. Uses {{key}} placeholders replaced by values from a struct.

render

render(template string, vars any) string

Renders a template string, replacing {{key}} placeholders with values from vars. Example: render("Hello {{name}}!", {name: "world"}) => "Hello world!"

tpl.test

test_simple_render

test_simple_render() string!

test_multiple_vars

test_multiple_vars() string!

test_no_vars

test_no_vars() string!

test_repeated_placeholder

test_repeated_placeholder() string!

user

User and group management.

create

create(name string, home: string?, shell: string?) string!

Creates a new user. Optionally specify a home directory and shell.

delete

delete(name string, remove_home: bool?) string!

Deletes a user. Set remove_home to true to also delete their home directory.

exists

exists(name string) bool

Returns true if a user exists on the system.

add_to_group

add_to_group(user string, group string) string!

Adds a user to a group.

create_group

create_group(name string) string!

Creates a group.

delete_group

delete_group(name string) string!

Deletes a group.

group_exists

group_exists(name string) bool

Returns true if a group exists.