Skip to content

Commit

Permalink
Better error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
m1guelpf committed Dec 14, 2022
1 parent 0c686b5 commit 458a701
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 19 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ Cargo.lock
**/*.rs.bk

.DS_Store
.tmp.sh
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ serde_json = { version = "1.0.89", default-features = false }
clap = { version = "4.0.29", features = ["derive"] }
reqwest = { version = "0.11.13", default-features = false, features = ["json", "blocking", "native-tls-crate", "default-tls", "hyper-tls", "__tls"] }
bat = { version = "0.22.1", default-features = false, features = ["regex-onig"] }
colored = "2.0.0"
10 changes: 3 additions & 7 deletions install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,28 @@ set -e

main() {
BIN_DIR=${BIN_DIR-"$HOME/.bin"}
mkdir -p $BIN_DIR
mkdir -p "$BIN_DIR"

case $SHELL in
*/zsh)

This comment has been minimized.

Copy link
@majdsabik

majdsabik Dec 14, 2022

Hey. Shouldn't this be only zsh? In my case it only worked when I got rid of the */ in front of it. The value of $SHELL is "zsh", yet the script always reached the echo "could not detect shell, manually add ${BIN_DIR} to your PATH." line.

PROFILE=$HOME/.zshrc
PREF_SHELL=zsh
;;
*/bash)
PROFILE=$HOME/.bashrc
PREF_SHELL=bash
;;
*/fish)
PROFILE=$HOME/.config/fish/config.fish
PREF_SHELL=fish
;;
*/ash)
PROFILE=$HOME/.profile
PREF_SHELL=ash
;;
*)
echo "could not detect shell, manually add ${BIN_DIR} to your PATH."
exit 1
esac

if [[ ":$PATH:" != *":${BIN_DIR}:"* ]]; then
echo >> $PROFILE && echo "export PATH=\"\$PATH:$BIN_DIR\"" >> $PROFILE
echo >> "$PROFILE" && echo "export PATH=\"\$PATH:$BIN_DIR\"" >> "$PROFILE"
fi

PLATFORM="$(uname -s)"
Expand Down Expand Up @@ -59,7 +55,7 @@ main() {
fi

BINARY_URL="https://github.com/m1guelpf/plz-cli/releases/latest/download/plz-${PLATFORM}-${ARCHITECTURE}"
echo $BINARY_URL
echo "$BINARY_URL"

echo "downloading latest binary"
ensure curl -L "$BINARY_URL" -o "$BIN_DIR/plz"
Expand Down
51 changes: 39 additions & 12 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use bat::PrettyPrinter;
use clap::Parser;
use colored::*;
use question::{Answer, Question};
use reqwest::blocking::Client;
use serde_json::json;
Expand All @@ -20,7 +21,7 @@ struct Cli {
fn main() {
let cli = Cli::parse();
let api_key = env::var("OPENAI_API_KEY").unwrap_or_else(|_| {
println!("This program requires an OpenAI API key to run. Please set the OPENAI_API_KEY environment variable.");
println!("{}", "This program requires an OpenAI API key to run. Please set the OPENAI_API_KEY environment variable.".red());
std::process::exit(1);
});

Expand All @@ -45,8 +46,10 @@ fn main() {
.error_for_status()
.unwrap_or_else(|_| {
spinner.stop_and_persist(
"✖",
"Failed to get a response. Have you set the OPENAI_API_KEY variable?".into(),
"✖".red().to_string().as_str(),
"Failed to get a response. Have you set the OPENAI_API_KEY variable?"
.red()
.to_string(),
);
std::process::exit(1);
});
Expand All @@ -56,7 +59,10 @@ fn main() {
.unwrap()
.to_string();

spinner.stop_and_persist("✔", "Got some code!".into());
spinner.stop_and_persist(
"✔".green().to_string().as_str(),
"Got some code!".green().to_string(),
);

PrettyPrinter::new()
.input_from_bytes(text.trim().as_bytes())
Expand All @@ -70,27 +76,48 @@ fn main() {

let mut should_run = true;
if !cli.force {
should_run = Question::new("\x1b[90m>> Run the generated program? [Y/n]\x1b[0m")
.yes_no()
.until_acceptable()
.default(Answer::YES)
.ask()
.expect("Couldn't ask question.")
should_run = Question::new(
">> Run the generated program? [Y/n]"
.bright_black()
.to_string()
.as_str(),
)
.yes_no()
.until_acceptable()
.default(Answer::YES)
.ask()
.expect("Couldn't ask question.")
== Answer::YES;
}

if should_run {
spinner = Spinner::new(Spinners::BouncingBar, "Executing...".into());

// run command and print output and error
let output = Command::new("bash")
.arg(".tmp.sh")
.output()
.unwrap_or_else(|_| {
spinner.stop_and_persist("✖", "Failed to execute the generated program.".into());
spinner.stop_and_persist(
"✖".red().to_string().as_str(),
"Failed to execute the generated program.".red().to_string(),
);
std::process::exit(1);
});

spinner.stop_and_persist("✔", "Command ran successfully".into());
if !output.status.success() {
spinner.stop_and_persist(
"✖".red().to_string().as_str(),
"The program threw an error.".red().to_string(),
);
println!("{}", String::from_utf8_lossy(&output.stderr));
std::process::exit(1);
}

spinner.stop_and_persist(
"✔".green().to_string().as_str(),
"Command ran successfully".green().to_string(),
);

println!("{}", String::from_utf8_lossy(&output.stdout));
}
Expand Down

0 comments on commit 458a701

Please sign in to comment.