Skip to content

Commit 7736762

Browse files
committed
refactor: move info into registry mod
1 parent 476969f commit 7736762

File tree

11 files changed

+93
-183
lines changed

11 files changed

+93
-183
lines changed

src/bin/cargo/commands/info.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cargo::ops::cargo_info::info;
1+
use cargo::ops::info;
22
use cargo::util::command_prelude::*;
33
use cargo_util_schemas::core::PackageIdSpec;
44

src/cargo/ops/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub use self::cargo_update::write_manifest_upgrades;
2424
pub use self::cargo_update::UpdateOptions;
2525
pub use self::fix::{fix, fix_exec_rustc, fix_get_proxy_lock_addr, FixOptions};
2626
pub use self::lockfile::{load_pkg_lockfile, resolve_to_string, write_pkg_lockfile};
27+
pub use self::registry::info;
2728
pub use self::registry::modify_owners;
2829
pub use self::registry::publish;
2930
pub use self::registry::registry_login;
@@ -46,7 +47,6 @@ pub(crate) mod cargo_compile;
4647
pub mod cargo_config;
4748
mod cargo_doc;
4849
mod cargo_fetch;
49-
pub mod cargo_info;
5050
mod cargo_install;
5151
mod cargo_new;
5252
mod cargo_output_metadata;

src/cargo/ops/cargo_info/mod.rs renamed to src/cargo/ops/registry/info/mod.rs

+27-159
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,34 @@
1-
mod view;
2-
3-
use std::collections::HashSet;
4-
use std::task::Poll;
5-
6-
use anyhow::{bail, Context};
1+
use anyhow::bail;
72
use cargo_credential::Operation;
83
use cargo_util_schemas::core::{PackageIdSpec, PartialVersion};
94
use crates_io::Registry as CratesIoRegistry;
105
use crates_io::User;
116

127
use crate::core::registry::PackageRegistry;
13-
use crate::core::{
14-
Dependency, Package, PackageId, PackageIdSpecQuery, Registry, SourceId, Workspace,
15-
};
16-
use crate::ops::cargo_info::view::pretty_view;
17-
use crate::ops::registry::RegistryOrIndex;
8+
use crate::core::{Dependency, Package, PackageId, PackageIdSpecQuery, Registry, Workspace};
9+
use crate::ops::registry::info::view::pretty_view;
10+
use crate::ops::registry::{RegistryOrIndex, RegistrySourceIds};
1811
use crate::ops::resolve_ws;
19-
use crate::sources::source::{QueryKind, Source};
20-
use crate::sources::{IndexSummary, RegistrySource, SourceConfigMap};
21-
use crate::util::auth::{auth_token, AuthorizationErrorReason};
12+
use crate::sources::source::QueryKind;
13+
use crate::sources::{IndexSummary, SourceConfigMap};
2214
use crate::util::cache_lock::CacheLockMode;
2315
use crate::util::command_prelude::root_manifest;
24-
use crate::util::network::http::http_handle;
2516
use crate::{CargoResult, GlobalContext};
2617

