-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathcli.rs
More file actions
49 lines (39 loc) · 1.48 KB
/
cli.rs
File metadata and controls
49 lines (39 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
use std::{env::args_os, ffi::OsStr, path::PathBuf, pin::Pin};
use fspy::AccessMode;
use tokio::{
fs::File,
io::{AsyncWrite, stdout},
};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
let mut args = args_os();
let _ = args.next();
assert_eq!(args.next().as_deref(), Some(OsStr::new("-o")));
let out_path = args.next().unwrap();
let program = PathBuf::from(args.next().unwrap());
let mut command = fspy::Command::new(program);
command.envs(std::env::vars_os()).args(args);
let child = command.spawn().await?;
let termination = child.wait_handle.await?;
let mut path_count = 0usize;
let out_file: Pin<Box<dyn AsyncWrite>> =
if out_path == "-" { Box::pin(stdout()) } else { Box::pin(File::create(out_path).await?) };
let mut csv_writer = csv_async::AsyncWriter::from_writer(out_file);
for acc in termination.path_accesses.iter() {
path_count += 1;
csv_writer
.write_record(&[
acc.path.to_cow_os_str().to_string_lossy().as_ref().as_bytes(),
match acc.mode {
AccessMode::Read => b"read".as_slice(),
AccessMode::ReadWrite => b"readwrite",
AccessMode::Write => b"write",
AccessMode::ReadDir => b"readdir",
},
])
.await?;
}
csv_writer.flush().await?;
eprintln!("\nfspy: {path_count} paths accessed. status: {}", termination.status);
Ok(())
}