Skip to content

Commit 677cd5e

Browse files
committed
refactor: extract owner to its own module
1 parent d263ca6 commit 677cd5e

File tree

2 files changed

+98
-82
lines changed

2 files changed

+98
-82
lines changed

src/cargo/ops/registry/mod.rs

Lines changed: 5 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
55
mod login;
66
mod logout;
7+
mod owner;
78
mod publish;
89
mod search;
910
mod yank;
@@ -20,18 +21,19 @@ use curl::easy::{Easy, InfoType, SslOpt, SslVersion};
2021
use log::{log, Level};
2122

2223
use crate::core::source::Source;
23-
use crate::core::{SourceId, Workspace};
24+
use crate::core::SourceId;
2425
use crate::sources::{RegistrySource, SourceConfigMap};
2526
use crate::util::auth::{self, Secret};
2627
use crate::util::config::{Config, SslVersionConfig, SslVersionConfigRange};
2728
use crate::util::errors::CargoResult;
28-
use crate::util::important_paths::find_root_manifest_for_wd;
2929
use crate::util::network;
3030
use crate::util::IntoUrl;
31-
use crate::{drop_print, drop_println, version};
31+
use crate::version;
3232

3333
pub use self::login::registry_login;
3434
pub use self::logout::registry_logout;
35+
pub use self::owner::modify_owners;
36+
pub use self::owner::OwnersOptions;
3537
pub use self::publish::publish;
3638
pub use self::publish::PublishOpts;
3739
pub use self::search::search;
@@ -354,85 +356,6 @@ impl HttpTimeout {
354356
}
355357
}
356358

357-
pub struct OwnersOptions {
358-
pub krate: Option<String>,
359-
pub token: Option<Secret<String>>,
360-
pub index: Option<String>,
361-
pub to_add: Option<Vec<String>>,
362-
pub to_remove: Option<Vec<String>>,
363-
pub list: bool,
364-
pub registry: Option<String>,
365-
}
366-
367-
pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
368-
let name = match opts.krate {
369-
Some(ref name) => name.clone(),
370-
None => {
371-
let manifest_path = find_root_manifest_for_wd(config.cwd())?;
372-
let ws = Workspace::new(&manifest_path, config)?;
373-
ws.current()?.package_id().name().to_string()
374-
}
375-
};
376-
377-
let mutation = auth::Mutation::Owners { name: &name };
378-
379-
let (mut registry, _) = registry(
380-
config,
381-
opts.token.as_ref().map(Secret::as_deref),
382-
opts.index.as_deref(),
383-
opts.registry.as_deref(),
384-
true,
385-
Some(mutation),
386-
)?;
387-
388-
if let Some(ref v) = opts.to_add {
389-
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
390-
let msg = registry.add_owners(&name, &v).with_context(|| {
391-
format!(
392-
"failed to invite owners to crate `{}` on registry at {}",
393-
name,
394-
registry.host()
395-
)
396-
})?;
397-
398-
config.shell().status("Owner", msg)?;
399-
}
400-
401-
if let Some(ref v) = opts.to_remove {
402-
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
403-
config
404-
.shell()
405-
.status("Owner", format!("removing {:?} from crate {}", v, name))?;
406-
registry.remove_owners(&name, &v).with_context(|| {
407-
format!(
408-
"failed to remove owners from crate `{}` on registry at {}",
409-
name,
410-
registry.host()
411-
)
412-
})?;
413-
}
414-
415-
if opts.list {
416-
let owners = registry.list_owners(&name).with_context(|| {
417-
format!(
418-
"failed to list owners of crate `{}` on registry at {}",
419-
name,
420-
registry.host()
421-
)
422-
})?;
423-
for owner in owners.iter() {
424-
drop_print!(config, "{}", owner.login);
425-
match (owner.name.as_ref(), owner.email.as_ref()) {
426-
(Some(name), Some(email)) => drop_println!(config, " ({} <{}>)", name, email),
427-
(Some(s), None) | (None, Some(s)) => drop_println!(config, " ({})", s),
428-
(None, None) => drop_println!(config),
429-
}
430-
}
431-
}
432-
433-
Ok(())
434-
}
435-
436359
/// Gets the SourceId for an index or registry setting.
437360
///
438361
/// The `index` and `reg` values are from the command-line or config settings.

src/cargo/ops/registry/owner.rs

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//! Interacts with the registry [owners API][1].
2+
//!
3+
//! [1]: https://doc.rust-lang.org/nightly/cargo/reference/registry-web-api.html#owners
4+
5+
use anyhow::Context as _;
6+
7+
use crate::core::Workspace;
8+
use crate::drop_print;
9+
use crate::drop_println;
10+
use crate::util::auth;
11+
use crate::util::auth::Secret;
12+
use crate::util::important_paths::find_root_manifest_for_wd;
13+
use crate::CargoResult;
14+
use crate::Config;
15+
16+
pub struct OwnersOptions {
17+
pub krate: Option<String>,
18+
pub token: Option<Secret<String>>,
19+
pub index: Option<String>,
20+
pub to_add: Option<Vec<String>>,
21+
pub to_remove: Option<Vec<String>>,
22+
pub list: bool,
23+
pub registry: Option<String>,
24+
}
25+
26+
pub fn modify_owners(config: &Config, opts: &OwnersOptions) -> CargoResult<()> {
27+
let name = match opts.krate {
28+
Some(ref name) => name.clone(),
29+
None => {
30+
let manifest_path = find_root_manifest_for_wd(config.cwd())?;
31+
let ws = Workspace::new(&manifest_path, config)?;
32+
ws.current()?.package_id().name().to_string()
33+
}
34+
};
35+
36+
let mutation = auth::Mutation::Owners { name: &name };
37+
38+
let (mut registry, _) = super::registry(
39+
config,
40+
opts.token.as_ref().map(Secret::as_deref),
41+
opts.index.as_deref(),
42+
opts.registry.as_deref(),
43+
true,
44+
Some(mutation),
45+
)?;
46+
47+
if let Some(ref v) = opts.to_add {
48+
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
49+
let msg = registry.add_owners(&name, &v).with_context(|| {
50+
format!(
51+
"failed to invite owners to crate `{}` on registry at {}",
52+
name,
53+
registry.host()
54+
)
55+
})?;
56+
57+
config.shell().status("Owner", msg)?;
58+
}
59+
60+
if let Some(ref v) = opts.to_remove {
61+
let v = v.iter().map(|s| &s[..]).collect::<Vec<_>>();
62+
config
63+
.shell()
64+
.status("Owner", format!("removing {:?} from crate {}", v, name))?;
65+
registry.remove_owners(&name, &v).with_context(|| {
66+
format!(
67+
"failed to remove owners from crate `{}` on registry at {}",
68+
name,
69+
registry.host()
70+
)
71+
})?;
72+
}
73+
74+
if opts.list {
75+
let owners = registry.list_owners(&name).with_context(|| {
76+
format!(
77+
"failed to list owners of crate `{}` on registry at {}",
78+
name,
79+
registry.host()
80+
)
81+
})?;
82+
for owner in owners.iter() {
83+
drop_print!(config, "{}", owner.login);
84+
match (owner.name.as_ref(), owner.email.as_ref()) {
85+
(Some(name), Some(email)) => drop_println!(config, " ({} <{}>)", name, email),
86+
(Some(s), None) | (None, Some(s)) => drop_println!(config, " ({})", s),
87+
(None, None) => drop_println!(config),
88+
}
89+
}
90+
}
91+
92+
Ok(())
93+
}

0 commit comments

Comments
 (0)