login: Send arguments separately to npm

All arguments were passed to `arg` inside one string when building a
`Command`. However, using the `arg` function, "only one argument can be
passed per use." This caused all arguments accidentally to be appended
to the registry URL. For example: After a successful login with a
provided `--auth_type`, the success message incorrectly displayed:
"Logged in as asf on https://registry.npmjs.org/%20--auth_type=Basic."
The space (%20 in hex) was caused by adding a fixed space before each
additional argument.

This commit pushes all arguments onto a `Vec<String>`. Then, the `args`
function adds the arguments separately to the command. This removes the
need to prepend spaces to each argument. Alternatively, `arg` could have
been used throughout to build the command argument-by-argument. However,
using `args` partitions the code more neatly into two distinct sections.
master
Dan Wilhelm 6 years ago
parent 0147dae9fe
commit 2343e97e1e
  1. 12
      src/npm.rs

@ -46,26 +46,24 @@ pub fn npm_login(
always_auth: bool, always_auth: bool,
auth_type: &Option<String>, auth_type: &Option<String>,
) -> Result<(), failure::Error> { ) -> Result<(), failure::Error> {
let mut args = String::new(); let mut args = vec![format!("login"), format!("--registry={}", registry)];
args.push_str(&format!("--registry={}", registry));
if let Some(scope) = scope { if let Some(scope) = scope {
args.push_str(&format!(" --scope={}", scope)); args.push(format!("--scope={}", scope));
} }
if always_auth == true { if always_auth == true {
args.push_str(" --always_auth"); args.push(format!("--always_auth"));
} }
if let Some(auth_type) = auth_type { if let Some(auth_type) = auth_type {
args.push_str(&format!(" --auth_type={}", auth_type)); args.push(format!("--auth_type={}", auth_type));
} }
// Interactively ask user for npm login info. // Interactively ask user for npm login info.
// (child::run does not support interactive input) // (child::run does not support interactive input)
let mut cmd = Command::new("npm"); let mut cmd = Command::new("npm");
cmd.arg("login").arg(args); cmd.args(args);
info!(log, "Running {:?}", cmd); info!(log, "Running {:?}", cmd);
match cmd.status()?.success() { match cmd.status()?.success() {

Loading…
Cancel
Save