]> git.proxmox.com Git - rustc.git/blame - src/tools/cargo/src/cargo/ops/registry/yank.rs
New upstream version 1.74.1+dfsg1
[rustc.git] / src / tools / cargo / src / cargo / ops / registry / yank.rs
CommitLineData
fe692bf9
FG
1//! Interacts with the registry [yank] and [unyank] API.
2//!
3//! [yank]: https://doc.rust-lang.org/nightly/cargo/reference/registry-web-api.html#yank
4//! [unyank]: https://doc.rust-lang.org/nightly/cargo/reference/registry-web-api.html#unyank
5
6use anyhow::bail;
7use anyhow::Context as _;
add651ee
FG
8use cargo_credential::Operation;
9use cargo_credential::Secret;
fe692bf9
FG
10
11use crate::core::Workspace;
fe692bf9
FG
12use crate::util::config::Config;
13use crate::util::errors::CargoResult;
14use crate::util::important_paths::find_root_manifest_for_wd;
15
781aab86
FG
16use super::RegistryOrIndex;
17
fe692bf9
FG
18pub fn yank(
19 config: &Config,
20 krate: Option<String>,
21 version: Option<String>,
22 token: Option<Secret<String>>,
781aab86 23 reg_or_index: Option<RegistryOrIndex>,
fe692bf9 24 undo: bool,
fe692bf9
FG
25) -> CargoResult<()> {
26 let name = match krate {
27 Some(name) => name,
28 None => {
29 let manifest_path = find_root_manifest_for_wd(config.cwd())?;
30 let ws = Workspace::new(&manifest_path, config)?;
31 ws.current()?.package_id().name().to_string()
32 }
33 };
781aab86
FG
34 let Some(version) = version else {
35 bail!("a version must be specified to yank")
fe692bf9
FG
36 };
37
38 let message = if undo {
add651ee 39 Operation::Unyank {
fe692bf9
FG
40 name: &name,
41 vers: &version,
42 }
43 } else {
add651ee 44 Operation::Yank {
fe692bf9
FG
45 name: &name,
46 vers: &version,
47 }
48 };
49
50 let (mut registry, _) = super::registry(
51 config,
52 token.as_ref().map(Secret::as_deref),
781aab86 53 reg_or_index.as_ref(),
fe692bf9
FG
54 true,
55 Some(message),
56 )?;
57
58 let package_spec = format!("{}@{}", name, version);
59 if undo {
60 config.shell().status("Unyank", package_spec)?;
61 registry.unyank(&name, &version).with_context(|| {
62 format!(
63 "failed to undo a yank from the registry at {}",
64 registry.host()
65 )
66 })?;
67 } else {
68 config.shell().status("Yank", package_spec)?;
69 registry
70 .yank(&name, &version)
71 .with_context(|| format!("failed to yank from the registry at {}", registry.host()))?;
72 }
73
74 Ok(())
75}