diff --git a/README.md b/README.md index e4990aa..051855f 100644 --- a/README.md +++ b/README.md @@ -2,8 +2,8 @@ Yet another service manager. -Services are BASH scripts that get *sourced* by `ssm`. Same pitfalls apply — -keep complex logic out of them. +Services are BASH scripts with a tiny DSL that get *sourced* by `ssm`. Same +pitfalls apply — keep complex logic out of them. ## Install @@ -59,7 +59,7 @@ Everything after `--` is treated as a literal service argument. ## Service files -Each service is a plain BASH script: +Each service is a BASH script written in a tiny DSL (see `var()` below), pure BASH is supported: ```bash #!/usr/bin/env ssm @@ -136,10 +136,51 @@ Optional (defaults shown): is equivalent to `service_oneshot = yes` and `service_respawn = 1` is equivalent to `always`. +## `var()` + +Everything ssm sources — the global config, service files and `functions/` +snippets — runs inside ssm's shell, where the built-in `var()` helper is +available. `var` turns an array-valued bash variable into a small ad-hoc +interface: `var ` creates a `` accessor function (and +`++` / `--`) that acts on an array variable of the same name. +Declared functions are read-only. Using the accessor bare tests truthiness; +other operators are evaluated in place and leave their result in `$?`, e.g. +under `if`/`&&`/`||`. + +```bash +var timeout = 5 # set (values become an array) +var service_args = -a --long-arg "parameter" + +service_args u --long-arg # membership in the array +timeout == 5 # compare first element against a value +timeout =~ '^[0-9]+$' # regex match +``` + +Operators: + +* `var name = value [value...]` — set the variable (it becomes a bash array). +* `var name += value [value...]` — append values. +* `var name _= value [value...]` — prepend values. +* `var name := value [value...]` — set only if the variable is unset or empty. +* `var name == value` — exit 0 if the variable's first element equals `value`. +* `var name =~ regex` — exit 0 if the variable's first element matches `regex`. +* `var name is file` / `is dir` / `is directory` / `is fs_object` / `is empty` — + filesystem-style tests on the value. +* `var name u value` — exit 0 if `value` is one of the array's elements. +* `var name` — no operator: exit 0 unless the variable is unset, empty, or + `false`/`0` (compared case-insensitively). +* `name++` / `name--` — increment/decrement the first element. +* `var name -v other_name` — build the accessor for `other_name` instead of + `name` (so `name` acts on `other_name`). + +`var` is how ssm does this itself (e.g. `cgroups`, `service_cgroup_path is dir`, +`service_cgroup_procs is empty`) — feel free to use it in service files and the +global config. + ## Global config `/etc/ssm/ssm.conf` (system) and `$XDG_CONFIG_HOME/ssm/ssm.conf` (user). -Since it is sourced, it is plain BASH: +Since it is sourced, it is a BASH script with a DSL (see `var()` above): ```bash # Enable cgroup-related features (experimental, cgroups v2)