Skip to content

Commit d263ca6

Browse files
committed
refactor: extract yank to its own module
1 parent 7c0add0 commit d263ca6

File tree

2 files changed

+78
-62
lines changed

2 files changed

+78
-62
lines changed

src/cargo/ops/registry/mod.rs

Lines changed: 2 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ mod login;
66
mod logout;
77
mod publish;
88
mod search;
9+
mod yank;
910

1011
use std::collections::HashSet;
1112
use std::path::PathBuf;
@@ -34,6 +35,7 @@ pub use self::logout::registry_logout;
3435
pub use self::publish::publish;
3536
pub use self::publish::PublishOpts;
3637
pub use self::search::search;
38+
pub use self::yank::yank;
3739

3840
/// Registry settings loaded from config files.
3941
///
@@ -431,68 +433,6 @@ pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
431433
Ok(())
432434
}
433435

434-
pub fn yank(
435-
config: &Config,
436-
krate: Option<String>,
437-
version: Option<String>,
438-
token: Option<Secret<String>>,
439-
index: Option<String>,
440-
undo: bool,
441-
reg: Option<String>,
442-
) -> CargoResult<()> {
443-
let name = match krate {
444-
Some(name) => name,
445-
None => {
446-
let manifest_path = find_root_manifest_for_wd(config.cwd())?;
447-
let ws = Workspace::new(&manifest_path, config)?;
448-
ws.current()?.package_id().name().to_string()
449-
}
450-
};
451-
let version = match version {
452-
Some(v) => v,
453-
None => bail!("a version must be specified to yank"),
454-
};
455-
456-
let message = if undo {
457-
auth::Mutation::Unyank {
458-
name: &name,
459-
vers: &version,
460-
}
461-
} else {
462-
auth::Mutation::Yank {
463-
name: &name,
464-
vers: &version,
465-
}
466-
};
467-
468-
let (mut registry, _) = registry(
469-
config,
470-
token.as_ref().map(Secret::as_deref),
471-
index.as_deref(),
472-
reg.as_deref(),
473-
true,
474-
Some(message),
475-
)?;
476-
477-
let package_spec = format!("{}@{}", name, version);
478-
if undo {
479-
config.shell().status("Unyank", package_spec)?;
480-
registry.unyank(&name, &version).with_context(|| {
481-
format!(
482-
"failed to undo a yank from the registry at {}",
483-
registry.host()
484-
)
485-
})?;
486-
} else {
487-
config.shell().status("Yank", package_spec)?;
488-
registry
489-
.yank(&name, &version)
490-
.with_context(|| format!("failed to yank from the registry at {}", registry.host()))?;
491-
}
492-
493-
Ok(())
494-
}
495-
496436
/// Gets the SourceId for an index or registry setting.
497437
///
498438
/// The `index` and `reg` values are from the command-line or config settings.

src/cargo/ops/registry/yank.rs

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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+
6+
use anyhow::bail;
7+
use anyhow::Context as _;
8+
9+
use crate::core::Workspace;
10+
use crate::util::auth;
11+
use crate::util::auth::Secret;
12+
use crate::util::config::Config;
13+
use crate::util::errors::CargoResult;
14+
use crate::util::important_paths::find_root_manifest_for_wd;
15+
16+
pub fn yank(
17+
config: &Config,
18+
krate: Option<String>,
19+
version: Option<String>,
20+
token: Option<Secret<String>>,
21+
index: Option<String>,
22+
undo: bool,
23+
reg: Option<String>,
24+
) -> CargoResult<()> {
25+
let name = match krate {
26+
Some(name) => name,
27+
None => {
28+
let manifest_path = find_root_manifest_for_wd(config.cwd())?;
29+
let ws = Workspace::new(&manifest_path, config)?;
30+
ws.current()?.package_id().name().to_string()
31+
}
32+
};
33+
let version = match version {
34+
Some(v) => v,
35+
None => bail!("a version must be specified to yank"),
36+
};
37+
38+
let message = if undo {
39+
auth::Mutation::Unyank {
40+
name: &name,
41+
vers: &version,
42+
}
43+
} else {
44+
auth::Mutation::Yank {
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),
53+
index.as_deref(),
54+
reg.as_deref(),
55+
true,
56+
Some(message),
57+
)?;
58+
59+
let package_spec = format!("{}@{}", name, version);
60+
if undo {
61+
config.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+
config.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+
}

0 commit comments

Comments
 (0)