dotfiles_actions/apt/
action.rs

1// Copyright (c) 2021-2022 Miguel Barreto and others
2//
3// Permission is hereby granted, free of charge, to any person obtaining
4// a copy of this software and associated documentation files (the
5// "Software"), to deal in the Software without restriction, including
6// without limitation the rights to use, copy, modify, merge, publish,
7// distribute, sublicense, and/or sell copies of the Software, and to
8// permit persons to whom the Software is furnished to do so, subject to
9// the following conditions:
10//
11// The above copyright notice and this permission notice shall be
12// included in all copies or substantial portions of the Software.
13//
14// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21
22//! This module contains the [AptAction] that installs
23//! packages using apt on debian-based distros
24
25#![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/// [AptAction] Installs software using apt.
75#[derive(Eq, PartialEq, Debug, ConditionalAction, Getters)]
76pub struct AptAction<'a> {
77  /// Skips this action if it is running in a CI environment.
78  #[get = "pub"]
79  skip_in_ci: bool,
80  /// List of packages to install.
81  #[get = "pub"]
82  packages: Vec<String>,
83
84  phantom_data: PhantomData<&'a String>,
85}
86impl<'a> AptAction<'a> {
87  /// Constructs a new [AptAction]
88  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}