Skip to content

Commit

Permalink
List orphan removal from sync
Browse files Browse the repository at this point in the history
And allow sync to run if no upgrades exist, but
removals do.

This doesn't change the behavior of sync, as it
previously removed orphans. We just didn't present
that information to the user nor allow it to run
if only removals exists & not upgrades
  • Loading branch information
tarkah committed Feb 13, 2024
1 parent 1a85bbd commit f3cff94
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 10 deletions.
27 changes: 25 additions & 2 deletions crates/moss/src/cli/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ use moss::{
Provider,
};
use thiserror::Error;
use tui::{pretty::print_to_columns, Stylize};
use tui::{
dialoguer::{theme::ColorfulTheme, Confirm},
pretty::print_to_columns,
Stylize,
};

pub fn command() -> Command {
Command::new("remove")
Expand All @@ -33,6 +37,7 @@ pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {
.flatten()
.map(|name| Provider::from_name(name).unwrap())
.collect::<Vec<_>>();
let yes = *args.get_one::<bool>("yes").unwrap();

// Grab a client for the target, enumerate packages
let client = Client::new(environment::NAME, root).await?;
Expand Down Expand Up @@ -70,7 +75,7 @@ pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {
.await?;

// Remove all pkgs for removal
transaction.remove(for_removal).await?;
transaction.remove(for_removal).await;

// Finalized tx has all reverse deps removed
let finalized = transaction.finalize().cloned().collect::<HashSet<_>>();
Expand All @@ -85,6 +90,18 @@ pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {
print_to_columns(&removed);
println!();

let result = if yes {
true
} else {
Confirm::with_theme(&ColorfulTheme::default())
.with_prompt(" Do you wish to continue? ")
.default(false)
.interact()?
};
if !result {
return Err(Error::Cancelled);
}

// Print each package to stdout
for package in removed {
println!(
Expand Down Expand Up @@ -132,6 +149,9 @@ pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {

#[derive(Debug, Error)]
pub enum Error {
#[error("cancelled")]
Cancelled,

#[error("Not yet implemented")]
NotImplemented,

Expand All @@ -146,4 +166,7 @@ pub enum Error {

#[error("io")]
Io(#[from] std::io::Error),

#[error("string processing")]
Dialog(#[from] tui::dialoguer::Error),
}
23 changes: 18 additions & 5 deletions crates/moss/src/cli/sync.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,29 @@ pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {
.iter()
.filter(|p| client.is_ephemeral() || !installed.iter().any(|i| i.id == p.id))
.collect::<Vec<_>>();
let removed = installed
.iter()
.filter(|p| !finalized.iter().any(|f| f.meta.name == p.meta.name))
.cloned()
.collect::<Vec<_>>();

if synced.is_empty() {
if synced.is_empty() && removed.is_empty() {
println!("No packages to sync");
return Ok(());
}

println!("The following packages will be sync'd: ");
println!();
print_to_columns(synced.as_slice());
println!();
if !synced.is_empty() {
println!("The following packages will be sync'd: ");
println!();
print_to_columns(synced.as_slice());
println!();
}
if !removed.is_empty() {
println!("The following orphaned packages will be removed: ");
println!();
print_to_columns(removed.as_slice());
println!();
}

// Must we prompt?
let result = if yes_all {
Expand Down
4 changes: 1 addition & 3 deletions crates/moss/src/registry/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl<'a> Transaction<'a> {
}

/// Remove a set of packages and their reverse dependencies
pub async fn remove(&mut self, packages: Vec<package::Id>) -> Result<(), Error> {
pub async fn remove(&mut self, packages: Vec<package::Id>) {
// Get transposed subgraph
let transposed = self.packages.transpose();
let subgraph = transposed.subgraph(&packages);
Expand All @@ -79,8 +79,6 @@ impl<'a> Transaction<'a> {
// Remove that package
self.packages.remove_node(package);
});

Ok(())
}

/// Return the package IDs in the fully baked configuration
Expand Down

0 comments on commit f3cff94

Please sign in to comment.