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

Add support for a default registry for cargo commands #6135

Merged
merged 4 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/bin/cargo/command_prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ pub trait ArgMatchesExt {
self.value_of_path("path", config).unwrap(),
self._value_of("name").map(|s| s.to_string()),
self._value_of("edition").map(|s| s.to_string()),
self.registry(config)?,
)
}

Expand All @@ -359,9 +360,22 @@ pub trait ArgMatchesExt {
requires -Zunstable-options to use."
));
}
Ok(Some(registry.to_string()))

if registry == "crates.io" {
Copy link
Member

Choose a reason for hiding this comment

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

I think this may be one of the few places that we accept this string, right? If so, could the special casing not be done here and perhaps later in a more centralized location?

Copy link
Member

Choose a reason for hiding this comment

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

Small nit - crates-io is used rather than crates.io in [patch] blocks so it might make sense to use the same here for consistency.

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 think this may be one of the few places that we accept this string, right? If so, could the special casing not be done here and perhaps later in a more centralized location?

I have made the string a constant (and updated other uses of it), so it is now just doing a string comparison, I did consider moving the logic into source_id, but we don't always need the source (for example in publish), so I have left it where it was. Is that ok?

// If "crates.io" is specified then we just need to return None
// as that will cause cargo to use crates.io. This is required
// for the case where a default alterative registry is used
// but the user wants to switch back to crates.io for a single
// command.
Ok(None)
}
else {
Ok(Some(registry.to_string()))
}
}
None => {
config.default_registry()
}
None => Ok(None),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn cli() -> App {
subcommand("init")
.about("Create a new cargo package in an existing directory")
.arg(Arg::with_name("path").default_value("."))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_new_opts()
}

Expand Down
1 change: 1 addition & 0 deletions src/bin/cargo/commands/new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub fn cli() -> App {
subcommand("new")
.about("Create a new cargo package at <path>")
.arg(Arg::with_name("path").required(true))
.arg(opt("registry", "Registry to use").value_name("REGISTRY"))
.arg_new_opts()
}

Expand Down
11 changes: 11 additions & 0 deletions src/cargo/ops/cargo_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub struct NewOptions {
pub path: PathBuf,
pub name: Option<String>,
pub edition: Option<String>,
pub registry: Option<String>,
}

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -67,6 +68,7 @@ struct MkOptions<'a> {
source_files: Vec<SourceFileInformation>,
bin: bool,
edition: Option<&'a str>,
registry: Option<&'a str>,
}

impl NewOptions {
Expand All @@ -77,6 +79,7 @@ impl NewOptions {
path: PathBuf,
name: Option<String>,
edition: Option<String>,
registry: Option<String>,
) -> CargoResult<NewOptions> {
let kind = match (bin, lib) {
(true, true) => bail!("can't specify both lib and binary outputs"),
Expand All @@ -91,6 +94,7 @@ impl NewOptions {
path,
name,
edition,
registry,
};
Ok(opts)
}
Expand Down Expand Up @@ -326,6 +330,7 @@ pub fn new(opts: &NewOptions, config: &Config) -> CargoResult<()> {
source_files: vec![plan_new_source_file(opts.kind.is_bin(), name.to_string())],
bin: opts.kind.is_bin(),
edition: opts.edition.as_ref().map(|s| &**s),
registry: opts.registry.as_ref().map(|s| &**s),
};

mk(config, &mkopts).chain_err(|| {
Expand Down Expand Up @@ -403,6 +408,7 @@ pub fn init(opts: &NewOptions, config: &Config) -> CargoResult<()> {
bin: src_paths_types.iter().any(|x| x.bin),
source_files: src_paths_types,
edition: opts.edition.as_ref().map(|s| &**s),
registry: opts.registry.as_ref().map(|s| &**s),
};

mk(config, &mkopts).chain_err(|| {
Expand Down Expand Up @@ -537,6 +543,7 @@ name = "{}"
version = "0.1.0"
authors = [{}]
edition = {}
publish = {}
cswindle marked this conversation as resolved.
Show resolved Hide resolved

[dependencies]
{}"#,
Expand All @@ -546,6 +553,10 @@ edition = {}
Some(edition) => toml::Value::String(edition.to_string()),
None => toml::Value::String("2018".to_string()),
},
match opts.registry {
Some(registry) => toml::Value::Array(vec!(toml::Value::String(registry.to_string()))),
None => toml::Value::Boolean(true),
},
cargotoml_path_specifier
).as_bytes(),
)?;
Expand Down
10 changes: 10 additions & 0 deletions src/cargo/util/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,16 @@ impl Config {
self.home_path.join("registry").join("src")
}

/// The default cargo registry (`alternative-registry`)
pub fn default_registry(&self) -> CargoResult<Option<String>> {
Ok(
match self.get_string("registry.default")? {
Some(registry) => Some(registry.val),
None => None,
}
)
}

/// Get a reference to the shell, for e.g. writing error messages
pub fn shell(&self) -> RefMut<Shell> {
self.shell.borrow_mut()
Expand Down
1 change: 1 addition & 0 deletions src/doc/src/reference/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ runner = ".."
[registry]
index = "..." # URL of the registry index (defaults to the central repository)
token = "..." # Access token (found on the central repo’s website)
default = "..." # Default alternative registry to use (can be overriden with --registry)

[http]
proxy = "host:port" # HTTP proxy to use for HTTP requests (defaults to none)
Expand Down