Skip to content

Commit

Permalink
moss/cli: Add boilerplate for DB merge in install
Browse files Browse the repository at this point in the history
We can now merge the metapayload (and consume the package type) upon
installation of packages, however layoutdb merge is presently broken
pending some batch_add fixups to match metadb.

Signed-off-by: Ikey Doherty <ikey@serpentos.com>
  • Loading branch information
ikeycode committed Oct 9, 2023
1 parent 4b60243 commit de5a04e
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 8 deletions.
49 changes: 43 additions & 6 deletions moss/src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use moss::{
registry::transaction,
Package,
};
use stone::read::Payload;
use thiserror::Error;
use tui::{pretty::print_to_columns, MultiProgress, ProgressBar, ProgressStyle, Stylize};

Expand Down Expand Up @@ -132,17 +133,19 @@ pub async fn handle(args: &ArgMatches) -> Result<(), Error> {
})
.await?;

let package_name = package.meta.name.to_string();

// Set progress to unpacking
progress_bar.set_message(format!(
"{} {}",
"Unpacking".yellow(),
package.meta.name.to_string().bold(),
package_name.clone().bold(),
));
progress_bar.set_length(1000);
progress_bar.set_position(0);

// Unpack and update progress
download
let unpacked = download
.unpack({
let progress_bar = progress_bar.clone();

Expand All @@ -152,11 +155,39 @@ pub async fn handle(args: &ArgMatches) -> Result<(), Error> {
})
.await?;

// Merge into the DB!
progress_bar.set_message(format!(
"{} {}",
"Accounting".white(),
package_name.clone().bold()
));
progress_bar.set_length(2);
progress_bar.set_position(0);

// Merge layoutdb
let _layouts = unpacked
.payloads
.iter()
.find_map(Payload::layout)
.ok_or(Error::CorruptedPackage)?
.iter()
.map(|l| (package.id.clone(), l.to_owned()))
.collect_vec();

// FIXME: batch_add broken from too many variables
//client.layout_db.batch_add(layouts).await?;

progress_bar.set_position(1);

// Consume the package
client.install_db.add(package.id, package.meta).await?;
progress_bar.set_position(2);

// Write installed line
multi_progress.println(format!(
"{} {}",
"Installed".green(),
package.meta.name.to_string().bold(),
package_name.clone().bold(),
))?;

// Remove this progress bar
Expand All @@ -166,9 +197,6 @@ pub async fn handle(args: &ArgMatches) -> Result<(), Error> {
// Inc total progress by 1
total_progress.inc(1);

// Get smarter borrow checker
drop(package);

Ok(()) as Result<(), Error>
}))
.buffer_unordered(CONCURRENT_TASKS)
Expand All @@ -186,6 +214,9 @@ pub enum Error {
#[error("client error")]
Client(#[from] client::Error),

#[error("corrupted package")]
CorruptedPackage,

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

Expand All @@ -198,6 +229,12 @@ pub enum Error {
#[error("package fetch error: {0}")]
Package(#[from] package::fetch::Error),

#[error("installdb error: {0}")]
InstallDB(#[from] moss::db::meta::Error),

#[error("layoutdb error: {0}")]
LayoutDB(#[from] moss::db::layout::Error),

#[error("io error: {0}")]
Io(#[from] std::io::Error),
}
14 changes: 12 additions & 2 deletions moss/src/package/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,20 @@ pub struct Download {
installation: Installation,
}

/// Upon fetch completion we have this unpacked asset bound with
/// an open reader
pub struct UnpackedAsset {
id: Id,
pub payloads: Vec<Payload>,
}

impl Download {
/// Unpack the downloaded package
// TODO: Return an "Unpacked" struct which has a "blit" method on it?
pub async fn unpack(
self,
on_progress: impl Fn(Progress) + Send + 'static,
) -> Result<(), Error> {
) -> Result<UnpackedAsset, Error> {
use std::fs::{create_dir_all, remove_file, File};
use std::io::{copy, Read, Seek, SeekFrom, Write};

Expand Down Expand Up @@ -173,7 +180,10 @@ impl Download {

remove_file(&content_path)?;

Ok(())
Ok(UnpackedAsset {
id: self.id.clone(),
payloads,
})
})
.await
.expect("join handle")
Expand Down

0 comments on commit de5a04e

Please sign in to comment.