flodl_cli/util/shell.rs
1//! Shell-quoting helpers shared by every place fdl composes an `sh -c` /
2//! `bash -c` command line (docker dispatch, cluster SSH fan-out, schema
3//! probes, prebuild).
4
5/// POSIX-quote a single token so it round-trips through `sh -c` / `bash
6/// -c` as one argument. Empty strings become `''`; tokens containing
7/// only safe characters pass through unchanged; everything else is
8/// wrapped in single quotes with embedded `'` escaped as `'\''`.
9///
10/// This is THE quoting implementation — an earlier era had three local
11/// copies whose safe-character sets silently diverged. Local copies
12/// dodge review; don't reintroduce one.
13pub(crate) fn posix_quote(s: &str) -> String {
14 if s.is_empty() {
15 return "''".to_string();
16 }
17 let safe = s.chars().all(|c| {
18 c.is_ascii_alphanumeric()
19 || matches!(c, '_' | '-' | '.' | '/' | ':' | '=' | '+' | '@' | ',')
20 });
21 if safe {
22 return s.to_string();
23 }
24 let mut out = String::with_capacity(s.len() + 2);
25 out.push('\'');
26 for c in s.chars() {
27 if c == '\'' {
28 out.push_str("'\\''");
29 } else {
30 out.push(c);
31 }
32 }
33 out.push('\'');
34 out
35}