Skip to content

Commit

Permalink
simplify heroku cli check by isolating Command creation
Browse files Browse the repository at this point in the history
  • Loading branch information
locks committed Oct 12, 2021
1 parent 704ab92 commit 0f1dc36
Showing 1 changed file with 16 additions and 43 deletions.
59 changes: 16 additions & 43 deletions src/utils/heroku.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,11 @@
use super::prompt::automated;

pub fn get_env_vars(project: &str) -> Vec<(String, String)> {
if cfg!(windows) {
check_heroku_cli_windows();
} else {
check_heroku_cli();
}
check_heroku_cli();

crate::utils::prompt::automated("Fetching env vars from heroku");

let heroku_vars = std::process::Command::new("heroku")
// .current_dir(&dir)
.arg("config")
.arg("-s")
.args(&["-a", project])
Expand All @@ -29,45 +24,11 @@ pub fn get_env_vars(project: &str) -> Vec<(String, String)> {
res
}

// Checks if heroku-cli is installed, and then checks if user is logged in.
// I was getting bogged down on building up the command according to the platform, so...
fn check_heroku_cli_windows() {
automated("Checking heroku-cli");

if std::process::Command::new("cmd")
.args(&["/C", "heroku"])
.stdout(std::process::Stdio::null())
.status()
.is_err()
{
eprintln!("heroku-cli not found. Please install and try again: https://devcenter.heroku.com/articles/heroku-cli");
std::process::exit(1);
}

if !std::process::Command::new("cmd")
.args(&["/C", "heroku", "auth:whoami"])
.status()
.expect("Could not confirm login")
.success()
{
let status = std::process::Command::new("cmd")
.args(&["/C", "heroku", "login"])
.spawn()
.expect("Could not log in user.")
.wait()
.expect("??");

if !status.success() {
std::process::exit(1);
}
}
}

// Checks if heroku-cli is installed, and then checks if user is logged in.
fn check_heroku_cli() {
automated("Checking heroku-cli");

if std::process::Command::new("heroku")
if heroku_cmd()
.stdout(std::process::Stdio::null())
.status()
.is_err()
Expand All @@ -76,13 +37,13 @@ fn check_heroku_cli() {
std::process::exit(1);
}

if !std::process::Command::new("heroku")
if !heroku_cmd()
.arg("auth:whoami")
.status()
.expect("Could not confirm login")
.success()
{
let status = std::process::Command::new("heroku")
let status = heroku_cmd()
.arg("login")
.spawn()
.expect("Could not log in user.")
Expand All @@ -94,3 +55,15 @@ fn check_heroku_cli() {
}
}
}

#[cfg(windows)]
fn heroku_cmd() -> std::process::Command {
let mut cmd = std::process::Command::new("cmd");
cmd.args(&["/C", "heroku"]);
cmd
}

#[cfg(not(windows))]
fn heroku_cmd() -> std::process::Command {
std::process::Command::new("heroku")
}

0 comments on commit 0f1dc36

Please sign in to comment.