feat: better error message for non-existent env value

This commit is contained in:
iff 2023-08-09 16:56:17 +02:00
parent a201c18c01
commit dc7f7e9c8d
2 changed files with 17 additions and 5 deletions

View file

@ -1,4 +1,4 @@
// pay-respect: Press F to correct your command // pay-respects: Press F to correct your command
// Copyright (C) 2023 iff // Copyright (C) 2023 iff
// This program is free software: you can redistribute it and/or modify // This program is free software: you can redistribute it and/or modify
@ -26,9 +26,14 @@ mod suggestions;
fn main() { fn main() {
args::handle_args(); args::handle_args();
let shell = std::env::var("_PR_SHELL").expect( let shell = match std::env::var("_PR_SHELL") {
"No _PR_SHELL in environment. Did you aliased the binary with the correct arguments?", Ok(shell) => shell,
); Err(_) => {
eprintln!("No _PR_SHELL in environment. Did you aliased the command with the correct argument?\n\nUse `pay-respects -h` for help");
std::process::exit(1);
}
};
let mut last_command = shell::last_command_expanded_alias(&shell); let mut last_command = shell::last_command_expanded_alias(&shell);
let mut error_msg = command_output(&shell, &last_command); let mut error_msg = command_output(&shell, &last_command);
loop { loop {

View file

@ -41,7 +41,14 @@ pub fn command_output(shell: &str, command: &str) -> String {
} }
fn last_command(shell: &str) -> String { fn last_command(shell: &str) -> String {
let last_command = std::env::var("_PR_LAST_COMMAND").expect("No _PR_LAST_COMMAND in environment. Did you aliased the command with the correct argument?"); let last_command = match std::env::var("_PR_LAST_COMMAND") {
Ok(command) => command,
Err(_) => {
eprintln!("No _PR_LAST_COMMAND in environment. Did you aliased the command with the correct argument?\n\nUse `pay-respects -h` for help");
exit(1);
}
};
match shell { match shell {
"bash" => { "bash" => {
let first_line = last_command.lines().next().unwrap().trim(); let first_line = last_command.lines().next().unwrap().trim();