项目作者: jaemk

项目描述 :
Self updates for rust executables
高级语言: Rust
项目地址: git://github.com/jaemk/self_update.git
创建时间: 2017-07-25T01:26:48Z
项目社区:https://github.com/jaemk/self_update

开源协议:MIT License

下载


self_update

Build status
Build Status
crates.io:clin
docs

self_update provides updaters for updating rust executables in-place from various release
distribution backends.

Usage

Update (replace) the current executable with the latest release downloaded
from https://api.github.com/repos/jaemk/self_update/releases/latest.
Note, the trust project provides a nice setup for
producing release-builds via CI (travis/appveyor).

Features

The following cargo features are
available (but disabled by default):

  • archive-tar: Support for tar archive format;
  • archive-zip: Support for zip archive format;
  • compression-flate2: Support for gzip compression;
  • compression-zip-deflate: Support for zip‘s deflate compression format;
  • compression-zip-bzip2: Support for zip‘s bzip2 compression format;
  • rustls: Use pure rust TLS implementation for network requests. This feature does not support 32bit macOS;
  • signatures: Use zipsign to verify .zip and .tar.gz artifacts. Artifacts are assumed to have been signed using zipsign.

Please activate the feature(s) needed by your release files.

Example

Run the following example to see self_update in action:

cargo run --example github --features "archive-tar archive-zip compression-flate2 compression-zip-deflate".

There’s also an equivalent example for gitlab:

cargo run --example gitlab --features "archive-tar archive-zip compression-flate2 compression-zip-deflate".

which runs something roughly equivalent to:

  1. use self_update::cargo_crate_version;
  2. fn update() -> Result<(), Box<dyn std::error::Error>> {
  3. let status = self_update::backends::github::Update::configure()
  4. .repo_owner("jaemk")
  5. .repo_name("self_update")
  6. .bin_name("github")
  7. .show_download_progress(true)
  8. .current_version(cargo_crate_version!())
  9. .build()?
  10. .update()?;
  11. println!("Update status: `{}`!", status.version());
  12. Ok(())
  13. }

Amazon S3, Google GCS, and DigitalOcean Spaces are also supported through the S3 backend to check for new releases. Provided a bucket_name
and asset_prefix string, self_update will look up all matching files using the following format
as a convention for the filenames: [directory/]<asset name>-<semver>-<platform/target>.<extension>.
Leading directories will be stripped from the file name allowing the use of subdirectories in the S3 bucket,
and any file not matching the format, or not matching the provided prefix string, will be ignored.

  1. use self_update::cargo_crate_version;
  2. fn update() -> Result<(), Box<::std::error::Error>> {
  3. let status = self_update::backends::s3::Update::configure()
  4. .bucket_name("self_update_releases")
  5. .asset_prefix("something/self_update")
  6. .region("eu-west-2")
  7. .bin_name("self_update_example")
  8. .show_download_progress(true)
  9. .current_version(cargo_crate_version!())
  10. .build()?
  11. .update()?;
  12. println!("S3 Update status: `{}`!", status.version());
  13. Ok(())
  14. }

Separate utilities are also exposed (NOTE: the following example requires the archive-tar feature,
see the features section above). The self_replace crate is re-exported for convenience:

  1. fn update() -> Result<(), Box<dyn std::error::Error>> {
  2. let releases = self_update::backends::github::ReleaseList::configure()
  3. .repo_owner("jaemk")
  4. .repo_name("self_update")
  5. .build()?
  6. .fetch()?;
  7. println!("found releases:");
  8. println!("{:#?}\n", releases);
  9. // get the first available release
  10. let asset = releases[0]
  11. .asset_for(&self_update::get_target(), None)
  12. .unwrap();
  13. let tmp_dir = tempfile::Builder::new()
  14. .prefix("self_update")
  15. .tempdir_in(::std::env::current_dir()?)?;
  16. let tmp_tarball_path = tmp_dir.path().join(&asset.name);
  17. let tmp_tarball = ::std::fs::File::open(&tmp_tarball_path)?;
  18. self_update::Download::from_url(&asset.download_url)
  19. .set_header(reqwest::header::ACCEPT, "application/octet-stream".parse()?)
  20. .download_to(&tmp_tarball)?;
  21. let bin_name = std::path::PathBuf::from("self_update_bin");
  22. self_update::Extract::from_source(&tmp_tarball_path)
  23. .archive(self_update::ArchiveKind::Tar(Some(self_update::Compression::Gz)))
  24. .extract_file(&tmp_dir.path(), &bin_name)?;
  25. let new_exe = tmp_dir.path().join(bin_name);
  26. self_replace::self_replace(new_exe)?;
  27. Ok(())
  28. }

Troubleshooting

When using cross compilation tools such as cross if you want to use rustls and not openssl

  1. self_update = { version = "0.27.0", features = ["rustls"], default-features = false }

License: MIT