Skip to content

Commit

Permalink
Improve bin UX
Browse files Browse the repository at this point in the history
  • Loading branch information
pawurb committed Jan 26, 2024
1 parent 6a62826 commit 53b45cd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ license = "MIT"
name = "pg-extras"
readme = "README.md"
repository = "https://github.com/pawurb/rust-pg-extras"
version = "0.3.0"
version = "0.3.1"

exclude = [
"docker-compose.yml.sample",
Expand Down
23 changes: 18 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,25 @@ use std::env;
use thiserror::Error;

#[tokio::main]
async fn main() -> Result<(), PgExtrasCmdError> {
async fn main() {
let args: Vec<String> = env::args().collect();
let command = if args.len() > 1 {
Some(args[1].clone())
} else {
None
};

match execute(command).await {
Ok(_) => {}
Err(error) => {
println!("{}", error);
}
}
}

async fn execute(command: Option<String>) -> Result<(), PgExtrasCmdError> {
if let Some(command) = command {
match &command[..] {
match command.as_str() {
"all_locks" => {
render_table(all_locks().await?);
}
Expand Down Expand Up @@ -113,9 +122,11 @@ async fn main() -> Result<(), PgExtrasCmdError> {
render_table(vacuum_stats().await?);
}
_ => {
return Err(PgExtrasCmdError::UnknownCommand(command));
return Err(PgExtrasCmdError::UnknownCommand(command.to_string()));
}
}
} else {
return Err(PgExtrasCmdError::MissingCommand);
}

Ok(())
Expand All @@ -124,9 +135,11 @@ async fn main() -> Result<(), PgExtrasCmdError> {
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum PgExtrasCmdError {
#[error("Unknown command")]
#[error("Unknown command '{0}'. Check https://github.com/pawurb/rust-pg-extras for list of available commands.")]
UnknownCommand(String),
#[error("Unknown pg-extras error")]
#[error("Missing command. Check https://github.com/pawurb/rust-pg-extras for list of available commands.")]
MissingCommand,
#[error("{0}")]
Other(PgExtrasError),
}

Expand Down

0 comments on commit 53b45cd

Please sign in to comment.