Skip to content

Commit

Permalink
(feat) add snap backend
Browse files Browse the repository at this point in the history
  • Loading branch information
watzon authored and ripytide committed Nov 24, 2024
1 parent 3f780d6 commit c7a91fa
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/backends/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ pub mod dnf;
pub mod flatpak;
pub mod pipx;
pub mod rustup;
pub mod snap;
pub mod winget;
pub mod xbps;

Expand All @@ -27,6 +28,7 @@ macro_rules! apply_public_backends {
(Flatpak, flatpak),
(Pipx, pipx),
(Rustup, rustup),
(Snap, snap),
(WinGet, winget),
(Xbps, xbps) }
};
Expand Down
89 changes: 89 additions & 0 deletions src/backends/snap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
use std::collections::{BTreeMap, BTreeSet};

use color_eyre::Result;
use serde::{Deserialize, Serialize};

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

#[derive(Debug, Copy, Clone, Default, PartialEq, Eq, PartialOrd, Ord, derive_more::Display)]
pub struct Snap;

#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct SnapQueryInfo {}

#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
pub struct SnapInstallOptions {}

impl Backend for Snap {
type QueryInfo = SnapQueryInfo;
type InstallOptions = SnapInstallOptions;

fn map_managed_packages(
packages: BTreeMap<String, Self::InstallOptions>,
_: &Config,
) -> Result<BTreeMap<String, Self::InstallOptions>> {
Ok(packages)
}

fn query_installed_packages(config: &Config) -> Result<BTreeMap<String, Self::QueryInfo>> {
if Self::version(config).is_err() {
return Ok(BTreeMap::new());
}

let output = run_command_for_stdout(["snap", "list"], Perms::Same, false)?;

// Skip the first line which is the header
Ok(output
.lines()
.skip(1)
.filter_map(|line| line.split_whitespace().next())
.map(|name| (name.to_string(), SnapQueryInfo {}))
.collect())
}

fn install_packages(
packages: &BTreeMap<String, Self::InstallOptions>,
no_confirm: bool,
_: &Config,
) -> Result<()> {
if !packages.is_empty() {
run_command(
["snap", "install"]
.into_iter()
.chain(Some("--assume-yes").filter(|_| no_confirm))
.chain(packages.keys().map(String::as_str)),
Perms::Sudo,
)?;
}

Ok(())
}

fn remove_packages(packages: &BTreeSet<String>, no_confirm: bool, _: &Config) -> Result<()> {
if !packages.is_empty() {
run_command(
["snap", "remove"]
.into_iter()
.chain(Some("--assume-yes").filter(|_| no_confirm))
.chain(packages.iter().map(String::as_str)),
Perms::Sudo,
)?;
}

Ok(())
}

fn version(_: &Config) -> Result<String> {
run_command_for_stdout(["snap", "--version"], Perms::Same, false)
.map(|output| {
output
.lines()
.next()
.unwrap_or_default()
.split_whitespace()
.collect::<Vec<_>>()
.join(" ")
})
}
}
1 change: 1 addition & 0 deletions src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub use crate::backends::dnf::{Dnf, DnfInstallOptions, DnfQueryInfo};
pub use crate::backends::flatpak::{Flatpak, FlatpakInstallOptions, FlatpakQueryInfo};
pub use crate::backends::pipx::{Pipx, PipxInstallOptions, PipxQueryOptions};
pub use crate::backends::rustup::{Rustup, RustupInstallOptions, RustupQueryInfo};
pub use crate::backends::snap::{Snap, SnapInstallOptions, SnapQueryInfo};
pub use crate::backends::winget::{WinGet, WinGetInstallOptions, WinGetQueryInfo};
pub use crate::backends::xbps::{Xbps, XbpsInstallOptions, XbpsQueryInfo};
pub use crate::backends::{Backend, StringPackageStruct};
Expand Down

0 comments on commit c7a91fa

Please sign in to comment.