Files
topgrade/src/steps/os/unix.rs

171 lines
4.9 KiB
Rust
Raw Normal View History

2019-11-12 15:38:50 +02:00
#[cfg(target_os = "linux")]
use crate::error::SkipStep;
use crate::error::TopgradeError;
2020-02-08 22:17:29 +02:00
use crate::execution_context::ExecutionContext;
use crate::executor::{ExecutorExitStatus, RunType};
2020-02-08 22:17:29 +02:00
use crate::terminal::{print_separator, print_warning};
2019-02-27 10:31:30 +02:00
use crate::utils::{require, PathExt};
use anyhow::Result;
2018-07-07 09:18:53 +03:00
use directories::BaseDirs;
2020-02-08 22:17:29 +02:00
use log::debug;
2019-02-26 02:40:51 -08:00
use std::env;
2020-02-08 22:17:29 +02:00
use std::fs;
use std::os::unix::fs::MetadataExt;
use std::path::PathBuf;
use std::process::Command;
pub fn run_fisher(base_dirs: &BaseDirs, run_type: RunType) -> Result<()> {
2019-02-27 10:31:30 +02:00
let fish = require("fish")?;
base_dirs
.home_dir()
.join(".config/fish/functions/fisher.fish")
.require()?;
2019-04-16 08:37:56 +03:00
print_separator("Fisher");
2019-02-27 10:31:30 +02:00
run_type
.execute(&fish)
.args(&["-c", "fisher self-update"])
.check_run()?;
2019-04-06 09:46:50 +03:00
2019-02-27 10:31:30 +02:00
run_type.execute(&fish).args(&["-c", "fisher"]).check_run()
2018-06-28 12:16:54 +03:00
}
2020-07-02 08:37:18 +03:00
pub fn run_brew(ctx: &ExecutionContext) -> Result<()> {
2019-02-27 10:31:30 +02:00
let brew = require("brew")?;
print_separator("Brew");
let run_type = ctx.run_type();
2019-02-27 10:31:30 +02:00
run_type.execute(&brew).arg("update").check_run()?;
run_type
.execute(&brew)
.args(&["upgrade", "--ignore-pinned"])
2020-07-02 08:37:18 +03:00
.check_run()
}
2020-02-08 22:17:29 +02:00
pub fn run_nix(ctx: &ExecutionContext) -> Result<()> {
2019-02-27 10:31:30 +02:00
let nix = require("nix")?;
let nix_channel = require("nix-channel")?;
2019-04-06 09:46:59 +03:00
let nix_env = require("nix-env")?;
2019-02-27 10:31:30 +02:00
print_separator("Nix");
2020-02-08 22:17:29 +02:00
let multi_user = fs::metadata(&nix)?.uid() == 0;
debug!("Multi user nix: {}", multi_user);
#[cfg(target_os = "linux")]
{
use super::linux::Distribution;
if let Ok(Distribution::NixOS) = Distribution::detect() {
debug!("Nix on NixOS must be upgraded via 'nixos-rebuild switch', skipping.");
return Err(SkipStep.into());
}
}
2020-02-08 22:17:29 +02:00
let run_type = ctx.run_type();
if multi_user {
if let Some(sudo) = ctx.sudo() {
run_type.execute(&sudo).arg("nix").arg("upgrade-nix").check_run()?;
} else {
print_warning("Need sudo to upgrade Nix");
}
} else {
run_type.execute(&nix).arg("upgrade-nix").check_run()?;
}
run_type.execute(&nix_channel).arg("--update").check_run()?;
2019-02-27 10:31:30 +02:00
run_type.execute(&nix_env).arg("--upgrade").check_run()
2018-10-21 13:05:49 +03:00
}
2019-02-19 08:47:01 +02:00
2020-07-11 08:00:35 +03:00
pub fn run_yadm(ctx: &ExecutionContext) -> Result<()> {
let yadm = require("yadm")?;
print_separator("yadm");
ctx.run_type().execute(&yadm).arg("pull").check_run()
}
2020-02-29 09:07:03 +02:00
pub fn run_asdf(run_type: RunType) -> Result<()> {
let asdf = require("asdf")?;
print_separator("asdf");
let exit_status = run_type.execute(&asdf).arg("update").spawn()?.wait()?;
if let ExecutorExitStatus::Wet(e) = exit_status {
if !(e.success() || e.code().map(|c| c == 42).unwrap_or(false)) {
return Err(TopgradeError::ProcessFailed(e).into());
}
}
2020-02-29 09:07:03 +02:00
run_type.execute(&asdf).args(&["plugin", "update", "--all"]).check_run()
}
pub fn run_home_manager(run_type: RunType) -> Result<()> {
let home_manager = require("home-manager")?;
print_separator("home-manager");
run_type.execute(&home_manager).arg("switch").check_run()
}
2020-01-05 22:56:18 +02:00
pub fn run_tldr(run_type: RunType) -> Result<()> {
let tldr = require("tldr")?;
print_separator("TLDR");
run_type.execute(&tldr).arg("--update").check_run()
2020-01-05 22:56:18 +02:00
}
pub fn run_pearl(run_type: RunType) -> Result<()> {
2019-02-19 08:47:01 +02:00
let pearl = require("pearl")?;
print_separator("pearl");
run_type.execute(&pearl).arg("update").check_run()
}
pub fn run_sdkman(base_dirs: &BaseDirs, cleanup: bool, run_type: RunType) -> Result<()> {
let bash = require("bash")?;
let sdkman_init_path = env::var("SDKMAN_DIR")
.map(PathBuf::from)
.unwrap_or_else(|_| base_dirs.home_dir().join(".sdkman"))
.join("bin")
.join("sdkman-init.sh")
.require()
.map(|p| format!("{}", &p.display()))?;
print_separator("SDKMAN!");
let cmd_selfupdate = format!("source {} && sdk selfupdate", &sdkman_init_path);
run_type
.execute(&bash)
.args(&["-c", cmd_selfupdate.as_str()])
.check_run()?;
let cmd_update = format!("source {} && sdk update", &sdkman_init_path);
run_type.execute(&bash).args(&["-c", cmd_update.as_str()]).check_run()?;
let cmd_upgrade = format!("source {} && sdk upgrade", &sdkman_init_path);
run_type
.execute(&bash)
.args(&["-c", cmd_upgrade.as_str()])
.check_run()?;
if cleanup {
let cmd_flush_archives = format!("source {} && sdk flush archives", &sdkman_init_path);
run_type
.execute(&bash)
.args(&["-c", cmd_flush_archives.as_str()])
.check_run()?;
let cmd_flush_temp = format!("source {} && sdk flush temp", &sdkman_init_path);
run_type
.execute(&bash)
.args(&["-c", cmd_flush_temp.as_str()])
.check_run()?;
}
Ok(())
}
2019-06-13 22:05:18 +03:00
pub fn reboot() {
2019-06-16 09:09:05 +03:00
print!("Rebooting...");
Command::new("sudo").arg("reboot").spawn().unwrap().wait().unwrap();
2019-06-13 22:05:18 +03:00
}