Skip to content

Commit 551b62b

Browse files
committed
Auto merge of #4680 - Metaswitch:registry-login, r=alexcrichton
Support login tokens for multiple registries This pull request builds on #4206 to support login using the the registry from alternate registries (RFC 2141). This includes the following changes: - allow credentials to be stored based on the registry - allow passing the registry to run cargo commands against using --registry Note that this does not include a feature gate on the use of --registry as the publish code blocks publish if we use any features. @alexcrichton, are you happy with this approach, or is there a way that you would recommend this should be relaxed for testing purposes?
2 parents a924c5a + 4ef8f55 commit 551b62b

File tree

12 files changed

+276
-91
lines changed

12 files changed

+276
-91
lines changed

src/bin/login.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::io;
44
use cargo::ops;
55
use cargo::core::{SourceId, Source};
66
use cargo::sources::RegistrySource;
7-
use cargo::util::{CliResult, CargoResultExt, Config};
7+
use cargo::util::{CargoError, CliResult, CargoResultExt, Config};
88

99
#[derive(Deserialize)]
1010
pub struct Options {
@@ -17,6 +17,7 @@ pub struct Options {
1717
flag_locked: bool,
1818
#[serde(rename = "flag_Z")]
1919
flag_z: Vec<String>,
20+
flag_registry: Option<String>,
2021
}
2122

2223
pub const USAGE: &'static str = "
@@ -34,6 +35,7 @@ Options:
3435
--frozen Require Cargo.lock and cache are up to date
3536
--locked Require Cargo.lock is up to date
3637
-Z FLAG ... Unstable (nightly-only) flags to Cargo
38+
--registry REGISTRY Registry to use
3739
3840
";
3941

@@ -44,26 +46,36 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
4446
options.flag_frozen,
4547
options.flag_locked,
4648
&options.flag_z)?;
47-
let token = match options.arg_token.clone() {
49+
50+
if options.flag_registry.is_some() && !config.cli_unstable().unstable_options {
51+
return Err(CargoError::from("registry option is an unstable feature and requires -Zunstable-options to use.").into());
52+
}
53+
54+
let token = match options.arg_token {
4855
Some(token) => token,
4956
None => {
50-
let src = SourceId::crates_io(config)?;
51-
let mut src = RegistrySource::remote(&src, config);
52-
src.update()?;
53-
let config = src.config()?.unwrap();
54-
let host = options.flag_host.clone().unwrap_or(config.api.unwrap());
57+
let host = match options.flag_registry {
58+
Some(ref _registry) => {
59+
return Err(CargoError::from("token must be provided when --registry is provided.").into());
60+
}
61+
None => {
62+
let src = SourceId::crates_io(config)?;
63+
let mut src = RegistrySource::remote(&src, config);
64+
src.update()?;
65+
let config = src.config()?.unwrap();
66+
options.flag_host.clone().unwrap_or(config.api.unwrap())
67+
}
68+
};
5569
println!("please visit {}me and paste the API Token below", host);
5670
let mut line = String::new();
5771
let input = io::stdin();
5872
input.lock().read_line(&mut line).chain_err(|| {
5973
"failed to read stdin"
6074
})?;
61-
line
75+
line.trim().to_string()
6276
}
6377
};
6478

65-
let token = token.trim().to_string();
66-
ops::registry_login(config, token)?;
79+
ops::registry_login(config, token, options.flag_registry)?;
6780
Ok(())
6881
}
69-

src/bin/owner.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cargo::ops;
2-
use cargo::util::{CliResult, Config};
2+
use cargo::util::{CargoError, CliResult, Config};
33

