Files
topgrade/src/main.rs

295 lines
8.5 KiB
Rust
Raw Normal View History

2020-12-01 08:59:59 +02:00
#![allow(clippy::cognitive_complexity)]
2021-12-18 20:39:57 +02:00
use std::env;
use std::io;
use std::path::PathBuf;
2021-12-18 20:39:57 +02:00
use std::process::exit;
v10.2.5 release (#330) * Don't show desktop notification on error (if `skip_notify = true`) (#275) * Use ─ (U+2500) to draw borders (#282) * Adds Pclinuxos support (#283) * Add Devkitpro Pacman support (#291) * Added support for Neovim package manager lazy.nvim (#293) * Added support for lazy.nvim From https://github.com/folke/lazy.nvim Authored-by: Jacob Lane Ledbetter <jledbetter460@gmail.com> * Make garuda-update update AUR packages by default (#296) * fix(#298): Don't throw error if no Helm repository found (#305) * Skip .NET when `dotnet tool list` is not successful (#302) * feat(pacstall): add `-y` flag variant (#312) * Add openSUSE MicroOS support (#315) * Adds notify-send timeout of 10s (#318) * Don't run yum when rpm-ostree is available (#313) * don't run yum when rpm-ostree is available * Clippy fix * rpm-ostree: set default value to true * Fixes if loop error * Fixes gem update --system requires sudo now (#317) * Fixes gem update --system requires sudo now * rubygem: Adds arg -EH to sudo * Use fixed nala path instead of which(nala) (#314) * Adds notify-send bug warning when topgrade is run (#324) * Adds notify-send bug warning when topgrade is run * fix typo + clippy * notify-send warning respects skip_notify flag * nix: Adds additional arguments support (#325) * Adds pip-review and pipupgrade support (#316) * Adds pip-review and pipupgrade support * Python: fixes pip_review and pipupgrade * v10.2.5 patch (#329) * WSL: Adds new wsl --update flags (#327) * wsl: Updates available flags * Clippy fix * Add WslUpdate runner * wsl: Code Typo * wsl: Code Typos * wsl: Code Typos * wsl: Code Typo * Adds AM Package Manager (#328) * Adds AM Package Manager * Clippy fixes * Cargo fmt * Moves am to linux only in main file --------- Co-authored-by: Guilherme Silva <626206+guihkx@users.noreply.github.com> Co-authored-by: Gabriel Augendre <gabriel@augendre.info> Co-authored-by: Cat Core <34719527+arthurbambou@users.noreply.github.com> Co-authored-by: Hugo Haas <hugoh@hugoh.net> Co-authored-by: Baptiste <32563450+BapRx@users.noreply.github.com> Co-authored-by: bbx0 <39773919+bbx0@users.noreply.github.com> Co-authored-by: Sourajyoti Basak <wiz28@protonmail.com>
2023-01-29 19:19:27 +00:00
use std::time::Duration;
2021-12-18 20:39:57 +02:00
use crate::breaking_changes::{first_run_of_major_release, print_breaking_changes, should_skip, write_keep_file};
use clap::CommandFactory;
use clap::{crate_version, Parser};
2022-11-11 09:39:29 -05:00
use color_eyre::eyre::Context;
use color_eyre::eyre::Result;
2021-12-18 20:39:57 +02:00
use console::Key;
use etcetera::base_strategy::BaseStrategy;
#[cfg(windows)]
use etcetera::base_strategy::Windows;
#[cfg(unix)]
use etcetera::base_strategy::Xdg;
use once_cell::sync::Lazy;
use rust_i18n::{i18n, t};
2022-11-16 13:43:57 -05:00
use tracing::debug;
2021-12-18 20:39:57 +02:00
use self::config::{CommandLineArgs, Config};
2021-12-18 20:39:57 +02:00
use self::error::StepFailed;
#[cfg(all(windows, feature = "self-update"))]
use self::error::Upgraded;
#[allow(clippy::wildcard_imports)]
2021-12-18 20:39:57 +02:00
use self::steps::{remote::*, *};
#[allow(clippy::wildcard_imports)]
2021-12-18 20:39:57 +02:00
use self::terminal::*;
use self::utils::{install_color_eyre, install_tracing, update_tracing};
mod breaking_changes;
mod command;
2018-06-28 12:16:54 +03:00
mod config;
mod ctrlc;
2018-12-11 16:43:26 +02:00
mod error;
2020-02-08 22:13:56 +02:00
mod execution_context;
2018-08-26 16:12:59 +03:00
mod executor;
2018-06-03 18:04:58 +03:00
mod report;
2020-02-09 13:41:55 +02:00
mod runner;
#[cfg(windows)]
mod self_renamer;
2018-11-26 14:27:19 +02:00
#[cfg(feature = "self-update")]
mod self_update;
mod step;
2018-12-15 21:52:21 +02:00
mod steps;
mod sudo;
2018-05-31 16:00:01 +03:00
mod terminal;
2018-06-17 11:43:25 +03:00
mod utils;
2018-05-30 07:53:19 +03:00
pub(crate) static HOME_DIR: Lazy<PathBuf> = Lazy::new(|| home::home_dir().expect("No home directory"));
#[cfg(unix)]
pub(crate) static XDG_DIRS: Lazy<Xdg> = Lazy::new(|| Xdg::new().expect("No home directory"));
#[cfg(windows)]
pub(crate) static WINDOWS_DIRS: Lazy<Windows> = Lazy::new(|| Windows::new().expect("No home directory"));
// Init and load the i18n files
i18n!("locales", fallback = "en");
#[allow(clippy::too_many_lines)]
fn run() -> Result<()> {
install_color_eyre()?;
ctrlc::set_handler();
let opt = CommandLineArgs::parse();
// Set up the logger with the filter directives from:
// 1. CLI option `--log-filter`
// 2. `debug` if the `--verbose` option is present
// We do this because we need our logger to work while loading the
// configuration file.
//
// When the configuration file is loaded, update the logger with the full
// filter directives.
//
// For more info, see the comments in `CommandLineArgs::tracing_filter_directives()`
// and `Config::tracing_filter_directives()`.
let reload_handle = install_tracing(&opt.tracing_filter_directives())?;
2022-05-07 08:11:53 +03:00
// Get current system locale and set it as the default locale
let system_locale = sys_locale::get_locale().unwrap_or("en".to_string());
rust_i18n::set_locale(&system_locale);
debug!("Current system locale is {system_locale}");
if let Some(shell) = opt.gen_completion {
let cmd = &mut CommandLineArgs::command();
clap_complete::generate(shell, cmd, clap::crate_name!(), &mut io::stdout());
return Ok(());
}
if opt.gen_manpage {
let man = clap_mangen::Man::new(CommandLineArgs::command());
man.render(&mut io::stdout())?;
return Ok(());
}
2022-05-07 08:11:53 +03:00
for env in opt.env_variables() {
let mut splitted = env.split('=');
let var = splitted.next().unwrap();
let value = splitted.next().unwrap();
env::set_var(var, value);
}
if opt.edit_config() {
Config::edit()?;
return Ok(());
};
if opt.show_config_reference() {
print!("{}", config::EXAMPLE_CONFIG);
return Ok(());
}
let config = Config::load(opt)?;
// Update the logger with the full filter directives.
update_tracing(&reload_handle, &config.tracing_filter_directives())?;
set_title(config.set_title());
display_time(config.display_time());
set_desktop_notifications(config.notify_each_step());
debug!("Version: {}", crate_version!());
debug!("OS: {}", env!("TARGET"));
debug!("{:?}", env::args());
debug!("Binary path: {:?}", std::env::current_exe());
debug!("self-update Feature Enabled: {:?}", cfg!(feature = "self-update"));
debug!("Configuration: {:?}", config);
if config.run_in_tmux() && env::var("TOPGRADE_INSIDE_TMUX").is_err() {
2018-06-27 23:04:39 +03:00
#[cfg(unix)]
{
tmux::run_in_tmux(config.tmux_config()?)?;
return Ok(());
2018-06-20 21:05:49 +03:00
}
}
#[cfg(target_os = "linux")]
let distribution = linux::Distribution::detect();
2018-11-07 14:31:44 +02:00
let sudo = config.sudo_command().map_or_else(sudo::Sudo::detect, sudo::Sudo::new);
let run_type = execution_context::RunType::new(config.dry_run());
let ctx = execution_context::ExecutionContext::new(
run_type,
sudo,
&config,
#[cfg(target_os = "linux")]
&distribution,
);
2020-02-09 13:41:55 +02:00
let mut runner = runner::Runner::new(&ctx);
// If
//
// 1. the breaking changes notification shouldnot be skipped
// 2. this is the first execution of a major release
//
// inform user of breaking changes
if !should_skip() && first_run_of_major_release()? {
print_breaking_changes();
if prompt_yesno("Confirmed?")? {
write_keep_file()?;
} else {
exit(1);
}
}
// Self-Update step, this will execute only if:
// 1. the `self-update` feature is enabled
// 2. it is not disabled from configuration (env var/CLI opt/file)
#[cfg(feature = "self-update")]
{
let should_self_update = env::var("TOPGRADE_NO_SELF_UPGRADE").is_err() && !config.no_self_update();
if should_self_update {
runner.execute(step::Step::SelfUpdate, "Self Update", || self_update::self_update(&ctx))?;
}
}
2018-11-07 14:31:44 +02:00
#[cfg(windows)]
let _self_rename = if config.self_rename() {
Some(crate::self_renamer::SelfRenamer::create()?)
} else {
None
};
2018-06-20 20:26:08 +03:00
if let Some(commands) = config.pre_commands() {
for (name, command) in commands {
2021-09-02 06:18:01 +03:00
generic::run_custom_command(name, command, &ctx)?;
2018-06-20 20:26:08 +03:00
}
}
if config.pre_sudo() {
if let Some(sudo) = ctx.sudo() {
sudo.elevate(&ctx)?;
}
}
for step in step::default_steps() {
step.run(&mut runner, &ctx)?
2020-06-10 11:51:52 +03:00
}
2020-02-09 13:41:55 +02:00
if !runner.report().data().is_empty() {
print_separator(t!("Summary"));
2018-06-03 18:04:58 +03:00
for (key, result) in runner.report().data() {
2020-08-21 23:04:36 +03:00
print_result(key, result);
2018-06-03 18:04:58 +03:00
}
#[cfg(target_os = "linux")]
{
if let Ok(distribution) = &distribution {
distribution.show_summary();
}
}
2018-05-29 23:48:30 +03:00
}
2020-08-30 07:40:06 +03:00
let mut post_command_failed = false;
if let Some(commands) = config.post_commands() {
for (name, command) in commands {
2021-09-02 06:18:01 +03:00
if generic::run_custom_command(name, command, &ctx).is_err() {
2020-08-30 07:40:06 +03:00
post_command_failed = true;
}
}
}
2019-06-16 09:09:05 +03:00
if config.keep_at_end() {
print_info(t!("\n(R)eboot\n(S)hell\n(Q)uit"));
2019-08-04 09:25:35 +03:00
loop {
match get_key() {
Ok(Key::Char('s' | 'S')) => {
run_shell().context("Failed to execute shell")?;
2021-10-28 22:05:35 +03:00
}
Ok(Key::Char('r' | 'R')) => {
reboot().context("Failed to reboot")?;
2021-10-28 22:05:35 +03:00
}
Ok(Key::Char('q' | 'Q')) => (),
2021-10-28 22:05:35 +03:00
_ => {
continue;
}
2019-06-13 22:05:18 +03:00
}
2019-08-04 09:25:35 +03:00
break;
2019-06-13 22:05:18 +03:00
}
}
let failed = post_command_failed || runner.report().data().iter().any(|(_, result)| result.failed());
if !config.skip_notify() {
notify_desktop(
if failed {
t!("Topgrade finished with errors")
} else {
t!("Topgrade finished successfully")
},
v10.2.5 release (#330) * Don't show desktop notification on error (if `skip_notify = true`) (#275) * Use ─ (U+2500) to draw borders (#282) * Adds Pclinuxos support (#283) * Add Devkitpro Pacman support (#291) * Added support for Neovim package manager lazy.nvim (#293) * Added support for lazy.nvim From https://github.com/folke/lazy.nvim Authored-by: Jacob Lane Ledbetter <jledbetter460@gmail.com> * Make garuda-update update AUR packages by default (#296) * fix(#298): Don't throw error if no Helm repository found (#305) * Skip .NET when `dotnet tool list` is not successful (#302) * feat(pacstall): add `-y` flag variant (#312) * Add openSUSE MicroOS support (#315) * Adds notify-send timeout of 10s (#318) * Don't run yum when rpm-ostree is available (#313) * don't run yum when rpm-ostree is available * Clippy fix * rpm-ostree: set default value to true * Fixes if loop error * Fixes gem update --system requires sudo now (#317) * Fixes gem update --system requires sudo now * rubygem: Adds arg -EH to sudo * Use fixed nala path instead of which(nala) (#314) * Adds notify-send bug warning when topgrade is run (#324) * Adds notify-send bug warning when topgrade is run * fix typo + clippy * notify-send warning respects skip_notify flag * nix: Adds additional arguments support (#325) * Adds pip-review and pipupgrade support (#316) * Adds pip-review and pipupgrade support * Python: fixes pip_review and pipupgrade * v10.2.5 patch (#329) * WSL: Adds new wsl --update flags (#327) * wsl: Updates available flags * Clippy fix * Add WslUpdate runner * wsl: Code Typo * wsl: Code Typos * wsl: Code Typos * wsl: Code Typo * Adds AM Package Manager (#328) * Adds AM Package Manager * Clippy fixes * Cargo fmt * Moves am to linux only in main file --------- Co-authored-by: Guilherme Silva <626206+guihkx@users.noreply.github.com> Co-authored-by: Gabriel Augendre <gabriel@augendre.info> Co-authored-by: Cat Core <34719527+arthurbambou@users.noreply.github.com> Co-authored-by: Hugo Haas <hugoh@hugoh.net> Co-authored-by: Baptiste <32563450+BapRx@users.noreply.github.com> Co-authored-by: bbx0 <39773919+bbx0@users.noreply.github.com> Co-authored-by: Sourajyoti Basak <wiz28@protonmail.com>
2023-01-29 19:19:27 +00:00
Some(Duration::from_secs(10)),
);
}
if failed {
Err(StepFailed.into())
} else {
Ok(())
}
}
fn main() {
match run() {
Ok(()) => {
exit(0);
}
Err(error) => {
#[cfg(all(windows, feature = "self-update"))]
{
if let Some(Upgraded(status)) = error.downcast_ref::<Upgraded>() {
exit(status.code().unwrap());
}
}
let skip_print = (error.downcast_ref::<StepFailed>().is_some())
|| (error
.downcast_ref::<io::Error>()
2018-12-11 16:43:26 +02:00
.filter(|io_error| io_error.kind() == io::ErrorKind::Interrupted)
.is_some());
2018-12-11 16:43:26 +02:00
if !skip_print {
2022-11-11 09:39:29 -05:00
// The `Debug` implementation of `eyre::Result` prints a multi-line
// error message that includes all the 'causes' added with
// `.with_context(...)` calls.
println!("{}", t!("Error: {error}", error = format!("{:?}", error)));
}
exit(1);
}
}
2018-05-29 23:48:30 +03:00
}