use std::collections::HashSet;
use std::sync::{Mutex, OnceLock};
pub fn quote(value: &str) -> String {
if value.is_empty() {
return "''".to_string();
}
if value.starts_with('\'') && value.ends_with('\'') && value.len() >= 2 {
let inner = &value[1..value.len() - 1];
if !inner.contains('\'') {
return value.to_string();
}
}
if value.starts_with('"') && value.ends_with('"') && value.len() > 2 {
return format!("'{}'", value);
}
let safe_pattern = regex::Regex::new(r"^[a-zA-Z0-9_\-./=,+@:]+$").unwrap();
if safe_pattern.is_match(value) {
return value.to_string();
}
format!("'{}'", value.replace('\'', "'\\''"))
}
pub fn quote_all(values: &[&str]) -> String {
values
.iter()
.map(|v| quote(v))
.collect::<Vec<_>>()
.join(" ")
}
pub fn needs_quoting(value: &str) -> bool {
if value.is_empty() {
return true;
}
let safe_pattern = regex::Regex::new(r"^[a-zA-Z0-9_\-./=,+@:]+$").unwrap();
!safe_pattern.is_match(value)
}
pub fn find_split_template_token(command: &str) -> Option<String> {
if !command.contains("{{") {
return None;
}
let chars: Vec<char> = command.chars().collect();
let n = chars.len();
let mut in_single = false;
let mut in_double = false;
let mut i = 0;
while i < n {
let c = chars[i];
if in_single {
in_single = c != '\'';
i += 1;
continue;
}
if in_double {
in_double = c != '"';
i += 1;
continue;
}
if c == '\'' {
in_single = true;
i += 1;
continue;
}
if c == '"' {
in_double = true;
i += 1;
continue;
}
if c == '{' && i + 1 < n && chars[i + 1] == '{' {
let (splits, end) = scan_template_close(&chars, i + 2);
if splits {
return Some(chars[i..=end + 1].iter().collect());
}
i = end + 1;
continue;
}
i += 1;
}
None
}
fn scan_template_close(chars: &[char], start: usize) -> (bool, usize) {
let n = chars.len();
let mut j = start;
let mut has_unquoted_space = false;
let mut in_single = false;
let mut in_double = false;
while j < n {
let c = chars[j];
if in_single {
in_single = c != '\'';
} else if in_double {
in_double = c != '"';
} else if c == '\'' {
in_single = true;
} else if c == '"' {
in_double = true;
} else if c == '}' && j + 1 < n && chars[j + 1] == '}' {
return (has_unquoted_space, j);
} else if c.is_whitespace() {
has_unquoted_space = true;
}
j += 1;
}
(false, j)
}
fn warned_template_snippets() -> &'static Mutex<HashSet<String>> {
static WARNED: OnceLock<Mutex<HashSet<String>>> = OnceLock::new();
WARNED.get_or_init(|| Mutex::new(HashSet::new()))
}
pub fn warn_on_split_template(command: &str) {
if std::env::var_os("COMMAND_STREAM_NO_TEMPLATE_WARNING").is_some() {
return;
}
let snippet = match find_split_template_token(command) {
Some(s) => s,
None => return,
};
{
let mut warned = warned_template_snippets().lock().unwrap();
if !warned.insert(snippet.clone()) {
return;
}
}
eprintln!(
"[command-stream] Warning: template token `{snippet}` contains an \
unquoted space, so the shell splits it into multiple arguments (just like \
bash would). Quote it ('{snippet}') or interpolate it as a single ${{value}} \
to pass it as one argument. See README \"Go templates & {{{{ }}}} arguments\". \
Set COMMAND_STREAM_NO_TEMPLATE_WARNING=1 to silence."
);
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_quote_empty() {
assert_eq!(quote(""), "''");
}
#[test]
fn test_quote_safe_chars() {
assert_eq!(quote("hello"), "hello");
assert_eq!(quote("/path/to/file"), "/path/to/file");
assert_eq!(quote("file.txt"), "file.txt");
assert_eq!(quote("key=value"), "key=value");
assert_eq!(quote("user@host"), "user@host");
}
#[test]
fn test_quote_special_chars() {
assert_eq!(quote("hello world"), "'hello world'");
assert_eq!(quote("it's"), "'it'\\''s'");
assert_eq!(quote("$var"), "'$var'");
assert_eq!(quote("test*"), "'test*'");
}
#[test]
fn test_quote_already_quoted() {
assert_eq!(quote("'already quoted'"), "'already quoted'");
assert_eq!(quote("\"double quoted\""), "'\"double quoted\"'");
}
#[test]
fn test_quote_all() {
let args = vec!["echo", "hello world", "test"];
assert_eq!(quote_all(&args), "echo 'hello world' test");
}
#[test]
fn test_needs_quoting() {
assert!(!needs_quoting("hello"));
assert!(!needs_quoting("/path/to/file"));
assert!(needs_quoting("hello world"));
assert!(needs_quoting("$PATH"));
assert!(needs_quoting(""));
assert!(needs_quoting("test*"));
}
#[test]
fn test_quote_with_newlines() {
assert_eq!(quote("line1\nline2"), "'line1\nline2'");
}
#[test]
fn test_quote_with_tabs() {
assert_eq!(quote("col1\tcol2"), "'col1\tcol2'");
}
#[test]
fn test_find_split_template_unquoted_with_space() {
assert_eq!(
find_split_template_token("docker inspect --format {{json .Config.Env}}"),
Some("{{json .Config.Env}}".to_string())
);
}
#[test]
fn test_find_split_template_space_free() {
assert_eq!(
find_split_template_token("docker inspect --format {{.Id}}"),
None
);
}
#[test]
fn test_find_split_template_single_quoted() {
assert_eq!(
find_split_template_token("docker inspect --format '{{json .Config.Env}}'"),
None
);
}
#[test]
fn test_find_split_template_double_quoted() {
assert_eq!(
find_split_template_token("docker inspect --format \"{{json .Config.Env}}\""),
None
);
}
#[test]
fn test_find_split_template_none_without_braces() {
assert_eq!(find_split_template_token("echo hello world"), None);
}
}