pub mod accounts;
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use crate::failure::{Doing, Failure};
use crate::rig::Answer;
pub struct Scripts(tempfile::TempDir);
impl Scripts {
pub fn of(files: &[(&str, &str)]) -> Self {
let dir = tempfile::tempdir().expect("a scratch directory");
for (name, body) in files {
let file = dir.path().join(name);
if let Some(parent) = file.parent() {
fs::create_dir_all(parent).expect(name);
}
fs::write(file, body).expect(name);
}
Self(dir)
}
pub fn dir(&self) -> &Path {
self.0.path()
}
pub fn at(&self, name: &str) -> PathBuf {
self.dir().join(name)
}
}
pub fn bash(script: PathBuf) -> Vec<OsString> {
vec!["bash".into(), script.into()]
}
pub fn sourcing(path: &Path, body: &str) -> Result<Answer, Failure> {
fs::write(path, body).doing(|| format!("writing {}", path.display()))?;
Ok(Answer::of(
"source",
[path.to_string_lossy()],
))
}