use self::UpdateSelfError as E;
use crate::cmd;
use crate::opts::UpdateSelfOptions;
use crate::tasks::ResolveEnv;
use crate::tasks::task::TaskStatus;
use camino::Utf8PathBuf;
use chrono::Utc;
use color_eyre::eyre::Context;
use color_eyre::eyre::Result;
use displaydoc::Display;
use serde_derive::Deserialize;
use std::env;
use std::fs;
use std::fs::File;
use std::fs::Permissions;
use std::io;
use std::os::unix::fs::PermissionsExt;
use thiserror::Error;
use tracing::debug;
use tracing::info;
use tracing::trace;
#[derive(Debug, Deserialize)]
struct GitHubReleaseJsonResponse {
tag_name: String,
}
const APP_USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_VERSION"),);
const CURRENT_VERSION: &str = env!("CARGO_PKG_VERSION");
impl ResolveEnv for UpdateSelfOptions {}
pub(crate) fn run(opts: &UpdateSelfOptions) -> Result<TaskStatus> {
let up_path = Utf8PathBuf::try_from(env::current_exe()?)?.canonicalize_utf8()?;
if !opts.always_update && up_path.starts_with(env!("CARGO_MANIFEST_DIR")) {
debug!("Skipping up update, current version '{up_path}' is a dev build.",);
return Ok(TaskStatus::Skipped);
}
let client = reqwest::blocking::Client::builder()
.user_agent(APP_USER_AGENT)
.build()?;
trace!("Self update opts: {opts:?}");
if opts.url == crate::opts::SELF_UPDATE_URL {
let latest_github_release = client
.get(crate::opts::LATEST_RELEASE_URL)
.send()?
.error_for_status()?
.json::<GitHubReleaseJsonResponse>()?;
trace!("latest_github_release: {latest_github_release:?}");
let latest_github_release = latest_github_release.tag_name;
if semver::Version::parse(&latest_github_release)?
<= semver::Version::parse(CURRENT_VERSION)?
{
debug!(
"Skipping up update, current version '{CURRENT_VERSION}' is not older than latest \
GitHub version '{latest_github_release}'",
);
return Ok(TaskStatus::Skipped);
}
trace!("Updating up from '{CURRENT_VERSION}' to '{latest_github_release}'",);
}
let temp_dir = Utf8PathBuf::try_from(env::temp_dir())?;
let temp_path = &temp_dir.join(format!("up-{}", Utc::now().to_rfc3339()));
trace!("Downloading url {url} to path {up_path}", url = &opts.url,);
trace!("Using temporary path: {temp_path}");
let mut response = reqwest::blocking::get(&opts.url)?.error_for_status()?;
fs::create_dir_all(&temp_dir).wrap_err_with(|| E::CreateDir { path: temp_dir })?;
let mut dest = File::create(temp_path).wrap_err_with(|| E::CreateFile {
path: temp_path.clone(),
})?;
io::copy(&mut response, &mut dest).wrap_err(E::Copy {})?;
let permissions = Permissions::from_mode(0o755);
fs::set_permissions(temp_path, permissions).wrap_err_with(|| E::SetPermissions {
path: temp_path.clone(),
})?;
let new_version = cmd!(temp_path.as_str(), "--version").read()?;
let new_version = new_version.trim_start_matches(concat!(env!("CARGO_PKG_NAME"), " "));
if semver::Version::parse(new_version)? > semver::Version::parse(CURRENT_VERSION)? {
info!("Updating up from '{CURRENT_VERSION}' to '{new_version}'",);
fs::rename(temp_path, &up_path).wrap_err_with(|| E::Rename {
from: temp_path.clone(),
to: up_path.clone(),
})?;
Ok(TaskStatus::Passed)
} else {
debug!(
"Skipping up update, current version '{CURRENT_VERSION}' and new version \
'{new_version}'",
);
Ok(TaskStatus::Skipped)
}
}
#[derive(Error, Debug, Display)]
pub enum UpdateSelfError {
CreateDir {
path: Utf8PathBuf,
},
CreateFile {
path: Utf8PathBuf,
},
Copy,
SetPermissions {
path: Utf8PathBuf,
},
Rename {
from: Utf8PathBuf,
to: Utf8PathBuf,
},
}