44
#[derive(Deserialize)]
55
pub struct Options {
@@ -16,6 +16,7 @@ pub struct Options {
1616
flag_locked: bool,
1717
#[serde(rename = "flag_Z")]
1818
flag_z: Vec<String>,
19+
flag_registry: Option<String>,
1920
}
2021

2122
pub const USAGE: &'static str = "
@@ -37,6 +38,7 @@ Options:
3738
--frozen Require Cargo.lock and cache are up to date
3839
--locked Require Cargo.lock is up to date
3940
-Z FLAG ... Unstable (nightly-only) flags to Cargo
41+
--registry REGISTRY Registry to use
4042
4143
This command will modify the owners for a package on the specified registry (or
4244
default). Note that owners of a package can upload new versions, yank old
@@ -61,7 +63,13 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
6163
to_add: options.flag_add,
6264
to_remove: options.flag_remove,
6365
list: options.flag_list,
66+
registry: options.flag_registry,
6467
};
68+
69+
if opts.registry.is_some() && !config.cli_unstable().unstable_options {
70+
return Err(CargoError::from("registry option is an unstable feature and requires -Zunstable-options to use.").into());
71+
}
72+
6573
ops::modify_owners(config, &opts)?;
6674
Ok(())
6775
}

src/bin/publish.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use cargo::core::Workspace;
22
use cargo::ops;
3-
use cargo::util::{CliResult, Config};
3+
use cargo::util::{CargoError, CliResult, Config};
44
use cargo::util::important_paths::find_root_manifest_for_wd;
55

66
#[derive(Deserialize)]
@@ -21,6 +21,7 @@ pub struct Options {
2121
flag_locked: bool,
2222
#[serde(rename = "flag_Z")]
2323
flag_z: Vec<String>,
24+
flag_registry: Option<String>,
2425
}
2526

2627
pub const USAGE: &'static str = "
@@ -46,6 +47,7 @@ Options:
4647
--frozen Require Cargo.lock and cache are up to date
4748
--locked Require Cargo.lock is up to date
4849
-Z FLAG ... Unstable (nightly-only) flags to Cargo
50+
--registry REGISTRY Registry to publish to
4951
5052
";
5153

@@ -67,9 +69,13 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
6769
flag_jobs: jobs,
6870
flag_dry_run: dry_run,
6971
flag_target: target,
72+
flag_registry: registry,
7073
..
7174
} = options;
7275

76+
if registry.is_some() && !config.cli_unstable().unstable_options {
77+
return Err(CargoError::from("registry option is an unstable feature and requires -Zunstable-options to use.").into());
78+
}
7379

7480
// TODO: Deprecated
7581
// remove once it has been decided --host can be removed
@@ -100,6 +106,7 @@ about this warning.";
100106
target: target.as_ref().map(|t| &t[..]),
101107
jobs: jobs,
102108
dry_run: dry_run,
109+
registry: registry,
103110
})?;
104111
Ok(())
105112
}

src/bin/search.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cargo::ops;
2-
use cargo::util::{CliResult, Config};
2+
use cargo::util::{CargoError, CliResult, Config};
33

44
use std::cmp;
55

@@ -16,6 +16,7 @@ pub struct Options {
1616
arg_query: Vec<String>,
1717
#[serde(rename = "flag_Z")]
1818
flag_z: Vec<String>,
19+
flag_registry: Option<String>,
1920
}
2021

2122
pub const USAGE: &'static str = "
@@ -36,6 +37,7 @@ Options:
3637
--frozen Require Cargo.lock and cache are up to date
3738
--locked Require Cargo.lock is up to date
3839
-Z FLAG ... Unstable (nightly-only) flags to Cargo
40+
--registry REGISTRY Registry to use
3941
";
4042

4143
pub fn execute(options: Options, config: &mut Config) -> CliResult {
@@ -50,9 +52,14 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
5052
flag_host: host, // TODO: Depricated, remove
5153
flag_limit: limit,
5254
arg_query: query,
55+
flag_registry: registry,
5356
..
5457
} = options;
5558

59+
if registry.is_some() && !config.cli_unstable().unstable_options {
60+
return Err(CargoError::from("registry option is an unstable feature and requires -Zunstable-options to use.").into());
61+
}
62+
5663
// TODO: Depricated
5764
// remove once it has been decided --host can be safely removed
5865
// We may instead want to repurpose the host flag, as
@@ -77,6 +84,6 @@ about this warning.";
7784
host
7885
};
7986

