Skip to content

Commit

Permalink
refactor: move get_source_id out of registry
Browse files Browse the repository at this point in the history
  • Loading branch information
Rustin170506 committed Jul 9, 2024
1 parent 637a2cd commit 6d1e66f
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 19 deletions.
11 changes: 9 additions & 2 deletions src/cargo/ops/registry/login.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,15 @@ pub fn registry_login(
) -> CargoResult<()> {
let source_ids = get_source_id(gctx, reg_or_index)?;

let login_url = match registry(gctx, token_from_cmdline.clone(), reg_or_index, false, None) {
Ok((registry, _)) => Some(format!("{}/me", registry.host())),
let login_url = match registry(
gctx,
&source_ids,
token_from_cmdline.clone(),
reg_or_index,
false,
None,
) {
Ok(registry) => Some(format!("{}/me", registry.host())),
Err(e) if e.is::<AuthorizationError>() => e
.downcast::<AuthorizationError>()
.unwrap()
Expand Down
14 changes: 8 additions & 6 deletions src/cargo/ops/registry/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ impl RegistryCredentialConfig {

/// Returns the `Registry` and `Source` based on command-line and config settings.
///
/// * `source_ids`: The source ID for the registry or index.
/// * `token_from_cmdline`: The token from the command-line. If not set, uses the token
/// from the config.
/// * `index`: The index URL from the command-line.
Expand All @@ -116,13 +117,12 @@ impl RegistryCredentialConfig {
/// * `token_required`: If `true`, the token will be set.
fn registry(
gctx: &GlobalContext,
source_ids: &RegistrySourceIds,
token_from_cmdline: Option<Secret<&str>>,
reg_or_index: Option<&RegistryOrIndex>,
force_update: bool,
token_required: Option<Operation<'_>>,
) -> CargoResult<(Registry, RegistrySourceIds)> {
let source_ids = get_source_id(gctx, reg_or_index)?;

) -> CargoResult<Registry> {
let is_index = reg_or_index.map(|v| v.is_index()).unwrap_or_default();
if is_index && token_required.is_some() && token_from_cmdline.is_none() {
bail!("command-line argument --index requires --token to be specified");
Expand Down Expand Up @@ -165,9 +165,11 @@ fn registry(
None
};
let handle = http_handle(gctx)?;
Ok((
Registry::new_handle(api_host, token, handle, cfg.auth_required),
source_ids,
Ok(Registry::new_handle(
api_host,
token,
handle,
cfg.auth_required,
))
}

Expand Down
5 changes: 3 additions & 2 deletions src/cargo/ops/registry/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@ pub fn modify_owners(gctx: &GlobalContext, opts: &OwnersOptions) -> CargoResult<
};

let operation = Operation::Owners { name: &name };

let (mut registry, _) = super::registry(
let source_ids = super::get_source_id(gctx, opts.reg_or_index.as_ref())?;
let mut registry = super::registry(
gctx,
&source_ids,
opts.token.as_ref().map(Secret::as_deref),
opts.reg_or_index.as_ref(),
true,
Expand Down
12 changes: 7 additions & 5 deletions src/cargo/ops/registry/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,16 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
}
val => val,
};
let (mut registry, reg_ids) = super::registry(
let source_ids = super::get_source_id(opts.gctx, reg_or_index.as_ref())?;
let mut registry = super::registry(
opts.gctx,
&source_ids,
opts.token.as_ref().map(Secret::as_deref),
reg_or_index.as_ref(),
true,
Some(operation).filter(|_| !opts.dry_run),
)?;
verify_dependencies(pkg, &registry, reg_ids.original)?;
verify_dependencies(pkg, &registry, source_ids.original)?;

// Prepare a tarball, with a non-suppressible warning if metadata
// is missing since this is being put online.
Expand Down Expand Up @@ -169,7 +171,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
};
registry.set_token(Some(auth::auth_token(
&opts.gctx,
&reg_ids.original,
&source_ids.original,
None,
operation,
vec![],
Expand All @@ -185,7 +187,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
pkg,
tarball.file(),
&mut registry,
reg_ids.original,
source_ids.original,
opts.dry_run,
)?;
if !opts.dry_run {
Expand All @@ -198,7 +200,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
};
if 0 < timeout {
let timeout = Duration::from_secs(timeout);
wait_for_publish(opts.gctx, reg_ids.original, pkg, timeout)?;
wait_for_publish(opts.gctx, source_ids.original, pkg, timeout)?;
}
}

Expand Down
5 changes: 3 additions & 2 deletions src/cargo/ops/registry/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ pub fn search(
reg_or_index: Option<RegistryOrIndex>,
limit: u32,
) -> CargoResult<()> {
let (mut registry, source_ids) =
super::registry(gctx, None, reg_or_index.as_ref(), false, None)?;
let source_ids = super::get_source_id(gctx, reg_or_index.as_ref())?;
let mut registry =
super::registry(gctx, &source_ids, None, reg_or_index.as_ref(), false, None)?;
let (crates, total_crates) = registry.search(query, limit).with_context(|| {
format!(
"failed to retrieve search results from the registry at {}",
Expand Down
5 changes: 3 additions & 2 deletions src/cargo/ops/registry/yank.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,10 @@ pub fn yank(
vers: &version,
}
};

let (mut registry, _) = super::registry(
let source_ids = super::get_source_id(gctx, reg_or_index.as_ref())?;
let mut registry = super::registry(
gctx,
&source_ids,
token.as_ref().map(Secret::as_deref),
reg_or_index.as_ref(),
true,
Expand Down

0 comments on commit 6d1e66f

Please sign in to comment.