use gadk_platform::go_quote_bytes;
pub fn center_string(s: &str, w: usize) -> String {
let sw = w as isize - s.len() as isize;
let lw = sw / 2;
let rw = sw - lw;
" ".repeat(lw.max(0) as usize) + s + &" ".repeat(rw.max(0) as usize)
}
pub fn validate_dockerfile_safe(s: impl AsRef<[u8]>, label: &str) -> Result<(), String> {
let s = s.as_ref();
if s.iter()
.any(|&c| c == b'"' || c == b'`' || c == b'\\' || c < 0x20)
{
return Err(format!(
"{label} {} contains a character (quote, backtick, backslash, or a control character) that is not allowed in a value embedded in generated Dockerfile content",
go_quote_bytes(s)
));
}
if std::str::from_utf8(s).is_err() {
return Err(format!(
"{label} {} is not valid UTF-8; the invalid bytes would reach the container as U+FFFD rather than as written",
go_quote_bytes(s)
));
}
Ok(())
}
pub fn validate_shell_arg_safe(s: impl AsRef<[u8]>, label: &str) -> Result<(), String> {
let s = s.as_ref();
let allowed =
|c: u8| c.is_ascii_alphanumeric() || matches!(c, b'_' | b'.' | b'/' | b'-') || c >= 0x80;
if !s.iter().all(|&c| allowed(c)) {
return Err(format!(
"{label} {} contains a character not allowed in a value embedded in generated Dockerfile content; whitespace splits COPY operands and shell metacharacters are live in a RUN line, so only letters, digits, '.', '/', '-', '_', and non-ASCII characters are permitted",
go_quote_bytes(s)
));
}
if std::str::from_utf8(s).is_err() {
return Err(format!(
"{label} {} is not valid UTF-8; a COPY operand containing an invalid byte does not match the file it names",
go_quote_bytes(s)
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use pretty_assertions::assert_eq;
use super::*;
#[test]
fn center_string_pads_like_go() {
assert_eq!(center_string("", 4), " ");
assert_eq!(center_string("ab", 5), " ab ");
assert_eq!(center_string("abc", 6), " abc ");
assert_eq!(center_string("abcd", 4), "abcd");
assert_eq!(center_string("Press Ctrl-C to stop", 80).len(), 80);
assert_eq!(
center_string("Press Ctrl-C to stop", 80),
format!("{}Press Ctrl-C to stop{}", " ".repeat(30), " ".repeat(30))
);
assert_eq!(center_string("é", 4), " é ");
assert_eq!(center_string("toolong", 3), "toolong");
}
#[test]
fn rejection_texts_are_go_verbatim() {
assert_eq!(
validate_dockerfile_safe("x\"y", "--a2a_agent_url").unwrap_err(),
r#"--a2a_agent_url "x\"y" contains a character (quote, backtick, backslash, or a control character) that is not allowed in a value embedded in generated Dockerfile content"#
);
assert_eq!(
validate_dockerfile_safe(b"http://x\xffy", "--a2a_agent_url").unwrap_err(),
r#"--a2a_agent_url "http://x\xffy" is not valid UTF-8; the invalid bytes would reach the container as U+FFFD rather than as written"#
);
assert_eq!(
validate_shell_arg_safe("main go", "label:").unwrap_err(),
r#"label: "main go" contains a character not allowed in a value embedded in generated Dockerfile content; whitespace splits COPY operands and shell metacharacters are live in a RUN line, so only letters, digits, '.', '/', '-', '_', and non-ASCII characters are permitted"#
);
assert_eq!(
validate_shell_arg_safe(b"agent\xff", "--source_dir").unwrap_err(),
r#"--source_dir "agent\xff" is not valid UTF-8; a COPY operand containing an invalid byte does not match the file it names"#
);
assert_eq!(
validate_dockerfile_safe("http://127.0.0.1:8081", "x"),
Ok(())
);
assert_eq!(validate_shell_arg_safe("agentes/café/main", "x"), Ok(()));
}
}