diff options
author | Aleksey Kladov <[email protected]> | 2020-02-14 16:05:56 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-14 16:05:56 +0000 |
commit | 269e2f22a942919d421b89287d5669b2c2607917 (patch) | |
tree | 5ae2ae26e9ab5cf604a2d8829a0aa8077fbfaf67 /xtask/src/not_bash.rs | |
parent | ce29497e4324d3e2f2c7c696a212672dbdb46884 (diff) |
More declarative fs massaging
Diffstat (limited to 'xtask/src/not_bash.rs')
-rw-r--r-- | xtask/src/not_bash.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/xtask/src/not_bash.rs b/xtask/src/not_bash.rs index a2e47c7af..4ec1efa73 100644 --- a/xtask/src/not_bash.rs +++ b/xtask/src/not_bash.rs | |||
@@ -2,6 +2,8 @@ | |||
2 | use std::{ | 2 | use std::{ |
3 | cell::RefCell, | 3 | cell::RefCell, |
4 | env, | 4 | env, |
5 | ffi::OsStr, | ||
6 | fs, | ||
5 | path::PathBuf, | 7 | path::PathBuf, |
6 | process::{Command, Stdio}, | 8 | process::{Command, Stdio}, |
7 | }; | 9 | }; |
@@ -33,6 +35,31 @@ impl Drop for Pushd { | |||
33 | } | 35 | } |
34 | } | 36 | } |
35 | 37 | ||
38 | pub fn rm(glob: &str) -> Result<()> { | ||
39 | let cwd = Env::with(|env| env.cwd()); | ||
40 | ls(glob)?.into_iter().try_for_each(|it| fs::remove_file(cwd.join(it)))?; | ||
41 | Ok(()) | ||
42 | } | ||
43 | |||
44 | pub fn ls(glob: &str) -> Result<Vec<PathBuf>> { | ||
45 | let cwd = Env::with(|env| env.cwd()); | ||
46 | let mut res = Vec::new(); | ||
47 | for entry in fs::read_dir(&cwd)? { | ||
48 | let entry = entry?; | ||
49 | if matches(&entry.file_name(), glob) { | ||
50 | let path = entry.path(); | ||
51 | let path = path.strip_prefix(&cwd).unwrap(); | ||
52 | res.push(path.to_path_buf()) | ||
53 | } | ||
54 | } | ||
55 | return Ok(res); | ||
56 | |||
57 | fn matches(file_name: &OsStr, glob: &str) -> bool { | ||
58 | assert!(glob.starts_with('*')); | ||
59 | file_name.to_string_lossy().ends_with(&glob[1..]) | ||
60 | } | ||
61 | } | ||
62 | |||
36 | #[doc(hidden)] | 63 | #[doc(hidden)] |
37 | pub fn run_process(cmd: String, echo: bool) -> Result<String> { | 64 | pub fn run_process(cmd: String, echo: bool) -> Result<String> { |
38 | run_process_inner(&cmd, echo).with_context(|| format!("process `{}` failed", cmd)) | 65 | run_process_inner(&cmd, echo).with_context(|| format!("process `{}` failed", cmd)) |