Rust 可執行文件自更新軟件庫
self_update 提供更新程序,用於從各種版本分發後端就地更新 rust 可執行文件。
該庫的一些特性如下:
-
archive-tar:支持 tar 歸檔格式;
-
archive-zip:支持 zip 存檔格式;
-
compression-flate2:支持 gzip 壓縮;
-
compression-zip-deflate:支持 zip 的 deflate 壓縮格式;
-
compression-zip-bzip2:支持 zip 的 bzip2 壓縮格式;
-
rustls:對網絡請求使用純 rust-TLS 實現。此功能不支持 32 位 macOS;
-
signatures:使用 zipsign 驗證. zip 和. tar.gz 工件。假定工件已使用 zipsign 簽名。
請選擇激活您的發佈文件所需的功能。例如這樣:
self_update
= { version =
"0.27.0"
, features = [
"rustls"
], default-features =
false
}
self_update 支持從多種後端下載,例如:Gitea、GitLab、Github 和 S3 後端。下面有幾個使用庫的例子:
一、使用 Github 後端:
use
self_update::cargo_crate_version;
fn
update
() ->
Result
<(), Box<::
std
::
error
::
Error
>> {
let
status
= self_update::backends::github::
Update
::configure()
.repo_owner(
"jaemk"
)
.repo_name(
"self_update"
)
.bin_name(
"github"
)
.show_download_progress(
true
)
.current_version(cargo_crate_version!())
.build()?
.update()?;
println!("
Update
status
:
`{}`
!
", status.version());
Ok(())
}
二、也支持如 Amazon S3、Google GCS 和 DigitalOcean Spaces 這樣的 S3 後端檢查新版本。提供 bucket_name 和 asset_prefix 字符串後,self_update 將使用以下格式作爲文件名的約定來查找所有匹配的文件:[directory/]--<platform/target>。前導目錄將從文件名中刪除,允許使用 S3 存儲桶中的子目錄,任何與格式不匹配或與提供的前綴字符串不匹配的文件都將被忽略。
use
self_update::cargo_crate_version;
fn
update
() ->
Result
<(), Box<::
std
::
error
::
Error
>> {
let
status
= self_update::backends::s3::
Update
::configure()
.bucket_name(
"self_update_releases"
)
.asset_prefix(
"something/self_update"
)
.region(
"eu-west-2"
)
.bin_name(
"self_update_example"
)
.show_download_progress(
true
)
.current_version(cargo_crate_version!())
.build()?
.update()?;
println!("S3
Update
status
:
`{}`
!
", status.version());
Ok(())
}
三、公開了一些實用程序,爲了方便,self_replace 被重新導出:
fn update() -> Result<(), Box<::std::error::Error>> {
let releases = self_update::backends::github::ReleaseList::configure()
.repo_owner(
"jaemk"
)
.repo_name(
"self_update"
)
.build()?
.fetch()?;
println!(
"found releases:"
);
println!(
"{:#?}\n"
, releases);
// get the first available release
let asset = releases[
0
]
.asset_for(&self_update::get_target()).unwrap();
let tmp_dir = tempfile::Builder::new()
.prefix(
"self_update"
)
.tempdir_in(::std::env::current_dir()?)?;
let tmp_tarball_path = tmp_dir.path().join(&asset.name);
let tmp_tarball = ::std::fs::File::open(&tmp_tarball_path)?;
self_update::Download::from_url(&asset.download_url)
.set_header(reqwest::header::ACCEPT,
"application/octet-stream"
.parse()?)
.download_to(&tmp_tarball)?;
let bin_name = std::path::PathBuf::from(
"self_update_bin"
);
self_update::Extract::from_source(&tmp_tarball_path)
.archive(self_update::ArchiveKind::Tar(Some(self_update::Compression::Gz)))
.extract_file(&tmp_dir.path(), &bin_name)?;
let new_exe = tmp_dir.path().join(bin_name);
self_replace::self_replace(new_exe)?;
Ok(())
}
更多內容,請參考 Github:
https://github.com/jaemk/self_update
本文由 Readfog 進行 AMP 轉碼,版權歸原作者所有。
來源:https://mp.weixin.qq.com/s/eL8ZOXdsdq2UmanLmiPYNA