diff options
author | fxqnlr <[email protected]> | 2024-09-14 14:20:52 +0200 |
---|---|---|
committer | fxqnlr <[email protected]> | 2024-09-14 14:20:52 +0200 |
commit | 1b99a4a1ed7772c9b68e59f46e493ea5b4715239 (patch) | |
tree | cd4e5b2dbac773ba1797cbe2c8bde62c7a94659f | |
parent | 553bbac36bdc483135a7053ca64507e01397e5e1 (diff) | |
download | arbs-1b99a4a1ed7772c9b68e59f46e493ea5b4715239.tar arbs-1b99a4a1ed7772c9b68e59f46e493ea5b4715239.tar.gz arbs-1b99a4a1ed7772c9b68e59f46e493ea5b4715239.zip |
add portage package manager
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | Cargo.toml | 6 | ||||
-rw-r--r-- | src/packages.rs | 18 | ||||
-rw-r--r-- | src/packages/portage.rs | 57 |
4 files changed, 56 insertions, 32 deletions
@@ -76,16 +76,9 @@ dependencies = [ | |||
76 | ] | 76 | ] |
77 | 77 | ||
78 | [[package]] | 78 | [[package]] |
79 | name = "anyhow" | ||
80 | version = "1.0.86" | ||
81 | source = "registry+https://github.com/rust-lang/crates.io-index" | ||
82 | checksum = "b3d1d046238990b9cf5bcde22a3fb3584ee5cf65fb2765f454ed428c7a0063da" | ||
83 | |||
84 | [[package]] | ||
85 | name = "arbs" | 79 | name = "arbs" |
86 | version = "0.1.0" | 80 | version = "0.1.0" |
87 | dependencies = [ | 81 | dependencies = [ |
88 | "anyhow", | ||
89 | "clap", | 82 | "clap", |
90 | "color-eyre", | 83 | "color-eyre", |
91 | "config", | 84 | "config", |
@@ -4,7 +4,6 @@ edition = "2021" | |||
4 | version = "0.1.0" | 4 | version = "0.1.0" |
5 | 5 | ||
6 | [dependencies] | 6 | [dependencies] |
7 | anyhow = "1.0.86" | ||
8 | color-eyre = "0.6.3" | 7 | color-eyre = "0.6.3" |
9 | dirs = "5.0.1" | 8 | dirs = "5.0.1" |
10 | gethostname = "0.5.0" | 9 | gethostname = "0.5.0" |
@@ -34,8 +33,3 @@ features = ["env-filter"] | |||
34 | [dependencies.uuid] | 33 | [dependencies.uuid] |
35 | version = "1.10.0" | 34 | version = "1.10.0" |
36 | features = ["v4"] | 35 | features = ["v4"] |
37 | |||
38 | [features] | ||
39 | default = ["pacman", "portage"] | ||
40 | pacman = [] | ||
41 | portage = [] | ||
diff --git a/src/packages.rs b/src/packages.rs index 2eadcfc..de818f4 100644 --- a/src/packages.rs +++ b/src/packages.rs | |||
@@ -6,9 +6,7 @@ use serde::{Deserialize, Serialize}; | |||
6 | 6 | ||
7 | use crate::error::{Error, Result}; | 7 | use crate::error::{Error, Result}; |
8 | 8 | ||
9 | #[cfg(feature = "pacman")] | ||
10 | mod pacman; | 9 | mod pacman; |
11 | #[cfg(feature = "portage")] | ||
12 | mod portage; | 10 | mod portage; |
13 | 11 | ||
14 | #[derive(Debug, Serialize, Deserialize)] | 12 | #[derive(Debug, Serialize, Deserialize)] |
@@ -19,7 +17,9 @@ pub struct PackageList { | |||
19 | 17 | ||
20 | impl PackageList { | 18 | impl PackageList { |
21 | pub fn install(&self) -> Result<()> { | 19 | pub fn install(&self) -> Result<()> { |
22 | self.manager.to_package_manager().install(self.packages.clone()) | 20 | self.manager |
21 | .to_package_manager() | ||
22 | .install(self.packages.clone()) | ||
23 | } | 23 | } |
24 | } | 24 | } |
25 | 25 | ||
@@ -32,19 +32,14 @@ pub struct Package { | |||
32 | 32 | ||
33 | #[derive(Debug, Clone, clap::ValueEnum, Serialize, Deserialize)] | 33 | #[derive(Debug, Clone, clap::ValueEnum, Serialize, Deserialize)] |
34 | pub enum Manager { | 34 | pub enum Manager { |
35 | #[cfg(feature = "pacman")] | ||
36 | Pacman, | 35 | Pacman, |
37 | #[cfg(feature = "portage")] | ||
38 | Portage, | 36 | Portage, |
39 | } | 37 | } |
40 | 38 | ||
41 | |||
42 | impl Manager { | 39 | impl Manager { |
43 | pub fn get_manager(manager: Option<Manager>) -> Result<Box<dyn PackageManager>> { | 40 | pub fn get_manager(manager: Option<Manager>) -> Result<Box<dyn PackageManager>> { |
44 | #[cfg(not(target_os = "linux"))] | 41 | #[cfg(not(target_os = "linux"))] |
45 | { | 42 | return Err(Error::Unsupported); |
46 | return Err(Error::Unsupported); | ||
47 | } | ||
48 | 43 | ||
49 | #[cfg(target_os = "linux")] | 44 | #[cfg(target_os = "linux")] |
50 | { | 45 | { |
@@ -70,9 +65,7 @@ impl Manager { | |||
70 | 65 | ||
71 | fn from_str(value: &str) -> Result<Box<dyn PackageManager>> { | 66 | fn from_str(value: &str) -> Result<Box<dyn PackageManager>> { |
72 | Ok(match value { | 67 | Ok(match value { |
73 | #[cfg(feature = "pacman")] | ||
74 | "arch" => Box::new(Pacman), | 68 | "arch" => Box::new(Pacman), |
75 | #[cfg(feature = "portage")] | ||
76 | "gentoo" => Box::new(Portage), | 69 | "gentoo" => Box::new(Portage), |
77 | _ => return Err(Error::Unsupported), | 70 | _ => return Err(Error::Unsupported), |
78 | }) | 71 | }) |
@@ -80,15 +73,12 @@ impl Manager { | |||
80 | 73 | ||
81 | fn to_package_manager(&self) -> Box<dyn PackageManager> { | 74 | fn to_package_manager(&self) -> Box<dyn PackageManager> { |
82 | match self { | 75 | match self { |
83 | #[cfg(feature = "pacman")] | ||
84 | Self::Pacman => Box::new(Pacman), | 76 | Self::Pacman => Box::new(Pacman), |
85 | #[cfg(feature = "portage")] | ||
86 | Self::Portage => Box::new(Portage), | 77 | Self::Portage => Box::new(Portage), |
87 | } | 78 | } |
88 | } | 79 | } |
89 | } | 80 | } |
90 | 81 | ||
91 | |||
92 | pub trait PackageManager { | 82 | pub trait PackageManager { |
93 | fn get_installed(&self) -> Result<PackageList>; | 83 | fn get_installed(&self) -> Result<PackageList>; |
94 | 84 | ||
diff --git a/src/packages/portage.rs b/src/packages/portage.rs index 7fa09a8..dac0c2f 100644 --- a/src/packages/portage.rs +++ b/src/packages/portage.rs | |||
@@ -1,16 +1,63 @@ | |||
1 | use tracing::error; | 1 | use std::process::{Command, Stdio}; |
2 | 2 | ||
3 | use super::{PackageList, PackageManager}; | 3 | use crate::error::Error; |
4 | |||
5 | use super::{Package, PackageList, PackageManager}; | ||
4 | 6 | ||
5 | pub struct Portage; | 7 | pub struct Portage; |
6 | 8 | ||
7 | impl PackageManager for Portage { | 9 | impl PackageManager for Portage { |
8 | fn get_installed(&self) -> crate::error::Result<PackageList> { | 10 | fn get_installed(&self) -> crate::error::Result<PackageList> { |
9 | todo!() | 11 | let eix_pkgs = Command::new("eix") |
12 | .args(["--world", "-c*"]) | ||
13 | .output() | ||
14 | .unwrap(); | ||
15 | |||
16 | let eix_pkgs_out = String::from_utf8(eix_pkgs.stdout).unwrap(); | ||
17 | |||
18 | let mut pkgs: Vec<Package> = Vec::new(); | ||
19 | let portage_pkgs: Vec<&str> = eix_pkgs_out.split('\n').collect(); | ||
20 | for pkg in portage_pkgs { | ||
21 | if pkg.is_empty() { | ||
22 | continue; | ||
23 | }; | ||
24 | let split = pkg.split_once('@'); | ||
25 | let info = if let Some(sp) = split { | ||
26 | sp.0 | ||
27 | } else { | ||
28 | return Err(Error::UnknownOutput); | ||
29 | }; | ||
30 | |||
31 | let (id, version) = if let Some(res) = info.split_once('(') { | ||
32 | (res.0.split_at(4).1.trim().to_string(), res.1.to_string()) | ||
33 | } else { | ||
34 | return Err(Error::UnknownOutput); | ||
35 | }; | ||
36 | |||
37 | pkgs.push(Package { | ||
38 | id, | ||
39 | version, | ||
40 | explicit: true, | ||
41 | }); | ||
42 | } | ||
43 | |||
44 | Ok(PackageList { | ||
45 | packages: pkgs, | ||
46 | manager: super::Manager::Portage, | ||
47 | }) | ||
10 | } | 48 | } |
11 | 49 | ||
12 | fn install(&self, pkgs: Vec<super::Package>) -> crate::error::Result<()> { | 50 | fn install(&self, pkgs: Vec<super::Package>) -> crate::error::Result<()> { |
13 | error!("Install {pkgs:?}"); | 51 | let mut args = vec!["emerge".to_string()]; |
14 | todo!() | 52 | |
53 | for pkg in pkgs { | ||
54 | args.push(pkg.id); | ||
55 | } | ||
56 | Command::new("doas") | ||
57 | .stdout(Stdio::inherit()) | ||
58 | .args(args) | ||
59 | .spawn()? | ||
60 | .wait_with_output()?; | ||
61 | Ok(()) | ||
15 | } | 62 | } |
16 | } | 63 | } |