cargo/ops/registry/
yank.rs
1use anyhow::bail;
7use anyhow::Context as _;
8use cargo_credential::Operation;
9use cargo_credential::Secret;
10
11use crate::core::Workspace;
12use crate::util::context::GlobalContext;
13use crate::util::errors::CargoResult;
14use crate::util::important_paths::find_root_manifest_for_wd;
15
16use super::RegistryOrIndex;
17
18pub fn yank(
19 gctx: &GlobalContext,
20 krate: Option<String>,
21 version: Option<String>,
22 token: Option<Secret<String>>,
23 reg_or_index: Option<RegistryOrIndex>,
24 undo: bool,
25) -> CargoResult<()> {
26 let name = match krate {
27 Some(name) => name,
28 None => {
29 let manifest_path = find_root_manifest_for_wd(gctx.cwd())?;
30 let ws = Workspace::new(&manifest_path, gctx)?;
31 ws.current()?.package_id().name().to_string()
32 }
33 };
34 let Some(version) = version else {
35 bail!("a version must be specified to yank")
36 };
37
38 let message = if undo {
39 Operation::Unyank {
40 name: &name,
41 vers: &version,
42 }
43 } else {
44 Operation::Yank {
45 name: &name,
46 vers: &version,
47 }
48 };
49 let source_ids = super::get_source_id(gctx, reg_or_index.as_ref())?;
50 let (mut registry, _) = super::registry(
51 gctx,
52 &source_ids,
53 token.as_ref().map(Secret::as_deref),
54 reg_or_index.as_ref(),
55 true,
56 Some(message),
57 )?;
58
59 let package_spec = format!("{}@{}", name, version);
60 if undo {
61 gctx.shell().status("Unyank", package_spec)?;
62 registry.unyank(&name, &version).with_context(|| {
63 format!(
64 "failed to undo a yank from the registry at {}",
65 registry.host()
66 )
67 })?;
68 } else {
69 gctx.shell().status("Yank", package_spec)?;
70 registry
71 .yank(&name, &version)
72 .with_context(|| format!("failed to yank from the registry at {}", registry.host()))?;
73 }
74
75 Ok(())
76}