1pub mod accounts;
9
10use std::ffi::OsString;
11use std::fs;
12use std::path::{Path, PathBuf};
13
14use crate::failure::{Doing, Failure};
15use crate::rig::Answer;
16
17pub struct Scripts(tempfile::TempDir);
20
21impl Scripts {
22 pub fn of(files: &[(&str, &str)]) -> Self {
25 let dir = tempfile::tempdir().expect("a scratch directory");
26 for (name, body) in files {
27 let file = dir.path().join(name);
28 if let Some(parent) = file.parent() {
29 fs::create_dir_all(parent).expect(name);
30 }
31 fs::write(file, body).expect(name);
32 }
33 Self(dir)
34 }
35
36 pub fn dir(&self) -> &Path {
37 self.0.path()
38 }
39
40 pub fn at(&self, name: &str) -> PathBuf {
42 self.dir().join(name)
43 }
44}
45
46pub fn bash(script: PathBuf) -> Vec<OsString> {
49 vec!["bash".into(), script.into()]
50}
51
52pub fn sourcing(path: &Path, body: &str) -> Result<Answer, Failure> {
54 fs::write(path, body).doing(|| format!("writing {}", path.display()))?;
55
56 Ok(Answer::of(
57 "source",
58 [path.to_string_lossy()],
59 ))
60}