dotfiles_actions/apt/
action.rs1#![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
76pub const PACKAGE_SETTING: &str = "package";
78
79pub fn default_settings() -> Settings {
81 initialize_settings_object(&[(SKIP_IN_CI_SETTING.to_owned(), Setting::Boolean(false))])
82}
83
84#[derive(Eq, PartialEq, Debug, Getters)]
86pub struct AptAction {
87 #[get = "pub"]
89 skip_in_ci: bool,
90 #[get = "pub"]
92 packages: Vec<String>,
93}
94
95impl AptAction {
96 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
120pub 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}