Skip to content

Commit

Permalink
moss/cli: Use name_to_provider mapper for install/info
Browse files Browse the repository at this point in the history
This allows a trivial lookup using the provider tables.
Note - this *should* be faster.

Signed-off-by: Ikey Doherty <ikey@serpentos.com>
  • Loading branch information
ikeycode committed Sep 20, 2023
1 parent 5acac51 commit 552979e
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
8 changes: 5 additions & 3 deletions moss/src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ use futures::StreamExt;
use itertools::Itertools;
use moss::{
client::{self, Client},
package::{Flags, Name},
package::Flags,
Package,
};
use thiserror::Error;
use tui::Stylize;

use super::name_to_provider;

const COLUMN_WIDTH: usize = 20;

pub fn command() -> Command {
Expand All @@ -37,10 +39,10 @@ pub async fn handle(args: &ArgMatches) -> Result<(), Error> {
let client = Client::new_for_root(root).await?;

for pkg in pkgs {
let nom = Name::from(pkg.clone());
let lookup = name_to_provider(&pkg);
let resolved = client
.registry
.by_name(&nom, Flags::AVAILABLE)
.by_provider(&lookup, Flags::AVAILABLE)
.collect::<Vec<_>>()
.await;
if resolved.is_empty() {
Expand Down
14 changes: 4 additions & 10 deletions moss/src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::{path::PathBuf, str::FromStr};
use std::path::PathBuf;

use clap::{arg, ArgMatches, Command};
use futures::StreamExt;
use moss::{
client::{self, Client},
package::Flags,
Provider,
};
use thiserror::Error;

use crate::cli::name_to_provider;

pub fn command() -> Command {
Command::new("install")
.about("Install packages")
Expand All @@ -36,14 +37,7 @@ pub async fn handle(args: &ArgMatches) -> Result<(), Error> {
let mut requested = vec![];

for pkg in pkgs {
let lookup = if pkg.contains('(') {
Provider::from_str(pkg.as_str()).unwrap()
} else {
Provider {
kind: moss::dependency::Kind::PackageName,
name: pkg.clone(),
}
};
let lookup = name_to_provider(&pkg);

let result = client
.registry
Expand Down
15 changes: 14 additions & 1 deletion moss/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::path::PathBuf;
use std::{path::PathBuf, str::FromStr};

use clap::{Arg, ArgAction, Command};
use moss::Provider;
use thiserror::Error;

mod extract;
Expand All @@ -16,6 +17,18 @@ mod remove;
mod repo;
mod version;

/// Convert the name to a lookup provider
pub(crate) fn name_to_provider(name: &str) -> Provider {
if name.contains('(') {
Provider::from_str(name).unwrap()
} else {
Provider {
kind: moss::dependency::Kind::PackageName,
name: name.to_owned(),
}
}
}

/// Generate the CLI command structure
fn command() -> Command {
Command::new("moss")
Expand Down

0 comments on commit 552979e

Please sign in to comment.