#[cfg(feature = "github-update")]
use self_update::cargo_crate_version;
use std::net::{SocketAddr, TcpStream, ToSocketAddrs};
use std::time::Duration;
#[cfg(feature = "github-update")]
pub(crate) fn run_auto_update_if_enabled() -> bool {
let _ = check_for_update_with_timeout(Duration::from_millis(1000));
true
}
#[cfg(feature = "github-update")]
fn check_for_update_with_timeout(timeout: Duration) -> bool {
if !api_host_is_reachable("api.github.com", 443, timeout) {
return false;
}
check_for_update().is_ok()
}
#[cfg(feature = "github-update")]
fn api_host_is_reachable(host: &str, port: u16, timeout: Duration) -> bool {
let Some(target) = (host, port)
.to_socket_addrs()
.ok()
.and_then(|mut addresses| {
addresses.find(|address| match address {
SocketAddr::V4(_) | SocketAddr::V6(_) => true,
})
})
else {
return false;
};
TcpStream::connect_timeout(&target, timeout).is_ok()
}
#[cfg(feature = "github-update")]
fn check_for_update() -> anyhow::Result<()> {
self_update::backends::github::Update::configure()
.repo_owner("alfanick")
.repo_name("mini-film")
.bin_name("mini-film")
.target(asset_target())
.no_confirm(true)
.show_download_progress(false)
.current_version(cargo_crate_version!())
.build()?
.update()?;
Ok(())
}
#[cfg(feature = "github-update")]
fn asset_target() -> &'static str {
if cfg!(all(target_os = "windows", target_arch = "x86_64")) {
"x86_64-pc-windows-msvc"
} else if cfg!(all(target_os = "linux", target_arch = "x86_64")) {
"x86_64-unknown-linux-gnu"
} else if cfg!(all(target_os = "linux", target_arch = "aarch64")) {
"aarch64-unknown-linux-gnu"
} else if cfg!(all(target_os = "macos", target_arch = "aarch64")) {
"aarch64-apple-darwin"
} else {
self_update::get_target()
}
}
#[cfg(not(feature = "github-update"))]
pub(crate) fn run_auto_update_if_enabled() -> bool {
true
}