18+
use super::registry;
19+
20+
mod view;
21+
2722
pub fn info(
2823
spec: &PackageIdSpec,
2924
gctx: &GlobalContext,
3025
reg_or_index: Option<RegistryOrIndex>,
3126
) -> CargoResult<()> {
3227
let source_config = SourceConfigMap::new(gctx)?;
33-
let mut registry = PackageRegistry::new_with_source_config(gctx, source_config)?;
28+
let mut reg = PackageRegistry::new_with_source_config(gctx, source_config)?;
3429
// Make sure we get the lock before we download anything.
3530
let _lock = gctx.acquire_package_cache_lock(CacheLockMode::DownloadExclusive)?;
36-
registry.lock_patches();
31+
reg.lock_patches();
3732

3833
// If we can find it in workspace, use it as a specific version.
3934
let nearest_manifest_path = root_manifest(None, gctx).ok();
@@ -46,7 +41,14 @@ pub fn info(
4641
.and_then(|path| ws.members().find(|p| p.manifest_path() == path))
4742
});
4843
let (mut package_id, is_member) = find_pkgid_in_ws(nearest_package, ws.as_ref(), spec);
49-
let (use_package_source_id, source_ids) = get_source_id(gctx, reg_or_index, package_id)?;
44+
let (mut api_registry, use_package_source_id, source_ids) = registry(
45+
gctx,
46+
package_id,
47+
None,
48+
reg_or_index.as_ref(),
49+
false,
50+
Some(Operation::Read),
51+
)?;
5052
// If we don't use the package's source, we need to query the package ID from the specified registry.
5153
if !use_package_source_id {
5254
package_id = None;
@@ -76,15 +78,15 @@ pub fn info(
7678
// For workspace members, `cargo tree --package <SPEC> --invert` is useless. It only prints itself.
7779
let suggest_cargo_tree_command = package_id.is_some() && !is_member;
7880

79-
let summaries = query_summaries(spec, &mut registry, &source_ids)?;
81+
let summaries = query_summaries(spec, &mut reg, &source_ids)?;
8082
let package_id = match package_id {
8183
Some(id) => id,
8284
None => find_pkgid_in_summaries(&summaries, spec, &rustc_version, &source_ids)?,
8385
};
8486

85-
let package = registry.get(&[package_id])?;
87+
let package = reg.get(&[package_id])?;
8688
let package = package.get_one(package_id)?;
87-
let owners = try_list_owners(gctx, source_ids, package_id.name().as_str())?;
89+
let owners = try_list_owners(&mut api_registry, source_ids, package_id.name().as_str())?;
8890
pretty_view(
8991
package,
9092
&summaries,
@@ -209,23 +211,17 @@ fn query_summaries(
209211

210212
// Try to list the login and name of all owners of a crate.
211213
fn try_list_owners(
212-
gctx: &GlobalContext,
214+
api_registry: &mut CratesIoRegistry,
213215
source_ids: RegistrySourceIds,
214216
package_name: &str,
215217
) -> CargoResult<Option<Vec<String>>> {
216218
// Only remote registries support listing owners.
217219
if !source_ids.original.is_remote_registry() {
218220
return Ok(None);
219221
}
220-
let registry = api_registry(gctx, source_ids)?;
221-
match registry {
222-
Some(mut registry) => {
223-
let owners = registry.list_owners(package_name)?;
224-
let names = owners.iter().map(get_username).collect();
225-
Ok(Some(names))
226-
}
227-
None => Ok(None),
228-
}
222+
let owners = api_registry.list_owners(package_name)?;
223+
let names = owners.iter().map(get_username).collect();
224+
Ok(Some(names))
229225
}
230226

231227
fn get_username(u: &User) -> String {
@@ -239,134 +235,6 @@ fn get_username(u: &User) -> String {
239235
)
240236
}
241237

242-
struct RegistrySourceIds {
243-
/// Use when looking up the auth token, or writing out `Cargo.lock`
244-
original: SourceId,
245-
/// Use when interacting with the source (querying / publishing , etc)
246-
///
247-
/// The source for crates.io may be replaced by a built-in source for accessing crates.io with
248-
/// the sparse protocol, or a source for the testing framework (when the replace_crates_io
249-
/// function is used)
250-
///
251-
/// User-defined source replacement is not applied.
252-
/// Note: This will be utilized when interfacing with the registry API.
253-
replacement: SourceId,
254-
}
255-
256-
fn get_source_id(
257-
gctx: &GlobalContext,
258-
reg_or_index: Option<RegistryOrIndex>,
259-
package_id: Option<PackageId>,
260-
) -> CargoResult<(bool, RegistrySourceIds)> {
261-
let (use_package_source_id, sid) = match (&reg_or_index, package_id) {
262-
(None, Some(package_id)) => (true, package_id.source_id()),
263-
(None, None) => (false, SourceId::crates_io(gctx)?),
264-
(Some(RegistryOrIndex::Index(url)), None) => (false, SourceId::for_registry(url)?),
265-
(Some(RegistryOrIndex::Registry(r)), None) => (false, SourceId::alt_registry(gctx, r)?),
266-
(Some(reg_or_index), Some(package_id)) => {
267-
let sid = match reg_or_index {
268-
RegistryOrIndex::Index(url) => SourceId::for_registry(url)?,
269-
RegistryOrIndex::Registry(r) => SourceId::alt_registry(gctx, r)?,
270-
};
271-
let package_source_id = package_id.source_id();
272-
// Same registry, use the package's source.
273-
if sid == package_source_id {
274-
(true, sid)
275-
} else {
276-
let pkg_source_replacement_sid = SourceConfigMap::new(gctx)?
277-
.load(package_source_id, &HashSet::new())?
278-
.replaced_source_id();
279-
// Use the package's source if the specified registry is a replacement for the package's source.
280-
if pkg_source_replacement_sid == sid {
281-
(true, package_source_id)
282-
} else {
283-
(false, sid)
284-
}
285-
}
286-
}
287-
};
288-
// Load source replacements that are built-in to Cargo.
289-
let builtin_replacement_sid = SourceConfigMap::empty(gctx)?
290-
.load(sid, &HashSet::new())?
291-
.replaced_source_id();
292-
let replacement_sid = SourceConfigMap::new(gctx)?
293-
.load(sid, &HashSet::new())?
294-
.replaced_source_id();
295-
// Check if the user has configured source-replacement for the registry we are querying.
296-
if reg_or_index.is_none() && replacement_sid != builtin_replacement_sid {
297-
// Neither --registry nor --index was passed and the user has configured source-replacement.
298-
if let Some(replacement_name) = replacement_sid.alt_registry_key() {
299-
bail!("crates-io is replaced with remote registry {replacement_name};\ninclude `--registry {replacement_name}` or `--registry crates-io`");
300-
} else {
301-
bail!("crates-io is replaced with non-remote-registry source {replacement_sid};\ninclude `--registry crates-io` to use crates.io");
302-
}
303-
} else {
304-
Ok((
305-
use_package_source_id,
306-
RegistrySourceIds {
307-
original: sid,
308-
replacement: builtin_replacement_sid,
309-
},
310-
))
311-
}
312-
}
313-
314-
// Try to get the crates.io registry which is used to access the registry API.
315-
// If the user is not logged in, the function will return None.
316-
fn api_registry(
317-
gctx: &GlobalContext,
318-
source_ids: RegistrySourceIds,
319-
) -> CargoResult<Option<CratesIoRegistry>> {
320-
let cfg = {
321-
let mut src = RegistrySource::remote(source_ids.replacement, &HashSet::new(), gctx)?;
322-
let cfg = loop {
323-
match src.config()? {
324-
Poll::Pending => src
325-
.block_until_ready()
326-
.with_context(|| format!("failed to update {}", source_ids.replacement))?,
327-
Poll::Ready(cfg) => break cfg,
328-
}
329-
};
330-
cfg.expect("remote registries must have config")
331-
};
332-
// This should only happen if the user has a custom registry configured.
333-
// Some registries may not have API support.
334-
let api_host = match cfg.api {
335-
Some(api_host) => api_host,
336-
None => return Ok(None),
337-
};
338-
let token = match auth_token(
339-
gctx,
340-
&source_ids.original,
341-
None,
342-
Operation::Read,
343-
vec![],
344-
false,
345-
) {
346-
Ok(token) => Some(token),
347-
Err(err) => {
348-
// If the token is missing, it means the user is not logged in.
349-
// We don't want to show an error in this case.
350-
if err.to_string().contains(
351-
(AuthorizationErrorReason::TokenMissing)
352-
.to_string()
353-
.as_str(),
354-
) {
355-
return Ok(None);
356-
}
357-
return Err(err);
358-
}
359-
};
360-
361-
let handle = http_handle(gctx)?;
362-
Ok(Some(CratesIoRegistry::new_handle(
363-
api_host,
364-
token,
365-
handle,
366-
cfg.auth_required,
367-
)))
368-
}
369-
370238
fn validate_locked_and_frozen_options(
371239
package_id: Option<PackageId>,
372240
gctx: &GlobalContext,

src/cargo/ops/cargo_info/view.rs renamed to src/cargo/ops/registry/info/view.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn pretty_source(source: SourceId, ctx: &GlobalContext) -> String {
169169
.local_path()
170170
.and_then(|path| pathdiff::diff_paths(path, ctx.cwd()))
171171
{
172-
let path = std::path::Path::new(".").join(relpath);
172+
let path = std::path::Path::new("../../../../..").join(relpath);
173173
path.display().to_string()
174174
} else {
175175
source.to_string()

src/cargo/ops/registry/login.rs

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,17 @@ pub fn registry_login(
2424
reg_or_index: Option<&RegistryOrIndex>,
2525
args: &[&str],
2626
) -> CargoResult<()> {
27-
let source_ids = get_source_id(gctx, reg_or_index)?;
27+
let (_, source_ids) = get_source_id(gctx, None, 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+
None,
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/logout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub fn registry_logout(
1414
gctx: &GlobalContext,
1515
reg_or_index: Option<RegistryOrIndex>,
1616
) -> CargoResult<()> {
17-
let source_ids = get_source_id(gctx, reg_or_index.as_ref())?;
17+
let (_, source_ids) = get_source_id(gctx, None, reg_or_index.as_ref())?;
1818
auth::logout(gctx, &source_ids.original)?;
1919
Ok(())
2020
}

0 commit comments

Comments
 (0)