80-
ops::search(&query.join("+"), config, index, cmp::min(100, limit.unwrap_or(10)) as u8)?;
87+
ops::search(&query.join("+"), config, index, cmp::min(100, limit.unwrap_or(10)) as u8, registry)?;
8188
Ok(())
8289
}

src/bin/yank.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use cargo::ops;
2-
use cargo::util::{CliResult, Config};
2+
use cargo::util::{CargoError, CliResult, Config};
33

44
#[derive(Deserialize)]
55
pub struct Options {
@@ -15,6 +15,7 @@ pub struct Options {
1515
flag_locked: bool,
1616
#[serde(rename = "flag_Z")]
1717
flag_z: Vec<String>,
18+
flag_registry: Option<String>,
1819
}
1920

2021
pub static USAGE: &'static str = "
@@ -35,6 +36,7 @@ Options:
3536
--frozen Require Cargo.lock and cache are up to date
3637
--locked Require Cargo.lock is up to date
3738
-Z FLAG ... Unstable (nightly-only) flags to Cargo
39+
--registry REGISTRY Registry to use
3840
3941
The yank command removes a previously pushed crate's version from the server's
4042
index. This command does not delete any data, and the crate will still be
@@ -52,12 +54,18 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
5254
options.flag_frozen,
5355
options.flag_locked,
5456
&options.flag_z)?;
57+
58+
if options.flag_registry.is_some() && !config.cli_unstable().unstable_options {
59+
return Err(CargoError::from("registry option is an unstable feature and requires -Zunstable-options to use.").into());
60+
}
61+
5562
ops::yank(config,
5663
options.arg_crate,
5764
options.flag_vers,
5865
options.flag_token,
5966
options.flag_index,
60-
options.flag_undo)?;
67+
options.flag_undo,
68+
options.flag_registry)?;
6169
Ok(())
6270
}
6371

src/cargo/core/features.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ impl Features {
231231
#[derive(Default, Debug)]
232232
pub struct CliUnstable {
233233
pub print_im_a_teapot: bool,
234+
pub unstable_options: bool,
234235
}
235236

236237
impl CliUnstable {
@@ -260,6 +261,7 @@ impl CliUnstable {
260261

261262
match k {
262263
"print-im-a-teapot" => self.print_im_a_teapot = parse_bool(v)?,
264+
"unstable-options" => self.unstable_options = true,
263265
_ => bail!("unknown `-Z` flag specified: {}", k),
264266
}
265267

src/cargo/core/source/source_id.rs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ impl SourceId {
165165
/// This is the main cargo registry by default, but it can be overridden in
166166
/// a `.cargo/config`.
167167
pub fn crates_io(config: &Config) -> CargoResult<SourceId> {
168-
let cfg = ops::registry_configuration(config)?;
168+
let cfg = ops::registry_configuration(config, None)?;
169169
let url = if let Some(ref index) = cfg.index {
170170
static WARNED: AtomicBool = ATOMIC_BOOL_INIT;
171171
if !WARNED.swap(true, SeqCst) {
@@ -183,17 +183,15 @@ impl SourceId {
183183
}
184184

185185
pub fn alt_registry(config: &Config, key: &str) -> CargoResult<SourceId> {
186-
if let Some(index) = config.get_string(&format!("registries.{}.index", key))? {
187-
let url = index.val.to_url()?;
188-
Ok(SourceId {
189-
inner: Arc::new(SourceIdInner {
190-
kind: Kind::Registry,
191-
canonical_url: git::canonicalize_url(&url)?,
192-
url: url,
193-
precise: None,
194-
}),
195-
})
196-
} else { Err(format!("No index found for registry: `{}`", key).into()) }
186+
let url = config.get_registry_index(key)?;
187+
Ok(SourceId {
188+
inner: Arc::new(SourceIdInner {
189+
kind: Kind::Registry,
190+
canonical_url: git::canonicalize_url(&url)?,
191+
url: url,
192+
precise: None,
193+
}),
194+
})
197195
}
198196

199197
/// Get this source URL

0 commit comments

Comments
 (0)