Skip to content

Commit

Permalink
Merge branch 'rs-dev'
Browse files Browse the repository at this point in the history
  • Loading branch information
rami3l committed May 27, 2020
2 parents a130b7e + 1deb586 commit 4b96539
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 47 deletions.
16 changes: 8 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 16 additions & 12 deletions src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ use structopt::StructOpt;

/// The command line options to be collected.
#[derive(Debug, StructOpt)]
#[structopt(
name = "pacpat-ng",
about = "A pacman-like wrapper for many package managers."
)]
#[structopt(about = "A pacman-like wrapper for many package managers.")]
pub struct Opt {
// Operations include Q(uery), R(emove), and S(ync).
#[structopt(short = "Q", long)]
Expand Down Expand Up @@ -133,8 +130,9 @@ impl Opt {
let no_confirm = self.no_confirm;
let force_cask = self.force_cask;

let unknown = Box::new(unknown::Unknown {});
let unknown = || Box::new(unknown::Unknown {});

// Windows
if cfg!(target_os = "windows") {
// Chocolatey
if is_exe("choco", "") {
Expand All @@ -143,9 +141,11 @@ impl Opt {
no_confirm,
})
} else {
unknown
unknown()
}
} else if cfg!(target_os = "macos") {
}
// macOS
else if cfg!(target_os = "macos") {
// Homebrew
if is_exe("brew", "/usr/local/bin/brew") {
Box::new(homebrew::Homebrew {
Expand All @@ -155,9 +155,11 @@ impl Opt {
no_confirm,
})
} else {
unknown
unknown()
}
} else if cfg!(target_os = "linux") {
}
// Linux
else if cfg!(target_os = "linux") {
// Apt/Dpkg for Debian/Ubuntu/Termux
if is_exe("apt-get", "/usr/bin/apt-get") {
Box::new(dpkg::Dpkg {
Expand All @@ -172,10 +174,12 @@ impl Opt {
no_confirm,
})
} else {
unknown
unknown()
}
} else {
unknown
}
// Unknown OS
else {
unknown()
}
}

Expand Down
16 changes: 12 additions & 4 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,21 +199,29 @@ pub fn cmd_str(cmd: &str, subcmd: &[&str], kws: &[&str], flags: &[&str]) -> Stri

/// Print out the command after the given prompt.
pub fn print_cmd(cmd: &str, subcmd: &[&str], kws: &[&str], flags: &[&str], prompt: &str) {
println!("{:>8} {}", prompt.green(), cmd_str(cmd, subcmd, kws, flags));
println!(
"{:>8} `{}`",
prompt.green().bold(),
cmd_str(cmd, subcmd, kws, flags)
);
}

/// Print out the command after the given prompt (dry run version).
pub fn print_dryrun(cmd: &str, subcmd: &[&str], kws: &[&str], flags: &[&str], prompt: &str) {
println!("{:>8} {}", prompt.green(), cmd_str(cmd, subcmd, kws, flags));
println!(
"{:>8} `{}`",
prompt.green().bold(),
cmd_str(cmd, subcmd, kws, flags)
);
}

/// Print out a message after the given prompt.
pub fn print_msg(msg: &str, prompt: &str) {
println!("{:>8} {}", prompt.green(), msg);
println!("{:>8} {}", prompt.green().bold(), msg);
}

pub fn print_err(err: impl std::error::Error, prompt: &str) {
eprintln!("{:>8} {}", prompt.red(), err);
eprintln!("{:>8} {}", prompt.bright_red().bold(), err);
}

/// Check if an executable exists by name (consult `$PATH`) or by path.
Expand Down
4 changes: 2 additions & 2 deletions src/packmanager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::exec::{self, Mode};
macro_rules! make_pm {
($( $(#[$meta:meta])* $method:ident ), *) => {
$($(#[$meta])*
fn $method(&self, _kws: &[&str], _flags: &[&str]) -> Result<(), Error> {
Err(format!("`{}` unimplemented", stringify!($method)).into())
fn $method(&self, _kws: &[&str], _flags: &[&str]) -> std::result::Result<(), crate::error::Error> {
std::result::Result::Err(format!("Operation `{}` unimplemented", stringify!($method)).into())
})*
};
}
Expand Down
40 changes: 19 additions & 21 deletions src/packmanager/homebrew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,9 @@ impl PackManager for Homebrew {

/// R removes a single package, leaving all of its dependencies installed.
fn r(&self, kws: &[&str], flags: &[&str]) -> Result<(), Error> {
for &pack in kws {
self.auto_cask_do(&["uninstall"], pack, flags)?;
}
Ok(())
kws.iter()
.map(|&pack| self.auto_cask_do(&["uninstall"], pack, flags))
.collect()
}

/// Rs removes a package and its dependencies which are not required by any other installed package.
Expand All @@ -183,10 +182,10 @@ impl PackManager for Homebrew {

if RMTREE_MISSING.find(&err_msg).is_some() {
print_msg(
"`rmtree` is not installed. You may try installing it with the following command:",
"`rmtree` is not installed. You may install it with the following command:",
PROMPT_INFO,
);
print_msg("brew tap beeftornado/rmtree", PROMPT_INFO);
print_msg("`brew tap beeftornado/rmtree`", PROMPT_INFO);
return Err("`rmtree` required".into());
}

Expand All @@ -195,17 +194,17 @@ impl PackManager for Homebrew {

/// S installs one or more packages by name.
fn s(&self, kws: &[&str], flags: &[&str]) -> Result<(), Error> {
for &pack in kws {
if self.needed {
self.auto_cask_do(&["install"], pack, flags)?
} else {
// If the package is not installed, `brew reinstall` behaves just like `brew install`,
// so `brew reinstall` matches perfectly the behavior of `pacman -S`.
self.auto_cask_do(&["reinstall"], pack, flags)?
}
}

Ok(())
kws.iter()
.map(|&pack| {
if self.needed {
self.auto_cask_do(&["install"], pack, flags)
} else {
// If the package is not installed, `brew reinstall` behaves just like `brew install`,
// so `brew reinstall` matches perfectly the behavior of `pacman -S`.
self.auto_cask_do(&["reinstall"], pack, flags)
}
})
.collect()
}

/// Sc removes all the cached packages that are not currently installed, and the unused sync database.
Expand Down Expand Up @@ -261,10 +260,9 @@ impl PackManager for Homebrew {
self.prompt_run("brew", &["upgrade"], kws, flags)?;
self.prompt_run("brew", &["cask", "upgrade"], kws, flags)
} else {
for &pack in kws {
self.auto_cask_do(&["upgrade"], pack, flags)?;
}
Ok(())
kws.iter()
.map(|&pack| self.auto_cask_do(&["upgrade"], pack, flags))
.collect()
}
}

Expand Down

0 comments on commit 4b96539

Please sign in to comment.