Skip to content

Commit

Permalink
add version subcommand
Browse files Browse the repository at this point in the history
Signed-off-by: innocentzero <[email protected]>
  • Loading branch information
InnocentZero committed Nov 15, 2024
1 parent 3914d03 commit cbd9fa9
Show file tree
Hide file tree
Showing 14 changed files with 126 additions and 1 deletion.
21 changes: 21 additions & 0 deletions src/backends/all.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ macro_rules! any {
$($upper_backend,)*
}
impl AnyBackend {
pub fn version(&self, config: &Config) -> Result<String> {
match self {
$( AnyBackend::$upper_backend => $upper_backend::version(config), )*
}
}
pub fn remove_packages(&self, packages: &BTreeSet<String>, no_confirm: bool, config: &Config) -> Result<()> {
match self {
$( AnyBackend::$upper_backend => $upper_backend::remove_packages(packages, no_confirm, config), )*
Expand All @@ -48,8 +53,24 @@ macro_rules! any {
}
};
}

macro_rules! versions {
($(($upper_backend:ident, $lower_backend:ident)), *) => {
pub fn backend_versions(config: &Config) -> BTreeMap<String, String> {
let mut results = BTreeMap::new();
$(
results.insert(stringify!($upper_backend).to_string(), AnyBackend::version(&AnyBackend::$upper_backend, config).unwrap());
)*

results
}
}
}

apply_public_backends!(any);

apply_public_backends!(versions);

macro_rules! raw_package_ids {
($(($upper_backend:ident, $lower_backend:ident)),*) => {
#[derive(Debug, Clone, Default)]
Expand Down
8 changes: 8 additions & 0 deletions src/backends/apt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ impl Backend for Apt {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("apt") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["apt", "--version"], Perms::Same)
}
}
}
11 changes: 11 additions & 0 deletions src/backends/arch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,4 +187,15 @@ impl Backend for Arch {

Ok(())
}

fn version(config: &Config) -> Result<String> {
if !command_found(config.arch_package_manager.as_command()) {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(
[config.arch_package_manager.as_command(), "--version"],
Perms::Same,
)
}
}
}
8 changes: 8 additions & 0 deletions src/backends/brew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,12 @@ impl Backend for Brew {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("brew") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["brew", "--version"], Perms::Same)
}
}
}
10 changes: 9 additions & 1 deletion src/backends/cargo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use serde_inline_default::serde_inline_default;
use serde_json::Value;

use crate::cmd::{command_found, run_command};
use crate::cmd::{command_found, run_command, run_command_for_stdout};
use crate::prelude::*;

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
Expand Down Expand Up @@ -113,6 +113,14 @@ impl Backend for Cargo {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("cargo") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["cargo", "--version"], Perms::Same)
}
}
}

fn extract_packages(contents: &str) -> Result<BTreeMap<String, CargoQueryInfo>> {
Expand Down
8 changes: 8 additions & 0 deletions src/backends/dnf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,14 @@ impl Backend for Dnf {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("dnf") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["dnf", "--version"], Perms::Same)
}
}
}

fn parse_package(package: &str) -> String {
Expand Down
8 changes: 8 additions & 0 deletions src/backends/flatpak.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,4 +180,12 @@ impl Backend for Flatpak {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("flatpak") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["flatpak", "--version"], Perms::Same)
}
}
}
2 changes: 2 additions & 0 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ pub trait Backend {
no_confirm: bool,
config: &Config,
) -> Result<()>;

fn version(config: &Config) -> Result<String>;
}
8 changes: 8 additions & 0 deletions src/backends/pipx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,14 @@ impl Backend for Pipx {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("pipx") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["pipx", "--version"], Perms::Same)
}
}
}

fn extract_package_names(stdout: String) -> Result<BTreeSet<String>> {
Expand Down
8 changes: 8 additions & 0 deletions src/backends/rustup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,12 @@ impl Backend for Rustup {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("rustup") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["rustup", "--version"], Perms::Same)
}
}
}
8 changes: 8 additions & 0 deletions src/backends/winget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,12 @@ impl Backend for WinGet {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("winget") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["winget", "--version"], Perms::Same)
}
}
}
8 changes: 8 additions & 0 deletions src/backends/xbps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,12 @@ impl Backend for Xbps {

Ok(())
}

fn version(_: &Config) -> Result<String> {
if !command_found("xbps-query") {
Ok(String::from("Not found\n"))
} else {
run_command_for_stdout(["xbps-query", "--version"], Perms::Same)
}
}
}
6 changes: 6 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub enum MainSubcommand {
Review(ReviewCommand),
Sync(SyncCommand),
Unmanaged(UnmanagedCommand),
Backends(BackendsCommand),
}

#[derive(Args)]
Expand Down Expand Up @@ -76,3 +77,8 @@ pub struct SyncCommand {
#[command(visible_alias("u"))]
/// show explicitly installed packages not managed by metapac
pub struct UnmanagedCommand {}

#[derive(Args)]
#[command(visible_alias("b"))]
/// show the backends found by metapac
pub struct BackendsCommand {}
13 changes: 13 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use color_eyre::Result;
use dialoguer::Confirm;
use toml_edit::{Array, DocumentMut, Item, Value};

use crate::backends::all::backend_versions;
use crate::cli::BackendsCommand;
use crate::prelude::*;
use crate::review::review;

Expand Down Expand Up @@ -41,6 +43,7 @@ impl MainArguments {
MainSubcommand::Review(review) => review.run(&managed, &config),
MainSubcommand::Sync(sync) => sync.run(&managed, &config),
MainSubcommand::Unmanaged(unmanaged) => unmanaged.run(&managed, &config),
MainSubcommand::Backends(found_backends) => found_backends.run(&config),
}
}
}
Expand Down Expand Up @@ -170,6 +173,16 @@ impl UnmanagedCommand {
}
}

impl BackendsCommand {
fn run(self, config: &Config) -> Result<()> {
for (backend, version) in backend_versions(config) {
println!("{backend}:");
println!("{version}");
}
Ok(())
}
}

fn unmanaged(managed: &InstallOptions, config: &Config) -> Result<PackageIds> {
QueryInfos::query_installed_packages(config)
.map(|x| x.to_package_ids().difference(&managed.to_package_ids()))
Expand Down

0 comments on commit cbd9fa9

Please sign in to comment.