-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Matthias Beyer <mail@beyermatthias.de>
- Loading branch information
1 parent
d28e81e
commit 39d3c28
Showing
4 changed files
with
149 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
use std::{path::{Path, PathBuf}, io::BufReader}; | ||
|
||
use crate::{cli::{ShowRange, ShowFormat}, config::Configuration, error::{Error, FragmentError}, fragment::Fragment}; | ||
|
||
#[derive(Debug, typed_builder::TypedBuilder)] | ||
pub struct Show { | ||
format: Option<crate::cli::ShowFormat>, | ||
range: Option<ShowRange>, | ||
} | ||
|
||
impl crate::command::Command for Show { | ||
fn execute(self, workdir: &Path, config: &Configuration) -> Result<(), Error> { | ||
fn walk_directory(path: &Path, filter_version: Option<&(semver::Version, semver::Version)>) -> Result<Vec<PathBuf>, Error> { | ||
walkdir::WalkDir::new(path) | ||
.follow_links(false) | ||
.max_open(100) | ||
.same_file_system(true) | ||
.into_iter() | ||
.filter_entry(|de| { | ||
log::debug!("Looking at {de:?}"); | ||
if de.path().is_dir() { | ||
true | ||
} else if de.path().is_file() { | ||
de.path().components().any(|comp| match comp { | ||
std::path::Component::Normal(osstr) => osstr.to_str() | ||
.map(|s| { | ||
if let Ok(version) = semver::Version::parse(s) { | ||
if let Some((from, until)) = filter_version { | ||
version > *from && version < *until | ||
} else { | ||
true | ||
} | ||
} | ||
else { | ||
false | ||
} | ||
}) | ||
.unwrap_or(false), | ||
_ => false, | ||
}) | ||
} else { | ||
false | ||
} | ||
}) | ||
.filter_map(|rde| match rde { | ||
Ok(de) => de.path().is_file().then(|| de.path().to_path_buf()).map(Ok), | ||
Err(e) => Some(Err(Error::from(e))), | ||
}) | ||
.collect::<Result<Vec<PathBuf>, Error>>() | ||
} | ||
|
||
let pathes = match self.range { | ||
None | Some(ShowRange::Unreleased) => { | ||
let unreleased_dir_path = workdir | ||
.join(config.fragment_dir()) | ||
.join(crate::consts::UNRELEASED_DIR_NAME); | ||
walk_directory(&unreleased_dir_path, None)? | ||
} | ||
Some(ShowRange::Exact { exact }) => { | ||
let path = workdir.join(config.fragment_dir()).join(&exact); | ||
if !path.exists() { | ||
return Err(Error::ExactVersionDoesNotExist { version: exact }); | ||
} | ||
walk_directory(&path, None)? | ||
} | ||
Some(ShowRange::Range { from, until }) => { | ||
let from = semver::Version::parse(&from)?; | ||
let until = semver::Version::parse(&until)?; | ||
|
||
let fragment_dir_path = workdir.join(config.fragment_dir()); | ||
walk_directory(&fragment_dir_path, Some(&(from, until)))? | ||
} | ||
}; | ||
|
||
let fragments = pathes | ||
.into_iter() | ||
.map(|path| { | ||
std::fs::OpenOptions::new() | ||
.read(true) | ||
.create(false) | ||
.write(false) | ||
.open(&path) | ||
.map_err(FragmentError::from) | ||
.map(BufReader::new) | ||
.and_then(|mut reader| Fragment::from_reader(&mut reader)) | ||
.map_err(|e| Error::FragmentError(e, path.to_path_buf())) | ||
}); | ||
|
||
match self.format { | ||
None | Some(ShowFormat::Text) => pretty_print(fragments), | ||
Some(ShowFormat::Json) => json_print(fragments), | ||
} | ||
} | ||
} | ||
|
||
fn pretty_print(iter: impl Iterator<Item = Result<Fragment, Error>>) -> Result<(), Error> { | ||
use std::io::Write; | ||
|
||
let out = std::io::stdout(); | ||
let mut output = out.lock(); | ||
|
||
iter.map(|fragment| { | ||
let fragment = fragment?; | ||
fragment.header() | ||
.iter() | ||
.map(|(key, value)| { | ||
writeln!(output, "{key}: {value}", value = value.display())?; | ||
Ok(()) as Result<(), Error> | ||
}) | ||
.collect::<Result<_, _>>()?; | ||
|
||
writeln!(output, "{text}", text = fragment.text())?; | ||
Ok(()) | ||
}) | ||
.collect::<Result<(), _>>() | ||
} | ||
|
||
fn json_print(_iter: impl Iterator<Item = Result<Fragment, Error>>) -> Result<(), Error> { | ||
unimplemented!() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters