Skip to content

Commit

Permalink
moss/cli/install: Add parsing lookup to find all input candidates
Browse files Browse the repository at this point in the history
This is required for the boulder interaction when we want to install by
pkgconfig names, etc.

Signed-off-by: Ikey Doherty <ikey@serpentos.com>
  • Loading branch information
ikeycode committed Sep 20, 2023
1 parent 040f494 commit 5acac51
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
68 changes: 67 additions & 1 deletion moss/src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,76 @@
//
// SPDX-License-Identifier: MPL-2.0

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

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

pub fn command() -> Command {
Command::new("install")
.about("Install packages")
.long_about("Install the requested software to the local system")
.arg(arg!(<NAME> ... "packages to install").value_parser(clap::value_parser!(String)))
}

/// Handle execution of `moss install`
pub async fn handle(args: &ArgMatches) -> Result<(), Error> {
let root = args.get_one::<PathBuf>("root").unwrap().clone();

let pkgs = args
.get_many::<String>("NAME")
.into_iter()
.flatten()
.cloned()
.collect::<Vec<_>>();

// Grab a client for the target, enumerate packages
let client = Client::new_for_root(root).await?;
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 result = client
.registry
.by_provider(&lookup, Flags::AVAILABLE)
.collect::<Vec<_>>()
.await;
if result.is_empty() {
return Err(Error::NoCandidate(pkg));
}
let front = result.first().unwrap();
requested.push(front.meta.id().clone());
}
requested.sort_by_key(|i| i.to_string());
requested.dedup();

println!("Candidates: {:?}", requested);

Err(Error::NotImplemented)
}

#[derive(Debug, Error)]
pub enum Error {
#[error("client error")]
Client(#[from] client::Error),

#[error("no such candidate: {0}")]
NoCandidate(String),

#[error("not yet implemented")]
NotImplemented,
}
4 changes: 4 additions & 0 deletions moss/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ pub async fn process() -> Result<(), Error> {
Some(("extract", args)) => extract::handle(args).await.map_err(Error::Extract),
Some(("info", args)) => info::handle(args).await.map_err(Error::Info),
Some(("inspect", args)) => inspect::handle(args).await.map_err(Error::Inspect),
Some(("install", args)) => install::handle(args).await.map_err(Error::Install),
Some(("version", _)) => {
version::print();
Ok(())
Expand All @@ -84,6 +85,9 @@ pub enum Error {
#[error("error handling info: {0}")]
Info(#[from] info::Error),

#[error("error handling install: {0}")]
Install(#[from] install::Error),

#[error("error handling list: {0}")]
List(#[from] list::Error),

Expand Down

0 comments on commit 5acac51

Please sign in to comment.