Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support login tokens for multiple registries #4680

Merged
merged 9 commits into from
Nov 1, 2017
34 changes: 23 additions & 11 deletions src/bin/login.rs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::io;
use cargo::ops;
use cargo::core::{SourceId, Source};
use cargo::sources::RegistrySource;
use cargo::util::{CliResult, CargoResultExt, Config};
use cargo::util::{CargoError, CliResult, CargoResultExt, Config};

#[derive(Deserialize)]
pub struct Options {
Expand All @@ -17,6 +17,7 @@ pub struct Options {
flag_locked: bool,
#[serde(rename = "flag_Z")]
flag_z: Vec<String>,
flag_registry: Option<String>,
}

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

";

Expand All @@ -44,26 +46,36 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
options.flag_frozen,
options.flag_locked,
&options.flag_z)?;
let token = match options.arg_token.clone() {

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

let token = match options.arg_token {
Some(token) => token,
None => {
let src = SourceId::crates_io(config)?;
let mut src = RegistrySource::remote(&src, config);
src.update()?;
let config = src.config()?.unwrap();
let host = options.flag_host.clone().unwrap_or(config.api.unwrap());
let host = match options.flag_registry {
Some(ref _registry) => {
return Err(CargoError::from("token must be provided when --registry is provided.").into());
}
None => {
let src = SourceId::crates_io(config)?;
let mut src = RegistrySource::remote(&src, config);
src.update()?;
let config = src.config()?.unwrap();
options.flag_host.clone().unwrap_or(config.api.unwrap())
}
};
println!("please visit {}me and paste the API Token below", host);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm I think this should only get printed for the crates.io registry, right? We may not want to assume the layout of another registry per se just yet, and instead we could probably just return an error if no token was passed and --registry was passed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was wondering if the /me API should be required for publish, I am happy to switch to just return an error stating that the token should be provided.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is one where we haven't done much to sketch out the path towards "what does a custom registry need HTTP-wise", so I think it's ok to err on the side of caution for now and just return an error

let mut line = String::new();
let input = io::stdin();
input.lock().read_line(&mut line).chain_err(|| {
"failed to read stdin"
})?;
line
line.trim().to_string()
}
};

let token = token.trim().to_string();
ops::registry_login(config, token)?;
ops::registry_login(config, token, options.flag_registry)?;
Ok(())
}

10 changes: 9 additions & 1 deletion src/bin/owner.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cargo::ops;
use cargo::util::{CliResult, Config};
use cargo::util::{CargoError, CliResult, Config};

#[derive(Deserialize)]
pub struct Options {
Expand All @@ -16,6 +16,7 @@ pub struct Options {
flag_locked: bool,
#[serde(rename = "flag_Z")]
flag_z: Vec<String>,
flag_registry: Option<String>,
}

pub const USAGE: &'static str = "
Expand All @@ -37,6 +38,7 @@ Options:
--frozen Require Cargo.lock and cache are up to date
--locked Require Cargo.lock is up to date
-Z FLAG ... Unstable (nightly-only) flags to Cargo
--registry REGISTRY Registry to use

This command will modify the owners for a package on the specified registry (or
default). Note that owners of a package can upload new versions, yank old
Expand All @@ -61,7 +63,13 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
to_add: options.flag_add,
to_remove: options.flag_remove,
list: options.flag_list,
registry: options.flag_registry,
};

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

ops::modify_owners(config, &opts)?;
Ok(())
}
Expand Down
9 changes: 8 additions & 1 deletion src/bin/publish.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use cargo::core::Workspace;
use cargo::ops;
use cargo::util::{CliResult, Config};
use cargo::util::{CargoError, CliResult, Config};
use cargo::util::important_paths::find_root_manifest_for_wd;

#[derive(Deserialize)]
Expand All @@ -21,6 +21,7 @@ pub struct Options {
flag_locked: bool,
#[serde(rename = "flag_Z")]
flag_z: Vec<String>,
flag_registry: Option<String>,
}

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

";

Expand All @@ -67,9 +69,13 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
flag_jobs: jobs,
flag_dry_run: dry_run,
flag_target: target,
flag_registry: registry,
..
} = options;

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

