Skip to content

Commit 416a2b9

Browse files
committed
refactor: move get_source_id out of registry
1 parent 637a2cd commit 416a2b9

File tree

6 files changed

+34
-19
lines changed

6 files changed

+34
-19
lines changed

src/cargo/ops/registry/login.rs

+9-2
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@ pub fn registry_login(
2626
) -> CargoResult<()> {
2727
let source_ids = get_source_id(gctx, reg_or_index)?;
2828

29-
let login_url = match registry(gctx, token_from_cmdline.clone(), reg_or_index, false, None) {
30-
Ok((registry, _)) => Some(format!("{}/me", registry.host())),
29+
let login_url = match registry(
30+
gctx,
31+
&source_ids,
32+
token_from_cmdline.clone(),
33+
reg_or_index,
34+
false,
35+
None,
36+
) {
37+
Ok(registry) => Some(format!("{}/me", registry.host())),
3138
Err(e) if e.is::<AuthorizationError>() => e
3239
.downcast::<AuthorizationError>()
3340
.unwrap()

src/cargo/ops/registry/mod.rs

+9-6
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ impl RegistryCredentialConfig {
107107

108108
/// Returns the `Registry` and `Source` based on command-line and config settings.
109109
///
110+
/// * `source_ids`: The source IDs for the registry. It contains the original source ID and
111+
/// the replacement source ID.
110112
/// * `token_from_cmdline`: The token from the command-line. If not set, uses the token
111113
/// from the config.
112114
/// * `index`: The index URL from the command-line.
@@ -116,13 +118,12 @@ impl RegistryCredentialConfig {
116118
/// * `token_required`: If `true`, the token will be set.
117119
fn registry(
118120
gctx: &GlobalContext,
121+
source_ids: &RegistrySourceIds,
119122
token_from_cmdline: Option<Secret<&str>>,
120123
reg_or_index: Option<&RegistryOrIndex>,
121124
force_update: bool,
122125
token_required: Option<Operation<'_>>,
123-
) -> CargoResult<(Registry, RegistrySourceIds)> {
124-
let source_ids = get_source_id(gctx, reg_or_index)?;
125-
126+
) -> CargoResult<Registry> {
126127
let is_index = reg_or_index.map(|v| v.is_index()).unwrap_or_default();
127128
if is_index && token_required.is_some() && token_from_cmdline.is_none() {
128129
bail!("command-line argument --index requires --token to be specified");
@@ -165,9 +166,11 @@ fn registry(
165166
None
166167
};
167168
let handle = http_handle(gctx)?;
168-
Ok((
169-
Registry::new_handle(api_host, token, handle, cfg.auth_required),
170-
source_ids,
169+
Ok(Registry::new_handle(
170+
api_host,
171+
token,
172+
handle,
173+
cfg.auth_required,
171174
))
172175
}
173176

src/cargo/ops/registry/owner.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,10 @@ pub fn modify_owners(gctx: &GlobalContext, opts: &OwnersOptions) -> CargoResult<
3535
};
3636

3737
let operation = Operation::Owners { name: &name };
38-
39-
let (mut registry, _) = super::registry(
38+
let source_ids = super::get_source_id(gctx, opts.reg_or_index.as_ref())?;
39+
let mut registry = super::registry(
4040
gctx,
41+
&source_ids,
4142
opts.token.as_ref().map(Secret::as_deref),
4243
opts.reg_or_index.as_ref(),
4344
true,

src/cargo/ops/registry/publish.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -130,14 +130,16 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
130130
}
131131
val => val,
132132
};
133-
let (mut registry, reg_ids) = super::registry(
133+
let source_ids = super::get_source_id(opts.gctx, reg_or_index.as_ref())?;
134+
let mut registry = super::registry(
134135
opts.gctx,
136+
&source_ids,
135137
opts.token.as_ref().map(Secret::as_deref),
136138
reg_or_index.as_ref(),
137139
true,
138140
Some(operation).filter(|_| !opts.dry_run),
139141
)?;
140-
verify_dependencies(pkg, &registry, reg_ids.original)?;
142+
verify_dependencies(pkg, &registry, source_ids.original)?;
141143

142144
// Prepare a tarball, with a non-suppressible warning if metadata
143145
// is missing since this is being put online.
@@ -169,7 +171,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
169171
};
170172
registry.set_token(Some(auth::auth_token(
171173
&opts.gctx,
172-
&reg_ids.original,
174+
&source_ids.original,
173175
None,
174176
operation,
175177
vec![],
@@ -185,7 +187,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
185187
pkg,
186188
tarball.file(),
187189
&mut registry,
188-
reg_ids.original,
190+
source_ids.original,
189191
opts.dry_run,
190192
)?;
191193
if !opts.dry_run {
@@ -198,7 +200,7 @@ pub fn publish(ws: &Workspace<'_>, opts: &PublishOpts<'_>) -> CargoResult<()> {
198200
};
199201
if 0 < timeout {
200202
let timeout = Duration::from_secs(timeout);
201-
wait_for_publish(opts.gctx, reg_ids.original, pkg, timeout)?;
203+
wait_for_publish(opts.gctx, source_ids.original, pkg, timeout)?;
202204
}
203205
}
204206

src/cargo/ops/registry/search.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ pub fn search(
2020
reg_or_index: Option<RegistryOrIndex>,
2121
limit: u32,
2222
) -> CargoResult<()> {
23-
let (mut registry, source_ids) =
24-
super::registry(gctx, None, reg_or_index.as_ref(), false, None)?;
23+
let source_ids = super::get_source_id(gctx, reg_or_index.as_ref())?;
24+
let mut registry =
25+
super::registry(gctx, &source_ids, None, reg_or_index.as_ref(), false, None)?;
2526
let (crates, total_crates) = registry.search(query, limit).with_context(|| {
2627
format!(
2728
"failed to retrieve search results from the registry at {}",

src/cargo/ops/registry/yank.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,10 @@ pub fn yank(
4646
vers: &version,
4747
}
4848
};
49-
50-
let (mut registry, _) = super::registry(
49+
let source_ids = super::get_source_id(gctx, reg_or_index.as_ref())?;
50+
let mut registry = super::registry(
5151
gctx,
52+
&source_ids,
5253
token.as_ref().map(Secret::as_deref),
5354
reg_or_index.as_ref(),
5455
true,

0 commit comments

Comments
 (0)