Skip to content

Commit

Permalink
feat(bin): adopt to breaking changes in lib, sort results of search
Browse files Browse the repository at this point in the history
… alphabetically
  • Loading branch information
vhyrro committed Jan 19, 2024
1 parent 4724230 commit 3ef003e
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
5 changes: 4 additions & 1 deletion rocks-bin/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::path::PathBuf;

use clap::{Parser, Subcommand};
use rocks_lib::config::Config;

mod download;
mod search;
Expand Down Expand Up @@ -122,9 +123,11 @@ enum Commands {
async fn main() {
let cli = Cli::parse();

let config = Config::default();

match cli.command {
Some(command) => match command {
Commands::Search(search_data) => search::search(search_data).await.unwrap(),
Commands::Search(search_data) => search::search(search_data, &config).await.unwrap(),
Commands::Download(download_data) => download::download(download_data).await.unwrap(),
_ => unimplemented!(),
},
Expand Down
21 changes: 13 additions & 8 deletions rocks-bin/src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use clap::Args;
use itertools::Itertools;
use text_trees::{FormatCharacters, StringTreeNode, TreeFormatting};

use rocks_lib::manifest::{manifest_from_server, ManifestMetadata};
use rocks_lib::{
config::Config,
manifest::{manifest_from_server, ManifestMetadata},
};

#[derive(Args)]
pub struct Search {
Expand All @@ -17,16 +20,21 @@ pub struct Search {
porcelain: bool,
}

pub async fn search(data: Search) -> Result<()> {
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("https://luarocks.org".into(), None, None).await?;
let manifest = manifest_from_server(config.server.to_owned(), &config).await?;

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

for key in metadata.repository.keys().collect::<Vec<&String>>() {
for key in metadata
.repository
.keys()
.sorted()
.collect::<Vec<&String>>()
{
// TODO(vhyrro): Use fuzzy matching here instead.
if key.contains(&data.name) {
let versions = metadata.repository[key]
Expand All @@ -35,10 +43,7 @@ pub async fn search(data: Search) -> Result<()> {

if data.porcelain {
versions.for_each(|version| {
println!(
"{} {} {} {}",
key, version, "src|rockspec", "https://luarocks.org/"
)
println!("{} {} {} {}", key, version, "src|rockspec", config.server)
});
} else {
let mut tree = StringTreeNode::new(key.to_owned());
Expand Down

0 comments on commit 3ef003e

Please sign in to comment.