use crate::config::gate_args::GateArgs;
use std::fs::read_to_string;
pub struct JobSource;
impl JobSource {
pub fn apply(args: GateArgs) -> Result<GateArgs, String> {
let jobs = Self::resolve(&args)?;
Ok(GateArgs { jobs, ..args })
}
pub fn resolve(args: &GateArgs) -> Result<Vec<String>, String> {
match (&args.jobs_file, args.jobs.is_empty()) {
(Some(_), false) => {
Err("both --jobs and --jobs-file were given; state the job list once".to_string())
}
(Some(path), true) => Self::read(&path.to_string_lossy()),
(None, false) => Ok(args.jobs.clone()),
(None, true) => Err("no jobs to run; pass --jobs or --jobs-file".to_string()),
}
}
fn read(path: &str) -> Result<Vec<String>, String> {
let contents = read_to_string(path)
.map_err(|error| format!("--jobs-file {path} could not be read: {error}"))?;
let names: Vec<String> = contents
.strip_prefix('\u{feff}')
.unwrap_or(&contents)
.lines()
.map(str::trim)
.filter(|line| !line.is_empty())
.map(str::to_string)
.collect();
if names.is_empty() {
return Err(format!("--jobs-file {path} names no jobs"));
}
Ok(names)
}
}