use crate::config::error::ConfigError;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScriptCommand {
pub name: String,
pub path: PathBuf,
pub desc: Option<String>,
pub service: Option<String>,
pub tty: bool,
pub env: BTreeMap<String, String>,
pub needs: Vec<String>,
pub forward_args: bool,
pub cwd: Option<String>,
pub optional: bool,
pub interactive: bool,
}
impl ScriptCommand {
pub fn from_path(path: &Path) -> Result<Self, ConfigError> {
let name = path
.file_stem()
.and_then(|s| s.to_str())
.ok_or_else(|| {
ConfigError::Invalid(format!(
"script path {} has no usable file stem",
path.display()
))
})?
.to_string();
let content = std::fs::read_to_string(path).map_err(|source| ConfigError::Io {
path: path.to_path_buf(),
source,
})?;
let mut cmd = Self {
name,
path: path.to_path_buf(),
desc: None,
service: None,
tty: false,
env: BTreeMap::new(),
needs: Vec::new(),
forward_args: false,
cwd: None,
optional: false,
interactive: false,
};
parse_frontmatter(&content, &mut cmd, path)?;
Ok(cmd)
}
}
fn parse_frontmatter(
content: &str,
cmd: &mut ScriptCommand,
path: &Path,
) -> Result<(), ConfigError> {
let mut lines = content.lines();
if let Some(first) = lines.clone().next()
&& first.starts_with("#!")
{
lines.next();
}
for line in lines {
let trimmed = line.trim_end();
let Some(rest) = trimmed.strip_prefix("# @") else {
break;
};
let Some((raw_key, raw_value)) = rest.split_once(':') else {
break;
};
let key = raw_key.trim().to_ascii_lowercase().replace('-', "_");
let value = raw_value.trim();
apply_frontmatter_kv(&key, value, cmd, path)?;
}
Ok(())
}
fn apply_frontmatter_kv(
key: &str,
value: &str,
cmd: &mut ScriptCommand,
path: &Path,
) -> Result<(), ConfigError> {
match key {
"desc" | "description" => cmd.desc = Some(value.to_string()),
"in" | "service" => cmd.service = Some(value.to_string()),
"tty" => cmd.tty = parse_bool(value, key, path)?,
"forward_args" => cmd.forward_args = parse_bool(value, key, path)?,
"cwd" => cmd.cwd = Some(value.to_string()),
"optional" => cmd.optional = parse_bool(value, key, path)?,
"interactive" => cmd.interactive = parse_bool(value, key, path)?,
"needs" => {
cmd.needs.extend(
value
.split(',')
.map(str::trim)
.filter(|s| !s.is_empty())
.map(str::to_string),
);
}
"env" => {
let (k, v) = value.split_once('=').ok_or_else(|| {
ConfigError::Invalid(format!(
"{} frontmatter `env` line must be KEY=VALUE, got `{}`",
path.display(),
value
))
})?;
cmd.env.insert(k.trim().into(), v.trim().into());
}
other => {
return Err(ConfigError::Invalid(format!(
"{} unknown frontmatter key `@{}`",
path.display(),
other
)));
}
}
Ok(())
}
fn parse_bool(value: &str, key: &str, path: &Path) -> Result<bool, ConfigError> {
match value.to_ascii_lowercase().as_str() {
"true" | "yes" | "1" | "on" => Ok(true),
"false" | "no" | "0" | "off" => Ok(false),
other => Err(ConfigError::Invalid(format!(
"{} frontmatter `{}` must be a boolean, got `{}`",
path.display(),
key,
other
))),
}
}
#[cfg(test)]
mod tests {
use super::*;
use pretty_assertions::assert_eq;
use tempfile::TempDir;
fn write_script(dir: &TempDir, name: &str, body: &str) -> PathBuf {
let p = dir.path().join(name);
std::fs::write(&p, body).unwrap();
p
}
#[test]
fn parses_full_frontmatter() {
let dir = TempDir::new().unwrap();
let p = write_script(
&dir,
"migrate.sh",
"#!/usr/bin/env bash\n\
# @desc: Run database migrations\n\
# @in: app\n\
# @needs: up, build\n\
# @env: APP_ENV=production\n\
# @env: KEY=val\n\
# @forward-args: true\n\
# @tty: yes\n\
# @cwd: subdir\n\
# @optional: yes\n\
# @interactive: false\n\
php artisan migrate \"$@\"\n",
);
let cmd = ScriptCommand::from_path(&p).unwrap();
assert_eq!(cmd.name, "migrate");
assert_eq!(cmd.desc.as_deref(), Some("Run database migrations"));
assert_eq!(cmd.service.as_deref(), Some("app"));
assert_eq!(cmd.needs, vec!["up", "build"]);
assert_eq!(
cmd.env.get("APP_ENV").map(String::as_str),
Some("production")
);
assert_eq!(cmd.env.get("KEY").map(String::as_str), Some("val"));
assert!(cmd.forward_args);
assert!(cmd.tty);
assert_eq!(cmd.cwd.as_deref(), Some("subdir"));
assert!(cmd.optional);
assert!(!cmd.interactive);
}
#[test]
fn no_extension_works() {
let dir = TempDir::new().unwrap();
let p = write_script(&dir, "seed", "#!/bin/sh\necho hi\n");
let cmd = ScriptCommand::from_path(&p).unwrap();
assert_eq!(cmd.name, "seed");
assert!(cmd.desc.is_none());
}
#[test]
fn frontmatter_stops_at_first_non_directive() {
let dir = TempDir::new().unwrap();
let p = write_script(
&dir,
"x.sh",
"#!/bin/bash\n# @desc: A\nset -e\n# @desc: Should be ignored\n",
);
let cmd = ScriptCommand::from_path(&p).unwrap();
assert_eq!(cmd.desc.as_deref(), Some("A"));
}
#[test]
fn unknown_key_is_an_error() {
let dir = TempDir::new().unwrap();
let p = write_script(&dir, "x.sh", "# @foo: bar\n");
let err = ScriptCommand::from_path(&p).unwrap_err();
match err {
ConfigError::Invalid(msg) => assert!(msg.contains("foo")),
other => panic!("unexpected: {other:?}"),
}
}
#[test]
fn malformed_env_is_an_error() {
let dir = TempDir::new().unwrap();
let p = write_script(&dir, "x.sh", "# @env: nope\n");
let err = ScriptCommand::from_path(&p).unwrap_err();
assert!(matches!(err, ConfigError::Invalid(_)));
}
#[test]
fn invalid_bool_is_an_error() {
let dir = TempDir::new().unwrap();
let p = write_script(&dir, "x.sh", "# @tty: maybe\n");
let err = ScriptCommand::from_path(&p).unwrap_err();
assert!(matches!(err, ConfigError::Invalid(_)));
}
#[test]
fn no_shebang_is_fine() {
let dir = TempDir::new().unwrap();
let p = write_script(&dir, "raw", "# @desc: noshebang\necho hi\n");
let cmd = ScriptCommand::from_path(&p).unwrap();
assert_eq!(cmd.desc.as_deref(), Some("noshebang"));
}
}