Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor!: produce JSON output when --porcelain is provided in rocks search #27

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 23 additions & 20 deletions rocks-bin/src/search.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::collections::HashMap;

use clap::Args;
use eyre::Result;
use itertools::Itertools;
Expand All @@ -23,35 +25,36 @@ pub struct Search {
pub async fn search(data: Search, config: &Config) -> Result<()> {
let formatting = TreeFormatting::dir_tree(FormatCharacters::box_chars());

// TODO(vhyrro): Pull in global configuration in the form of a second parameter (including which server to use for the manifest).

let manifest = manifest_from_server(config.server.to_owned(), config).await?;

let metadata = ManifestMetadata::new(&manifest)?;

for key in metadata
let rock_to_version_map: HashMap<&String, Vec<&String>> = metadata
.repository
.keys()
.sorted()
.collect::<Vec<&String>>()
{
// TODO(vhyrro): Use fuzzy matching here instead.
if key.contains(&data.name) {
let versions = metadata.repository[key]
.keys()
.sorted_by(|a, b| Ord::cmp(b, a));

if data.porcelain {
versions.for_each(|version| {
println!("{} {} src|rockspec {}", key, version, config.server)
});
.iter()
.filter_map(|(key, value)| {
if key.contains(&data.name) {
Some((
key,
value.keys().sorted_by(|a, b| Ord::cmp(b, a)).collect_vec(),
))
} else {
let mut tree = StringTreeNode::new(key.to_owned());
None
}
})
.collect();

versions.for_each(|version| tree.push(version.to_owned()));
if data.porcelain {
println!("{}", serde_json::to_string(&rock_to_version_map)?);
} else {
for (key, versions) in rock_to_version_map.into_iter().sorted() {
let mut tree = StringTreeNode::new(key.to_owned());

println!("{}", tree.to_string_with_format(&formatting)?);
for version in versions {
tree.push(version.to_owned());
}

println!("{}", tree.to_string_with_format(&formatting).unwrap());
}
Comment on lines +47 to 58
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: this could be rewritten as a match statement for utmost clarity I guess, but I'm impartial to cosmetics like this. Thoughts are welcome.

}

Expand Down
Loading