pacsea/install/
command.rs1use crate::state::{PackageItem, Source};
4
5use super::utils::shell_single_quote;
6
7const HOLD_TAIL: &str = "; echo; echo 'Finished.'; echo 'Press any key to close...'; read -rn1 -s _ || (echo; echo 'Press Ctrl+C to close'; sleep infinity)";
9
10pub fn build_install_command(
29 item: &PackageItem,
30 password: Option<&str>,
31 dry_run: bool,
32) -> Result<(String, bool), String> {
33 let reinstall = crate::index::is_installed(&item.name);
34 let neutral = crate::integrations::arch_toolkit::install::single_install(item, reinstall)?;
35 match item.source {
36 Source::Official { .. } => {
37 let tool = crate::logic::privilege::active_tool()?;
38 let privileged = password.filter(|value| !value.is_empty()).map_or_else(
39 || crate::logic::privilege::build_privilege_command(tool, &neutral),
40 |value| {
41 crate::logic::privilege::build_password_pipe(tool, value, &neutral)
42 .unwrap_or_else(|| {
43 crate::logic::privilege::build_privilege_command(tool, &neutral)
44 })
45 },
46 );
47 Ok((render_terminal_command(&privileged, dry_run), true))
48 }
49 Source::Aur => Ok((render_terminal_command(&neutral, dry_run), false)),
50 }
51}
52
53fn render_terminal_command(command: &str, dry_run: bool) -> String {
65 let with_hold = format!("{command}{HOLD_TAIL}");
66 if dry_run {
67 format!("echo DRY RUN: {}", shell_single_quote(&with_hold))
68 } else {
69 with_hold
70 }
71}
72
73#[cfg(test)]
74mod tests {
75 use super::build_install_command;
76 use crate::state::{PackageItem, Source};
77
78 #[test]
89 fn official_install_uses_toolkit_plan() {
90 let tool = crate::logic::privilege::active_tool().expect("privilege tool should exist");
91 let item = package(Source::Official {
92 repo: "extra".to_string(),
93 arch: "x86_64".to_string(),
94 });
95 let (command, privileged) =
96 build_install_command(&item, None, false).expect("plan should build");
97 assert!(privileged);
98 assert!(command.contains(&format!(
99 "{} pacman -S --needed --noconfirm -- ripgrep",
100 tool.binary_name()
101 )));
102 assert!(command.contains("Press any key to close"));
103
104 let (dry_run, _) =
105 build_install_command(&item, None, true).expect("dry-run plan should build");
106 assert!(dry_run.starts_with("echo DRY RUN: '"));
107 assert!(dry_run.contains("pacman -S --needed --noconfirm -- ripgrep"));
108 }
109
110 #[test]
121 fn aur_install_uses_unprivileged_toolkit_fallback() {
122 let item = package(Source::Aur);
123 let (command, privileged) =
124 build_install_command(&item, None, false).expect("plan should build");
125 assert!(!privileged);
126 assert!(command.contains("paru -S --aur --needed --noconfirm -- ripgrep"));
127 assert!(command.contains("yay -S --aur --needed --noconfirm -- ripgrep"));
128 assert!(command.contains("exit 127"));
129 }
130
131 fn package(source: Source) -> PackageItem {
142 PackageItem {
143 name: "ripgrep".to_string(),
144 version: String::new(),
145 description: String::new(),
146 source,
147 popularity: None,
148 out_of_date: None,
149 orphaned: false,
150 }
151 }
152}