// TODO: Deprecated
// remove once it has been decided --host can be removed
Expand Down Expand Up @@ -100,6 +106,7 @@ about this warning.";
target: target.as_ref().map(|t| &t[..]),
jobs: jobs,
dry_run: dry_run,
registry: registry,
})?;
Ok(())
}
11 changes: 9 additions & 2 deletions src/bin/search.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cargo::ops;
use cargo::util::{CliResult, Config};
use cargo::util::{CargoError, CliResult, Config};

use std::cmp;

Expand All @@ -16,6 +16,7 @@ pub struct Options {
arg_query: Vec<String>,
#[serde(rename = "flag_Z")]
flag_z: Vec<String>,
flag_registry: Option<String>,
}

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

pub fn execute(options: Options, config: &mut Config) -> CliResult {
Expand All @@ -50,9 +52,14 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
flag_host: host, // TODO: Depricated, remove
flag_limit: limit,
arg_query: query,
flag_registry: registry,
..
} = options;

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

// TODO: Depricated
// remove once it has been decided --host can be safely removed
// We may instead want to repurpose the host flag, as
Expand All @@ -77,6 +84,6 @@ about this warning.";
host
};

ops::search(&query.join("+"), config, index, cmp::min(100, limit.unwrap_or(10)) as u8)?;
ops::search(&query.join("+"), config, index, cmp::min(100, limit.unwrap_or(10)) as u8, registry)?;
Ok(())
}
12 changes: 10 additions & 2 deletions src/bin/yank.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use cargo::ops;
use cargo::util::{CliResult, Config};
use cargo::util::{CargoError, CliResult, Config};

#[derive(Deserialize)]
pub struct Options {
Expand All @@ -15,6 +15,7 @@ pub struct Options {
flag_locked: bool,
#[serde(rename = "flag_Z")]
flag_z: Vec<String>,
flag_registry: Option<String>,
}

pub static USAGE: &'static str = "
Expand All @@ -35,6 +36,7 @@ Options:
--frozen Require Cargo.lock and cache are up to date
--locked Require Cargo.lock is up to date
-Z FLAG ... Unstable (nightly-only) flags to Cargo
--registry REGISTRY Registry to use

The yank command removes a previously pushed crate's version from the server's
index. This command does not delete any data, and the crate will still be
Expand All @@ -52,12 +54,18 @@ pub fn execute(options: Options, config: &mut Config) -> CliResult {
options.flag_frozen,
options.flag_locked,
&options.flag_z)?;

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

ops::yank(config,
options.arg_crate,
options.flag_vers,
options.flag_token,
options.flag_index,
options.flag_undo)?;
options.flag_undo,
options.flag_registry)?;
Ok(())
}

2 changes: 2 additions & 0 deletions src/cargo/core/features.rs
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ impl Features {
#[derive(Default, Debug)]
pub struct CliUnstable {
pub print_im_a_teapot: bool,
pub unstable_options: bool,
}

impl CliUnstable {
Expand Down Expand Up @@ -260,6 +261,7 @@ impl CliUnstable {

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

Expand Down
22 changes: 10 additions & 12 deletions src/cargo/core/source/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ impl SourceId {
/// This is the main cargo registry by default, but it can be overridden in
/// a `.cargo/config`.
pub fn crates_io(config: &Config) -> CargoResult<SourceId> {
let cfg = ops::registry_configuration(config)?;
let cfg = ops::registry_configuration(config, None)?;
let url = if let Some(ref index) = cfg.index {
static WARNED: AtomicBool = ATOMIC_BOOL_INIT;
if !WARNED.swap(true, SeqCst) {
Expand All @@ -183,17 +183,15 @@ impl SourceId {
}

pub fn alt_registry(config: &Config, key: &str) -> CargoResult<SourceId> {
if let Some(index) = config.get_string(&format!("registries.{}.index", key))? {
let url = index.val.to_url()?;
Ok(SourceId {
inner: Arc::new(SourceIdInner {
kind: Kind::Registry,
canonical_url: git::canonicalize_url(&url)?,
url: url,
precise: None,
}),
})
} else { Err(format!("No index found for registry: `{}`", key).into()) }
let url = config.get_registry_index(key)?;
Ok(SourceId {
inner: Arc::new(SourceIdInner {
kind: Kind::Registry,
canonical_url: git::canonicalize_url(&url)?,
url: url,
precise: None,
}),
})
}

/// Get this source URL
Expand Down
Loading