Skip to content

Commit

Permalink
moss: info: Show info for cobbled stones
Browse files Browse the repository at this point in the history
  • Loading branch information
tarkah committed Apr 24, 2024
1 parent 88279d0 commit 958c346
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 20 deletions.
48 changes: 37 additions & 11 deletions moss/src/cli/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@
//
// SPDX-License-Identifier: MPL-2.0

use std::path::PathBuf;

use clap::{arg, ArgMatches, Command};
use itertools::Itertools;
use itertools::{Either, Itertools};
use moss::{
client::{self, Client},
environment,
Expand Down Expand Up @@ -35,26 +37,48 @@ pub fn handle(args: &ArgMatches, installation: Installation) -> Result<(), Error
.collect::<Vec<_>>();
let show_files = args.get_flag("files");

let client = Client::new(environment::NAME, installation)?;
let mut client = Client::new(environment::NAME, installation)?;

// Partition between names and stone paths
let (pkg_names, local_stones): (Vec<String>, Vec<PathBuf>) = pkgs.into_iter().partition_map(|name| {
if name.ends_with(".stone") {
Either::Right(name.into())
} else {
Either::Left(name)
}
});

let mut candidates = vec![];

// Cobble local stones and add their packages
if !local_stones.is_empty() {
candidates.extend(client.cobble(&local_stones)?);
}

for pkg in pkgs {
let lookup = Provider::from_name(&pkg).unwrap();
// Resolve & add all package names
for pkg in pkg_names {
let lookup = Provider::from_name(&pkg).map_err(|_| Error::ParseProvider(pkg.to_string()))?;
let resolved = client
.registry
.by_provider(&lookup, Flags::default())
.collect::<Vec<_>>();

if resolved.is_empty() {
return Err(Error::NotFound(pkg));
}
for candidate in resolved {
print_package(&candidate);

if candidate.flags.installed && show_files {
let vfs = client.vfs([&candidate.id])?;
print_files(vfs);
}
println!();
candidates.extend(resolved);
}

// Print each candidate
for candidate in candidates {
print_package(&candidate);

if candidate.flags.installed && show_files {
let vfs = client.vfs([&candidate.id])?;
print_files(vfs);
}
println!();
}

Ok(())
Expand Down Expand Up @@ -133,6 +157,8 @@ fn print_files(vfs: vfs::Tree<client::PendingFile>) {

#[derive(Debug, Error)]
pub enum Error {
#[error("Not a valid provider name: {0}")]
ParseProvider(String),
#[error("No such package {0}")]
NotFound(String),
#[error("client")]
Expand Down
2 changes: 1 addition & 1 deletion moss/src/client/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub fn install(client: &mut Client, pkgs: &[&str], yes: bool) -> Result<Timing,

// Add local stones to cobble plugin
if !local_stones.is_empty() {
input.extend(client.cobble(&local_stones)?);
input.extend(client.cobble(&local_stones)?.into_iter().map(|pkg| pkg.id));
}

// Add all inputs
Expand Down
2 changes: 1 addition & 1 deletion moss/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ impl Client {

/// Adds the provided binary stone paths into a cobble plugin to
/// query / install local stones
pub fn cobble(&mut self, stones: &[&Path]) -> Result<Vec<package::Id>, Error> {
pub fn cobble<T: AsRef<Path>>(&mut self, stones: &[T]) -> Result<Vec<Package>, Error> {
let mut cobble = plugin::Cobble::default();

let packages = stones
Expand Down
20 changes: 13 additions & 7 deletions moss/src/registry/plugin/cobble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ pub struct Cobble {

impl Cobble {
/// Add a package to the cobble set
pub fn add_package(&mut self, path: impl Into<PathBuf>) -> Result<package::Id, Error> {
let path = path.into();
pub fn add_package(&mut self, path: impl AsRef<Path>) -> Result<Package, Error> {
let path = path.as_ref();

let mut file = File::open(&path)?;
let mut file = File::open(path)?;

// Hash file to SHA256 and get size
let (file_size, file_hash) = stat_file(&file)?;
Expand Down Expand Up @@ -54,20 +54,26 @@ impl Cobble {
meta.download_size = Some(file_size);

// Create a package ID from the hashed path
let id = path_hash(&path);
let id = path_hash(path);

let state = State {
path: path.to_path_buf(),
meta,
};
let package = state.package(id.clone());

// Whack it into the cobbler
self.packages.insert(id.clone(), State { path, meta });
self.packages.insert(id, state);

Ok(id)
Ok(package)
}

pub fn package(&self, id: &package::Id) -> Option<Package> {
self.packages.get(id).map(|state| state.package(id.clone()))
}

fn query(&self, flags: package::Flags, filter: impl Fn(&Meta) -> bool) -> Vec<Package> {
if flags.available {
if flags.available || flags == package::Flags::default() {
self.packages
.iter()
.filter(|(_, state)| filter(&state.meta))
Expand Down
1 change: 1 addition & 0 deletions moss/src/registry/plugin/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ impl Plugin {

/// Returns a list of packages with matching `provider` and `flags`
pub fn query_provider(&self, provider: &Provider, flags: package::Flags) -> package::Sorted<Vec<Package>> {
dbg!(&provider);
package::Sorted::new(match self {
Plugin::Active(plugin) => plugin.query_provider(provider, flags),
Plugin::Cobble(plugin) => plugin.query_provider(provider, flags),
Expand Down

0 comments on commit 958c346

Please sign in to comment.