Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

Commit

Permalink
config: use stdin instead of arguments
Browse files Browse the repository at this point in the history
This changes the way `wrangler config` works, previously both the email
and the api_key were passed as arguments which would be captured by the
terminal history.

When launching the command it will prompt for an email, once stdin
receive a line it will prompt for the api_key. Both lines are values and
will be stored in the config.
  • Loading branch information
xtuc committed Jul 2, 2019
1 parent c1a9808 commit d25d65b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 21 deletions.
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ number_prefix = "0.3.0"
flate2 = "1.0.7"
base64 = "0.10.1"
lazy_static = "1.3.0"
text_io = "0.1.7"

[dev-dependencies]
assert_cmd = "0.11.1"
Expand Down
31 changes: 10 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
#![allow(clippy::redundant_closure)]

#[macro_use]
extern crate text_io;

use std::env;
use std::str::FromStr;

Expand Down Expand Up @@ -122,19 +125,7 @@ fn main() -> Result<(), failure::Error> {
.about(&*format!(
"{} Setup wrangler with your Cloudflare account",
emoji::SLEUTH
))
.arg(
Arg::with_name("email")
.help("the email address associated with your Cloudflare account")
.index(1)
.required(true),
)
.arg(
Arg::with_name("api-key")
.help("your Cloudflare API key")
.index(2)
.required(true),
),
)),
)
.subcommand(
SubCommand::with_name("subdomain")
Expand All @@ -156,14 +147,12 @@ fn main() -> Result<(), failure::Error> {
.get_matches();

if let Some(matches) = matches.subcommand_matches("config") {
let email = matches
.value_of("email")
.expect("An email address must be provided.");
let api_key = matches
.value_of("api-key")
.expect("An API key must be provided.");

commands::global_config(email, api_key)?;
println!("Enter email: ");
let email: String = read!("{}\n");
println!("Enter api key: ");
let api_key: String = read!("{}\n");

commands::global_config(&email, &api_key)?;
} else if let Some(matches) = matches.subcommand_matches("generate") {
let name = matches.value_of("name").unwrap_or("worker");
let project_type = match matches.value_of("type") {
Expand Down
51 changes: 51 additions & 0 deletions tests/config.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use assert_cmd::prelude::*;
use std::env;
use std::fs;
use std::io::prelude::*;
use std::process::{Child, Command, Stdio};

#[test]
fn it_generates_the_config() {
let fake_home_dir = env::current_dir()
.expect("could not retrieve cwd")
.join(".it_generates_the_config");
let cmd = config_with_home(fake_home_dir.to_str().unwrap());
let mut stdin = cmd.stdin.unwrap();

write!(stdin, "a\n").unwrap(); // email
write!(stdin, "b\n").unwrap(); // api_key

let mut buffer = "".to_string();
let mut stdout = cmd.stdout.unwrap();
stdout
.read_to_string(&mut buffer)
.expect("could not read output");
assert!(buffer.contains("Enter email: \nEnter api key: \n Successfully configured."));

let config_file = fake_home_dir
.join(".wrangler")
.join("config")
.join("default.toml");

let config = fs::read_to_string(&config_file)
.expect(&format!("could not read config at {:?}", &config_file));
assert_eq!(
config,
r#"email = "a"
api_key = "b"
"#
);

fs::remove_dir_all(&fake_home_dir).expect("could not delete dir");
}

fn config_with_home(home_dir: &str) -> Child {
let mut wrangler = Command::cargo_bin(env!("CARGO_PKG_NAME")).unwrap();
wrangler
.arg("config")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.env("HOME", home_dir)
.spawn()
.unwrap()
}

0 comments on commit d25d65b

Please sign in to comment.