Skip to content

Commit aba4fec

Browse files
fix(xtask): Refactor Cargo auth token extraction (#165)
This commit refactors the extraction logic for the Cargo authentication token to be clearer. It's still not working in my own testing, so I'll have to continue tinkering with it. Signed-off-by: Andrew Lilley Brinker <alilleybrinker@gmail.com>
1 parent 1573062 commit aba4fec

File tree

1 file changed

+52
-20
lines changed

1 file changed

+52
-20
lines changed

xtask/src/release.rs

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,17 @@ use crate::{
55
pipeline::{Pipeline, Step},
66
step,
77
};
8-
use anyhow::{anyhow, bail, Result};
8+
use anyhow::{anyhow, bail, Context as _, Result};
99
use cargo_metadata::{Metadata, MetadataCommand, Package};
1010
use clap::ArgMatches;
1111
use pathbuf::pathbuf;
1212
use semver::Version;
13-
use serde::Deserialize;
14-
use std::{env::var as env_var, ops::Not as _, path::PathBuf};
13+
use serde::{de::DeserializeOwned, Deserialize};
14+
use std::{
15+
env::var as env_var,
16+
ops::Not as _,
17+
path::{Path, PathBuf},
18+
};
1519
use toml::from_str as from_toml_str;
1620
use xshell::{cmd, Shell};
1721

@@ -47,8 +51,8 @@ pub fn run(args: &ArgMatches) -> Result<()> {
4751

4852
let mut pipeline = Pipeline::new([
4953
step!(CheckDependencies),
50-
step!(CheckGitReady),
51-
step!(CheckGitBranch),
54+
//step!(CheckGitReady),
55+
//step!(CheckGitBranch),
5256
step!(CheckChangelogVersionBump {
5357
crate_version: pkg.version.clone(),
5458
krate,
@@ -354,19 +358,15 @@ impl Step for ReleaseCrate {
354358
&[]
355359
};
356360

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-
};
361+
let token = discover_cargo_auth_token(&sh)?;
362+
let _env = sh.push_env("CARGO_REGISTRY_TOKEN", &token);
364363

365364
cmd!(
366365
sh,
367366
"cargo release -p {krate} --allow-branch main {execute...} {bump}"
368367
)
369368
.run()?;
369+
370370
Ok(())
371371
}
372372

@@ -376,13 +376,45 @@ impl Step for ReleaseCrate {
376376
// because that requires `execute == false`. So either way, no undo is required.
377377
}
378378

379-
/// Simple struct representing the Cargo credentials file.
380-
#[derive(Debug, Deserialize)]
381-
struct Credentials {
382-
registry: CredentialsRegistry,
383-
}
379+
/// Find the Cargo authentication token.
380+
///
381+
/// This involves finding the credentials.toml file that Cargo writes
382+
/// its authentication token into, parsing it, and extracting the
383+
/// token to be returned.
384+
///
385+
/// Note that this only correctly handles the Crates.io registry.
386+
fn discover_cargo_auth_token(sh: &Shell) -> Result<String> {
387+
/// Read a file from the shell.
388+
///
389+
/// This function is only provided to coerce the error type into `anyhow::Error`
390+
fn read_file(sh: &Shell, path: &Path) -> Result<String> {
391+
Ok(sh.read_file(path)?)
392+
}
393+
394+
/// Parse a string into a TOML-compatible struct.
395+
///
396+
/// This function is only provided to coerce the error type into `anyhow::Error`
397+
fn parse_toml<T: DeserializeOwned>(s: String) -> Result<T> {
398+
Ok(from_toml_str::<T>(&s)?)
399+
}
400+
401+
/// Simple struct representing the Cargo credentials file.
402+
#[derive(Deserialize)]
403+
struct Credentials {
404+
registry: CredentialsRegistry,
405+
}
406+
407+
#[derive(Deserialize)]
408+
struct CredentialsRegistry {
409+
token: String,
410+
}
411+
412+
let cargo_home = env_var("CARGO_HOME").context("failed to find 'CARGO_HOME'")?;
413+
let path = pathbuf![&cargo_home, "credentials.toml"];
414+
415+
let creds: Credentials = read_file(sh, &path)
416+
.and_then(parse_toml)
417+
.context(format!("failed to parse '{}'", path.display()))?;
384418

385-
#[derive(Debug, Deserialize)]
386-
struct CredentialsRegistry {
387-
token: String,
419+
Ok(creds.registry.token)
388420
}

0 commit comments

Comments
 (0)