dotfiles_actions/apt/
action.rs1#![cfg(unix)]
26use crate::install_command::InstallCommand;
27use dotfiles_core::action::Action;
28use dotfiles_core::error::DotfilesError;
29use dotfiles_core_macros::ConditionalAction;
30use getset::Getters;
31use std::marker::PhantomData;
32use subprocess::Exec;
33
34struct AptCommand {
35 items: Vec<String>,
36 args: Vec<String>,
37}
38
39impl InstallCommand<String> for AptCommand {
40 fn base_command(&self) -> Exec {
41 Exec::cmd("sudo")
42 }
43
44 fn args(&self) -> &Vec<String> {
45 &self.args
46 }
47
48 fn action_description(&self) -> &str {
49 "apt installing"
50 }
51
52 fn items(&self) -> &Vec<String> {
53 &self.items
54 }
55
56 fn action_name(&self) -> &str {
57 "apt install"
58 }
59}
60
61impl AptCommand {
62 fn install(items: &Vec<String>) -> AptCommand {
63 let mut arg_items = items.clone();
64 let mut args: Vec<String> = vec!["apt".into(), "install".into(), "-y".into()];
65 args.append(&mut arg_items);
66
67 AptCommand {
68 items: items.clone(),
69 args: args,
70 }
71 }
72}
73
74#[derive(Eq, PartialEq, Debug, ConditionalAction, Getters)]
76pub struct AptAction<'a> {
77 #[get = "pub"]
79 skip_in_ci: bool,
80 #[get = "pub"]
82 packages: Vec<String>,
83
84 phantom_data: PhantomData<&'a String>,
85}
86impl<'a> AptAction<'a> {
87 pub fn new(skip_in_ci: bool, packages: Vec<String>) -> Self {
89 let action = AptAction {
90 skip_in_ci,
91 packages,
92 phantom_data: PhantomData,
93 };
94 log::trace!("Creating new {:?}", action);
95 action
96 }
97}
98
99impl Action<'_> for AptAction<'_> {
100 fn execute(&self) -> Result<(), DotfilesError> {
101 if !self.packages.is_empty() {
102 AptCommand::install(&self.packages).execute()?;
103 }
104 Ok(())
105 }
106}