Skip to content

Commit

Permalink
Remove crates.io token loading and usage
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
nabijaczleweli committed Oct 29, 2016
1 parent ea63b12 commit 625bc86
Show file tree
Hide file tree
Showing 7 changed files with 12 additions and 97 deletions.
3 changes: 1 addition & 2 deletions cargo-install-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ Only updates packages from the main repository.

Exit values and possible errors:

-2 - cargo install process was terminated by a signal (Linux-only)
-1 - failed to acquire crates.io auth token
-1 - cargo install process was terminated by a signal (Linux-only)
1 - option parsing error
X - bubbled-up cargo install exit value

Expand Down
4 changes: 1 addition & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
//! Options::parse()
//! |> installed_main_repo_packages()
//! |> intersect_packages()
//! |> crates_token()
//! |> MainRepoPackage::pull_version()
//! ```
//!
Expand All @@ -32,8 +31,7 @@
//! Exit values and possible errors:
//!
//! ```text
//! -2 - cargo install process was terminated by a signal (Linux-only)
//! -1 - failed to acquire crates.io auth token
//! -1 - cargo install process was terminated by a signal (Linux-only)
//! 1 - option parsing error
//! X - bubbled-up cargo install exit value
//! ```
Expand Down
6 changes: 2 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,8 @@ fn actual_main() -> Result<(), i32> {
packages = cargo_update::ops::intersect_packages(packages, &opts.to_update);
}

let token = try!(cargo_update::ops::crates_token(&opts.cargo_dir.1));

for package in &mut packages {
package.pull_version(&token);
package.pull_version();
}

{
Expand Down Expand Up @@ -55,7 +53,7 @@ fn actual_main() -> Result<(), i32> {

let install_res = Command::new("cargo").arg("install").arg("-f").arg(&package.name).status().unwrap();
if !install_res.success() {
try!(Err(install_res.code().unwrap_or(-2)));
try!(Err(install_res.code().unwrap_or(-1)));
}

println!("");
Expand Down
59 changes: 8 additions & 51 deletions src/ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
//!
//! Use `installed_main_repo_packages()` to list the installed packages,
//! then use `intersect_packages()` to confirm which ones should be updated,
//! acquire the [`crates.io`](https://crates.io) auth token via `crates_token()`,
//! use it to poll the packages' latest versions by calling `MainRepoPackage::pull_version` on them,
//! poll the packages' latest versions by calling `MainRepoPackage::pull_version` on them,
//! continue with doing whatever you wish.

use hyper::header::{Authorization, Bearer};
use hyper::Client as HttpClient;
use semver::Version as Semver;
use std::path::Path;
Expand Down Expand Up @@ -37,7 +35,6 @@ lazy_static! {
/// # use cargo_update::ops::MainRepoPackage;
/// # use semver::Version as Semver;
/// # fn main() {
/// # let crates_token = "Da39A3Ee5e6B4B0D3255bfeF95601890";
/// let package_s = "racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)";
/// let mut package = MainRepoPackage::parse(package_s).unwrap();
/// assert_eq!(package,
Expand All @@ -47,7 +44,7 @@ lazy_static! {
/// newest_version: None,
/// });
///
/// package.pull_version(crates_token);
/// package.pull_version();
/// assert!(package.newest_version.is_some());
/// # }
/// ```
Expand Down Expand Up @@ -124,20 +121,17 @@ impl MainRepoPackage {

/// Download the version list for this crate off the main [`crates.io`](https://crates.io) registry.
///
/// The provided token might or might not need to be valid, investigation ongoing.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::MainRepoPackage;
/// # let crates_token = "Da39A3Ee5e6B4B0D3255bfeF95601890";
/// let package_s = "racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)";
/// let mut package = MainRepoPackage::parse(package_s).unwrap();
/// package.pull_version(crates_token);
/// package.pull_version();
/// assert!(package.newest_version.is_some());
/// ```
pub fn pull_version(&mut self, crates_token: &str) {
let vers = crate_versions(&crate_versions_raw(crates_token, &self.name));
pub fn pull_version(&mut self) {
let vers = crate_versions(&crate_versions_raw(&self.name));
self.newest_version = vers.into_iter().max();
}
}
Expand Down Expand Up @@ -171,38 +165,6 @@ pub fn installed_main_repo_packages(cargo_dir: &Path) -> Vec<MainRepoPackage> {
}
}

/// Load the [`crates.io`](https://crates.io) authorisation token for API requests.
///
/// If the `config` file doesn't exist an `Err` is returned.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::{MainRepoPackage, crates_token};
/// # use std::env::temp_dir;
/// # fn main() {
/// # test();
/// # }
/// # fn test() -> Result<(), i32> {
/// # let cargo_dir = temp_dir();
/// # let mut package = MainRepoPackage::parse("racer 1.2.10 (registry+https://github.com/rust-lang/crates.io-index)").unwrap();
/// let token = try!(crates_token(&cargo_dir));
/// package.pull_version(&token);
/// # Ok(())
/// # }
/// ```
pub fn crates_token(cargo_dir: &Path) -> Result<String, i32> {
let config_path = cargo_dir.join("config");
if config_path.exists() {
let mut config = String::new();
File::open(config_path).unwrap().read_to_string(&mut config).unwrap();

Ok(toml::Parser::new(&config).parse().unwrap()["registry"].as_table().unwrap()["token"].as_str().unwrap().to_string())
} else {
Err(-1)
}
}

/// Filter out the installed packages not specified to be updated.
///
/// List installed packages with `installed_main_repo_packages()`.
Expand Down Expand Up @@ -233,20 +195,16 @@ pub fn intersect_packages(installed: Vec<MainRepoPackage>, to_update: &[String])
///
/// Plug into `crate_versions()` to convert to machine-readable form.
///
/// Whether the token is required to be valid (or at all) is yet to be seen.
///
/// # Examples
///
/// ```
/// # use cargo_update::ops::crate_versions_raw;
/// # let crates_token = "Da39A3Ee5e6B4B0D3255bfeF95601890";
/// let raw_versions = crate_versions_raw(crates_token, "checksums");
/// let raw_versions = crate_versions_raw("checksums");
/// ```
pub fn crate_versions_raw(token: &str, crate_name: &str) -> String {
pub fn crate_versions_raw(crate_name: &str) -> String {
let mut buf = String::new();
HttpClient::new()
.get(&format!("https://crates.io/api/v1/crates/{}/versions", crate_name))
.header(Authorization(Bearer { token: token.to_string() }))
.send()
.unwrap()
.read_to_string(&mut buf)
Expand All @@ -260,8 +218,7 @@ pub fn crate_versions_raw(token: &str, crate_name: &str) -> String {
///
/// ```
/// # use cargo_update::ops::{crate_versions_raw, crate_versions};
/// # let crates_token = "Da39A3Ee5e6B4B0D3255bfeF95601890";
/// # let raw_versions = crate_versions_raw(crates_token, "checksums");
/// # let raw_versions = crate_versions_raw("checksums");
/// let versions = crate_versions(&raw_versions);
///
/// println!("Released versions of checksums:");
Expand Down
2 changes: 0 additions & 2 deletions test-data/cargo-config.toml

This file was deleted.

34 changes: 0 additions & 34 deletions tests/ops/crates_token.rs

This file was deleted.

1 change: 0 additions & 1 deletion tests/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use semver::Version as Semver;

mod installed_main_repo_packages;
mod main_repo_package;
mod crates_token;


static CHECKSUMS_VERSIONS: &'static str = include_str!("../../test-data/checksums-versions.json");
Expand Down

0 comments on commit 625bc86

Please sign in to comment.