Use #[cfg] instead of if cfg!

This commit is contained in:
Roey Darwish Dror
2018-06-27 23:04:39 +03:00
parent 33a2b5d84c
commit c667e224a6
3 changed files with 27 additions and 14 deletions

View File

@@ -18,6 +18,7 @@ extern crate termcolor;
mod config; mod config;
mod git; mod git;
#[cfg(target_os = "linux")]
mod linux; mod linux;
mod npm; mod npm;
mod report; mod report;
@@ -35,8 +36,10 @@ use std::env;
use std::env::home_dir; use std::env::home_dir;
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::process::CommandExt; use std::os::unix::process::CommandExt;
#[cfg(unix)]
use std::path::PathBuf; use std::path::PathBuf;
use std::process::exit; use std::process::exit;
#[cfg(unix)]
use std::process::Command; use std::process::Command;
use steps::*; use steps::*;
use terminal::Terminal; use terminal::Terminal;
@@ -70,7 +73,8 @@ fn run() -> Result<(), Error> {
.get_matches(); .get_matches();
if matches.is_present("tmux") && !env::var("TMUX").is_ok() { if matches.is_present("tmux") && !env::var("TMUX").is_ok() {
if cfg!(unix) { #[cfg(unix)]
{
let tmux = utils::which("tmux").expect("Could not find tmux"); let tmux = utils::which("tmux").expect("Could not find tmux");
let err = Command::new(tmux) let err = Command::new(tmux)
.args(&[ .args(&[
@@ -87,8 +91,6 @@ fn run() -> Result<(), Error> {
]) ])
.exec(); .exec();
panic!("{:?}", err); panic!("{:?}", err);
} else {
panic!("This flag is only implemented in Unix systems");
} }
} }
@@ -99,11 +101,8 @@ fn run() -> Result<(), Error> {
let config = Config::read()?; let config = Config::read()?;
let mut reports = Report::new(); let mut reports = Report::new();
let sudo = if cfg!(target_os = "linux") { #[cfg(target_os = "linux")]
utils::which("sudo") let sudo = utils::which("sudo");
} else {
None
};
if let Some(commands) = config.pre_commands() { if let Some(commands) = config.pre_commands() {
for (name, command) in commands { for (name, command) in commands {
@@ -112,7 +111,8 @@ fn run() -> Result<(), Error> {
} }
} }
if cfg!(target_os = "linux") { #[cfg(target_os = "linux")]
{
terminal.print_separator("System update"); terminal.print_separator("System update");
match linux::Distribution::detect() { match linux::Distribution::detect() {
Ok(distribution) => { Ok(distribution) => {
@@ -139,7 +139,8 @@ fn run() -> Result<(), Error> {
git_repos.insert(home_path(".emacs.d")); git_repos.insert(home_path(".emacs.d"));
if cfg!(unix) { #[cfg(unix)]
{
git_repos.insert(home_path(".zshrc")); git_repos.insert(home_path(".zshrc"));
git_repos.insert(home_path(".oh-my-zsh")); git_repos.insert(home_path(".oh-my-zsh"));
git_repos.insert(home_path(".tmux")); git_repos.insert(home_path(".tmux"));
@@ -159,7 +160,8 @@ fn run() -> Result<(), Error> {
} }
} }
if cfg!(unix) { #[cfg(unix)]
{
if let Some(zsh) = utils::which("zsh") { if let Some(zsh) = utils::which("zsh") {
if home_path(".zplug").exists() { if home_path(".zplug").exists() {
terminal.print_separator("zplug"); terminal.print_separator("zplug");
@@ -216,7 +218,8 @@ fn run() -> Result<(), Error> {
run_apm(&apm).report("Atom Package Manager", &mut reports); run_apm(&apm).report("Atom Package Manager", &mut reports);
} }
if cfg!(target_os = "linux") { #[cfg(target_os = "linux")]
{
if let Some(flatpak) = utils::which("flatpak") { if let Some(flatpak) = utils::which("flatpak") {
terminal.print_separator("Flatpak"); terminal.print_separator("Flatpak");
run_flatpak(&flatpak).report("Flatpak", &mut reports); run_flatpak(&flatpak).report("Flatpak", &mut reports);
@@ -237,7 +240,8 @@ fn run() -> Result<(), Error> {
} }
} }
if cfg!(target_os = "linux") { #[cfg(target_os = "linux")]
{
if let Some(fwupdmgr) = utils::which("fwupdmgr") { if let Some(fwupdmgr) = utils::which("fwupdmgr") {
terminal.print_separator("Firmware upgrades"); terminal.print_separator("Firmware upgrades");
run_fwupdmgr(&fwupdmgr).report("Firmware upgrade", &mut reports); run_fwupdmgr(&fwupdmgr).report("Firmware upgrade", &mut reports);
@@ -251,7 +255,8 @@ fn run() -> Result<(), Error> {
} }
} }
if cfg!(target_os = "macos") { #[cfg(target_os = "macos")]
{
terminal.print_separator("App Store"); terminal.print_separator("App Store");
upgrade_macos().report("App Store", &mut reports);; upgrade_macos().report("App Store", &mut reports);;
} }

View File

@@ -7,6 +7,7 @@ use utils::is_ancestor;
const EMACS_UPGRADE: &str = include_str!("emacs.el"); const EMACS_UPGRADE: &str = include_str!("emacs.el");
#[cfg(unix)]
pub fn run_zplug(zsh: &PathBuf) -> Result<(), failure::Error> { pub fn run_zplug(zsh: &PathBuf) -> Result<(), failure::Error> {
Command::new(zsh) Command::new(zsh)
.args(&["-c", "source ~/.zshrc && zplug update"]) .args(&["-c", "source ~/.zshrc && zplug update"])
@@ -17,6 +18,7 @@ pub fn run_zplug(zsh: &PathBuf) -> Result<(), failure::Error> {
Ok(()) Ok(())
} }
#[cfg(unix)]
pub fn run_tpm(tpm: &PathBuf) -> Result<(), failure::Error> { pub fn run_tpm(tpm: &PathBuf) -> Result<(), failure::Error> {
Command::new(&tpm).arg("all").spawn()?.wait()?.check()?; Command::new(&tpm).arg("all").spawn()?.wait()?.check()?;
@@ -86,6 +88,7 @@ pub fn run_apm(apm: &PathBuf) -> Result<(), failure::Error> {
Ok(()) Ok(())
} }
#[cfg(target_os = "linux")]
pub fn run_needrestart(sudo: &PathBuf) -> Result<(), failure::Error> { pub fn run_needrestart(sudo: &PathBuf) -> Result<(), failure::Error> {
Command::new(&sudo) Command::new(&sudo)
.arg("needrestart") .arg("needrestart")
@@ -96,6 +99,7 @@ pub fn run_needrestart(sudo: &PathBuf) -> Result<(), failure::Error> {
Ok(()) Ok(())
} }
#[cfg(target_os = "linux")]
pub fn run_fwupdmgr(fwupdmgr: &PathBuf) -> Result<(), failure::Error> { pub fn run_fwupdmgr(fwupdmgr: &PathBuf) -> Result<(), failure::Error> {
Command::new(&fwupdmgr) Command::new(&fwupdmgr)
.arg("refresh") .arg("refresh")
@@ -126,6 +130,7 @@ pub fn run_rustup(rustup: &PathBuf) -> Result<(), failure::Error> {
Ok(()) Ok(())
} }
#[cfg(target_os = "macos")]
pub fn upgrade_macos() -> Result<(), failure::Error> { pub fn upgrade_macos() -> Result<(), failure::Error> {
Command::new("softwareupdate") Command::new("softwareupdate")
.args(&["--install", "--all"]) .args(&["--install", "--all"])
@@ -163,6 +168,7 @@ pub fn run_custom_command(command: &str) -> Result<(), failure::Error> {
Ok(()) Ok(())
} }
#[cfg(target_os = "linux")]
pub fn run_flatpak(flatpak: &PathBuf) -> Result<(), failure::Error> { pub fn run_flatpak(flatpak: &PathBuf) -> Result<(), failure::Error> {
Command::new(&flatpak) Command::new(&flatpak)
.arg("update") .arg("update")
@@ -173,6 +179,7 @@ pub fn run_flatpak(flatpak: &PathBuf) -> Result<(), failure::Error> {
Ok(()) Ok(())
} }
#[cfg(target_os = "linux")]
pub fn run_snap(sudo: &PathBuf, snap: &PathBuf) -> Result<(), failure::Error> { pub fn run_snap(sudo: &PathBuf, snap: &PathBuf) -> Result<(), failure::Error> {
Command::new(&sudo) Command::new(&sudo)
.args(&[snap.to_str().unwrap(), "refresh"]) .args(&[snap.to_str().unwrap(), "refresh"])

View File

@@ -38,6 +38,7 @@ impl Terminal {
} }
} }
#[allow(dead_code)]
pub fn print_warning<P: AsRef<str>>(&mut self, message: P) { pub fn print_warning<P: AsRef<str>>(&mut self, message: P) {
let message = message.as_ref(); let message = message.as_ref();