shell.ts

Shell quoting for recipes that compose command text.

A recipe splices its values into a command a shell reads: a script's check and run, or a script it writes to a file. Each value goes in as one single-quoted word, inside which a shell reads every byte literally except a single quote — so a value can't change what the command means, whatever it holds ($, backticks, ", ;, spaces, newlines).

A value may be a reference (env_ref / zap.env). Its marker holds no single quote, so it lands inside the quotes, and zap refuses to resolve a reference whose value holds one when the command runs. A literal single quote in the text is escaped ('\''), which is what it means inside single quotes.

A pure function, like the recipes that import it: it only assembles a string.

view source

Declarations
#

2 declarations

single_quote
#

shell.ts view source

(value: string): string import {single_quote} from '@fuzdev/zap/shell.js';

value as one single-quoted shell word, each ' in it written '\''.

The twin of zap's own quoting for the values it composes into commands, so a recipe's command quotes the way zap's do.

value

the text, literal or holding references

type string

returns

string

the quoted word

sudo_as_user
#

shell.ts view source

(recipe: string, user: string): string import {sudo_as_user} from '@fuzdev/zap/shell.js';

The sudo -u <user> prefix of a command a recipe runs as user, refusing a user that wouldn't name the account the recipe means.

sudo -u reads a user starting with # as a uid (#0 is root), and an empty one as nothing at all. A reference (env_ref / zap.env) is refused too: its value isn't known until the command runs, so a recipe can't check it, and a value of #0 would run the command as root.

recipe

the recipe's name, for the error

type string

user

the account's name

type string

returns

string

sudo -u and the quoted name, then a space

throws

  • Error - if `user` is empty, starts with `#`, or holds a reference

Imported by
#