Skip to content

Commit

Permalink
Merge pull request #60 from serpent-os/feat/prune
Browse files Browse the repository at this point in the history
Feat/prune
  • Loading branch information
ikeycode authored Oct 19, 2023
2 parents 71913db + 5b189dd commit 0df3886
Show file tree
Hide file tree
Showing 16 changed files with 605 additions and 153 deletions.
8 changes: 7 additions & 1 deletion moss/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ mod install;
mod list;
mod remove;
mod repo;
mod state;
mod version;

/// Convert the name to a lookup provider
Expand Down Expand Up @@ -64,8 +65,9 @@ fn command() -> Command {
.subcommand(install::command())
.subcommand(list::command())
.subcommand(remove::command())
.subcommand(version::command())
.subcommand(repo::command())
.subcommand(state::command())
.subcommand(version::command())
}

/// Process all CLI arguments
Expand All @@ -90,6 +92,7 @@ pub async fn process() -> Result<(), Error> {
Some(("list", args)) => list::handle(args).await.map_err(Error::List),
Some(("remove", args)) => remove::handle(args, root).await.map_err(Error::Remove),
Some(("repo", args)) => repo::handle(args, root).await.map_err(Error::Repo),
Some(("state", args)) => state::handle(args, root).await.map_err(Error::State),
_ => unreachable!(),
}
}
Expand All @@ -116,4 +119,7 @@ pub enum Error {

#[error("error handling repo: {0}")]
Repo(#[from] repo::Error),

#[error("error handling state: {0}")]
State(#[from] state::Error),
}
2 changes: 1 addition & 1 deletion moss/src/cli/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {
// Add all installed packages to transaction
let mut transaction = client
.registry
.transaction_with_packages(installed_ids.iter().cloned().cloned().collect())
.transaction_with_installed(installed_ids.iter().cloned().cloned().collect())
.await?;

// Remove all pkgs for removal
Expand Down
71 changes: 71 additions & 0 deletions moss/src/cli/state.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// SPDX-FileCopyrightText: Copyright © 2020-2023 Serpent OS Developers
//
// SPDX-License-Identifier: MPL-2.0

use std::path::Path;

use clap::{arg, ArgAction, ArgMatches, Command};
use futures::{stream, StreamExt, TryFutureExt, TryStreamExt};
use moss::{
client::{self, prune, Client},
state,
};
use thiserror::Error;
use tui::pretty::print_to_columns;

pub fn command() -> Command {
Command::new("state")
.about("Manage state")
.long_about("Manage state ...")
.subcommand_required(true)
.subcommand(Command::new("list").about("List all states"))
.subcommand(
Command::new("prune").about("Prune old states").arg(
arg!(-k --keep "Keep this many states")
.action(ArgAction::Set)
.default_value("10")
.value_parser(clap::value_parser!(u64).range(1..)),
),
)
}

pub async fn handle(args: &ArgMatches, root: &Path) -> Result<(), Error> {
match args.subcommand() {
Some(("list", _)) => list(root).await,
Some(("prune", args)) => prune(args, root).await,
_ => unreachable!(),
}
}

pub async fn list(root: &Path) -> Result<(), Error> {
let client = Client::new_for_root(root).await?;

let state_ids = client.state_db.list_ids().await?;

let states = stream::iter(state_ids.iter().map(|(id, _)| id))
.then(|id| client.state_db.get(id).map_err(Error::StateDB))
.try_collect::<Vec<_>>()
.await?;

print_to_columns(&states.iter().map(state::ColumnDisplay).collect::<Vec<_>>());

Ok(())
}

pub async fn prune(args: &ArgMatches, root: &Path) -> Result<(), Error> {
let keep = *args.get_one::<u64>("keep").unwrap();

let client = Client::new_for_root(root).await?;
client.prune(prune::Strategy::KeepRecent(keep)).await?;

Ok(())
}

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

#[error("statedb error: {0}")]
StateDB(#[from] moss::db::state::Error),
}
22 changes: 20 additions & 2 deletions moss/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ use std::path::PathBuf;

use thiserror::Error;

use self::prune::prune;
use crate::{
db,
registry::plugin::{self, Plugin},
repository, Installation, Registry,
repository, Installation, Registry, State,
};

pub mod prune;

#[derive(Debug, Error)]
pub enum Error {
#[error("Root is invalid")]
Expand All @@ -25,6 +28,9 @@ pub enum Error {
Layout(#[from] db::layout::Error),
#[error("state: {0}")]
State(#[from] db::state::Error),

#[error("prune: {0}")]
Prune(#[from] prune::Error),
}

/// A Client is a connection to the underlying package management systems
Expand Down Expand Up @@ -96,12 +102,24 @@ impl Client {

Ok(())
}

pub async fn prune(&self, strategy: prune::Strategy) -> Result<(), Error> {
prune(
strategy,
&self.state_db,
&self.install_db,
&self.layout_db,
&self.installation,
)
.await?;
Ok(())
}
}

async fn build_registry(
repositories: &repository::Manager,
installdb: &db::meta::Database,
state: Option<db::state::State>,
state: Option<State>,
) -> Result<Registry, Error> {
let mut registry = Registry::default();

Expand Down
Loading

0 comments on commit 0df3886

Please sign in to comment.