Skip to main content

dotfiles_actions/apt/
action.rs

1// Copyright (c) 2021-2026 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::error::DotfilesError;
28use dotfiles_core::settings::{initialize_settings_object, Setting, Settings};
29use dotfiles_core::yaml_util;
30use dotfiles_core::{action::SKIP_IN_CI_SETTING, Action};
31use getset::Getters;
32use std::path::Path;
33use strict_yaml_rust::StrictYaml;
34use subprocess::Exec;
35
36struct AptCommand {
37  items: Vec<String>,
38  args: Vec<String>,
39}
40
41impl InstallCommand<String> for AptCommand {
42  fn base_command(&self) -> Exec {
43    Exec::cmd("sudo")
44  }
45
46  fn args(&self) -> &Vec<String> {
47    &self.args
48  }
49
50  fn action_description(&self) -> &str {
51    "apt installing"
52  }
53
54  fn items(&self) -> &Vec<String> {
55    &self.items
56  }
57
58  fn action_name(&self) -> &str {
59    "apt install"
60  }
61}
62
63impl AptCommand {
64  fn install(items: &Vec<String>) -> AptCommand {
65    let mut arg_items = items.clone();
66    let mut args: Vec<String> = vec!["apt".into(), "install".into(), "-y".into()];
67    args.append(&mut arg_items);
68
69    AptCommand {
70      items: items.clone(),
71      args: args,
72    }
73  }
74}
75
76/// The string that identifies the list of packages to install
77pub const PACKAGE_SETTING: &str = "package";
78
79/// Default settings for AptAction.
80pub fn default_settings() -> Settings {
81  initialize_settings_object(&[(SKIP_IN_CI_SETTING.to_owned(), Setting::Boolean(false))])
82}
83
84/// [AptAction] Installs software using apt.
85#[derive(Eq, PartialEq, Debug, Getters)]
86pub struct AptAction {
87  /// Skips this action if it is running in a CI environment.
88  #[get = "pub"]
89  skip_in_ci: bool,
90  /// List of packages to install.
91  #[get = "pub"]
92  packages: Vec<String>,
93}
94
95impl AptAction {
96  /// Constructs a new [AptAction]
97  pub fn new(skip_in_ci: bool, packages: Vec<String>) -> Self {
98    let action = AptAction {
99      skip_in_ci,
100      packages,
101    };
102    log::trace!("Creating new {:?}", action);
103    action
104  }
105}
106
107impl Action for AptAction {
108  fn execute(&self) -> Result<(), DotfilesError> {
109    if !self.packages.is_empty() {
110      AptCommand::install(&self.packages).execute()?;
111    }
112    Ok(())
113  }
114
115  fn skip_in_ci(&self) -> bool {
116    self.skip_in_ci
117  }
118}
119
120/// Static parsing function to build a list of AptActions from YAML and settings context
121pub fn parse_action_list(
122  settings: &Settings,
123  yaml: &StrictYaml,
124  _current_dir: &Path,
125) -> Result<Vec<AptAction>, DotfilesError> {
126  let defaults = default_settings();
127  let skip_in_ci = yaml_util::get_boolean_setting_from_yaml_or_context(
128    SKIP_IN_CI_SETTING,
129    yaml,
130    settings,
131    &defaults,
132  )?;
133  let packages = yaml_util::get_optional_string_array_from_yaml_hash(PACKAGE_SETTING, yaml)?;
134  Ok(vec![AptAction::new(skip_in_ci, packages)])
135}