Skip to main content

resolve_token

Function resolve_token 

Source
pub fn resolve_token(
    cli_token: Option<&str>,
    ledger_auth: &LedgerAuth,
) -> Result<Option<String>, TokenError>
Expand description

Attempts to resolve a crates.io API token from three sources in priority order.

  1. cli_token argument (highest priority – for scripting and CI).
  2. ledger_auth.token_env: reads the named environment variable. ledger_auth.token_cmd: runs the command string in a shell and captures stdout. Only one of token_env or token_cmd is used (env takes precedence if both set).
  3. Fall through to ~/.cargo/credentials.toml [registry] token field.

Returns Ok(Some(token)) if any tier succeeds, Ok(None) if no token found (caller decides whether to proceed – cargo itself may have credentials).

§Errors

Returns TokenError::EnvVarEmpty if token_env is set but the variable is empty. Returns TokenError::CmdFailed if token_cmd fails or produces no output. Returns TokenError::CargoCredentials if credentials.toml exists but is unreadable.