Skip to content

Commit e0b9035

Browse files
committed
moss: info: Show info for cobbled stones
1 parent 88279d0 commit e0b9035

File tree

4 files changed

+52
-20
lines changed

4 files changed

+52
-20
lines changed

moss/src/cli/info.rs

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
//
33
// SPDX-License-Identifier: MPL-2.0
44

5+
use std::path::PathBuf;
6+
57
use clap::{arg, ArgMatches, Command};
6-
use itertools::Itertools;
8+
use itertools::{Either, Itertools};
79
use moss::{
810
client::{self, Client},
911
environment,
@@ -35,26 +37,48 @@ pub fn handle(args: &ArgMatches, installation: Installation) -> Result<(), Error
3537
.collect::<Vec<_>>();
3638
let show_files = args.get_flag("files");
3739

38-
let client = Client::new(environment::NAME, installation)?;
40+
let mut client = Client::new(environment::NAME, installation)?;
41+
42+
// Partition between names and stone paths
43+
let (pkg_names, local_stones): (Vec<String>, Vec<PathBuf>) = pkgs.into_iter().partition_map(|name| {
44+
if name.ends_with(".stone") {
45+
Either::Right(name.into())
46+
} else {
47+
Either::Left(name)
48+
}
49+
});
50+
51+
let mut candidates = vec![];
52+
53+
// Cobble local stones and add their packages
54+
if !local_stones.is_empty() {
55+
candidates.extend(client.cobble(&local_stones)?);
56+
}
3957

40-
for pkg in pkgs {
41-
let lookup = Provider::from_name(&pkg).unwrap();
58+
// Resolve & add all package names
59+
for pkg in pkg_names {
60+
let lookup = Provider::from_name(&pkg).map_err(|_| Error::ParseProvider(pkg.to_string()))?;
4261
let resolved = client
4362
.registry
4463
.by_provider(&lookup, Flags::default())
4564
.collect::<Vec<_>>();
65+
4666
if resolved.is_empty() {
4767
return Err(Error::NotFound(pkg));
4868
}
49-
for candidate in resolved {
50-
print_package(&candidate);
5169

52-
if candidate.flags.installed && show_files {
53-
let vfs = client.vfs([&candidate.id])?;
54-
print_files(vfs);
55-
}
56-
println!();
70+
candidates.extend(resolved);
71+
}
72+
73+
// Print each candidate
74+
for candidate in candidates {
75+
print_package(&candidate);
76+
77+
if candidate.flags.installed && show_files {
78+
let vfs = client.vfs([&candidate.id])?;
79+
print_files(vfs);
5780
}
81+
println!();
5882
}
5983

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

134158
#[derive(Debug, Error)]
135159
pub enum Error {
160+
#[error("Not a valid provider name: {0}")]
161+
ParseProvider(String),
136162
#[error("No such package {0}")]
137163
NotFound(String),
138164
#[error("client")]

moss/src/client/install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pub fn install(client: &mut Client, pkgs: &[&str], yes: bool) -> Result<Timing,
4545

4646
// Add local stones to cobble plugin
4747
if !local_stones.is_empty() {
48-
input.extend(client.cobble(&local_stones)?);
48+
input.extend(client.cobble(&local_stones)?.into_iter().map(|pkg| pkg.id));
4949
}
5050

5151
// Add all inputs

moss/src/client/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl Client {
178178

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

184184
let packages = stones

moss/src/registry/plugin/cobble.rs

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ pub struct Cobble {
2121

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

27-
let mut file = File::open(&path)?;
27+
let mut file = File::open(path)?;
2828

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

5656
// Create a package ID from the hashed path
57-
let id = path_hash(&path);
57+
let id = path_hash(path);
58+
59+
let state = State {
60+
path: path.to_path_buf(),
61+
meta,
62+
};
63+
let package = state.package(id.clone());
5864

5965
// Whack it into the cobbler
60-
self.packages.insert(id.clone(), State { path, meta });
66+
self.packages.insert(id, state);
6167

62-
Ok(id)
68+
Ok(package)
6369
}
6470

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

6975
fn query(&self, flags: package::Flags, filter: impl Fn(&Meta) -> bool) -> Vec<Package> {
70-
if flags.available {
76+
if flags.available || flags == package::Flags::default() {
7177
self.packages
7278
.iter()
7379
.filter(|(_, state)| filter(&state.meta))

0 commit comments

Comments
 (0)