Skip to content

Commit 1573062

Browse files
feat(xtask): Make release pickup Cargo creds. (#164)
Previously, `cargo-release` being run in the sub-shell wasn't picking up Cargo's authentication information, even if you'd previously run 'cargo login' to authenticate with the Crates.io registry. This commit is intended to fix that by manually extracting the saved token from Cargo's credentials file and passing it into the subshell using an environment variable. With how `xshell` works, this environment variable won't be logged anywhere, so it's safe to pass the authentication token this way. Signed-off-by: Andrew Lilley Brinker <alilleybrinker@gmail.com>
1 parent 9c96dca commit 1573062

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

xtask/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,7 @@ env_logger = "0.11.2"
1717
log = "0.4.20"
1818
pathbuf = "1.0.0"
1919
semver = "1.0.22"
20+
serde = { version = "1.0.197", features = ["derive"] }
21+
toml = "0.8.10"
2022
which = "6.0.0"
2123
xshell = "0.2.5"

xtask/src/release.rs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ use cargo_metadata::{Metadata, MetadataCommand, Package};
1010
use clap::ArgMatches;
1111
use pathbuf::pathbuf;
1212
use semver::Version;
13-
use std::{ops::Not as _, path::PathBuf};
13+
use serde::Deserialize;
14+
use std::{env::var as env_var, ops::Not as _, path::PathBuf};
15+
use toml::from_str as from_toml_str;
1416
use xshell::{cmd, Shell};
1517

1618
/// Run the release command.
@@ -346,8 +348,19 @@ impl Step for ReleaseCrate {
346348
let krate = self.krate.name();
347349
let bump = self.bump.to_string();
348350

349-
let possible_args = ["--execute", "--no-confirm"];
350-
let execute = self.execute.then_some(&possible_args[..]).unwrap_or(&[]);
351+
let execute = if self.execute {
352+
&["--execute", "--no-confirm"][..]
353+
} else {
354+
&[]
355+
};
356+
357+
// Set the Cargo registry auth token in the shell environment.
358+
let _env = {
359+
let creds_path = pathbuf![&env_var("CARGO_HOME")?, "credentials.toml"];
360+
let raw_creds = sh.read_file(creds_path)?;
361+
let creds = from_toml_str::<Credentials>(&raw_creds)?;
362+
sh.push_env("CARGO_REGISTRY_TOKEN", &creds.registry.token)
363+
};
351364

352365
cmd!(
353366
sh,
@@ -362,3 +375,14 @@ impl Step for ReleaseCrate {
362375
// is enabled, then it'll run, _and_ we won't need to force-rollback at the end
363376
// because that requires `execute == false`. So either way, no undo is required.
364377
}
378+
379+
/// Simple struct representing the Cargo credentials file.
380+
#[derive(Debug, Deserialize)]
381+
struct Credentials {
382+
registry: CredentialsRegistry,
383+
}
384+
385+
#[derive(Debug, Deserialize)]
386+
struct CredentialsRegistry {
387+
token: String,
388+
}

0 commit comments

Comments
 (0)