From e488f3b66c1e8f4c9aea1d43ceaaafc67d33d3aa Mon Sep 17 00:00:00 2001 From: Stephen Leitnick Date: Tue, 26 Jul 2022 15:04:58 -0400 Subject: [PATCH] Propagate errors --- Cargo.lock | 1 + Cargo.toml | 1 + src/cli/experience_cli.rs | 9 ++++----- src/cli/messaging_cli.rs | 9 ++++----- src/cli/mod.rs | 2 +- src/main.rs | 15 +++++++++++++-- src/util.rs | 18 ------------------ 7 files changed, 24 insertions(+), 31 deletions(-) delete mode 100644 src/util.rs diff --git a/Cargo.lock b/Cargo.lock index 429c07d..e46b6b8 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -607,6 +607,7 @@ version = "0.1.0" dependencies = [ "anyhow", "clap", + "log", "reqwest", "serde", "serde_json", diff --git a/Cargo.toml b/Cargo.toml index 25882d6..0dff881 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,7 @@ authors = ["Stephen Leitnick"] [dependencies] anyhow = "1.0.58" clap = { version = "3.2.14", features = ["derive"] } +log = "0.4.17" reqwest = { version = "0.11.11", features = ["json"] } serde = { version = "1.0.140", features = ["derive"] } serde_json = "1.0.82" diff --git a/src/cli/experience_cli.rs b/src/cli/experience_cli.rs index dd13abd..ef224dc 100644 --- a/src/cli/experience_cli.rs +++ b/src/cli/experience_cli.rs @@ -1,6 +1,6 @@ use clap::{Subcommand, ValueEnum, Args}; -use crate::{rbx::{RbxCloud, PublishVersionType}, util::{print_success, print_error}}; +use crate::rbx::{RbxCloud, PublishVersionType}; #[derive(Debug, Subcommand)] pub enum ExperienceCommands { @@ -40,7 +40,7 @@ pub enum VersionType { } impl Experience { - pub async fn run(self) -> anyhow::Result<()> { + pub async fn run(self) -> anyhow::Result> { match self.command { ExperienceCommands::Publish {place_id,universe_id,version_type,api_key, filename } => { let rbx_cloud = RbxCloud::new(api_key, universe_id); @@ -51,14 +51,13 @@ impl Experience { let res = rbx_cloud.experience(place_id).publish(&filename, publish_version_type).await; match res { Ok(body) => { - print_success(format!("{:?} {}/{} with version number {}", version_type, universe_id, place_id, body.version_number).to_lowercase()); + Ok(Some(format!("{:?} {}/{} with version number {}", version_type, universe_id, place_id, body.version_number).to_lowercase())) } Err(err) => { - print_error(format!("{}", err.to_string())); + Err(err) } } } } - Ok(()) } } diff --git a/src/cli/messaging_cli.rs b/src/cli/messaging_cli.rs index 09888aa..f8d420f 100644 --- a/src/cli/messaging_cli.rs +++ b/src/cli/messaging_cli.rs @@ -1,6 +1,6 @@ use clap::{Subcommand, Args}; -use crate::{rbx::RbxCloud, util::{print_success, print_error}}; +use crate::rbx::RbxCloud; #[derive(Debug, Subcommand)] pub enum MessagingCommands { @@ -30,7 +30,7 @@ pub struct Messaging { } impl Messaging { - pub async fn run(self) -> anyhow::Result<()> { + pub async fn run(self) -> anyhow::Result> { match self.command { MessagingCommands::Publish { topic, message, universe_id, api_key } => { let rbx_cloud = RbxCloud::new(api_key, universe_id); @@ -38,14 +38,13 @@ impl Messaging { let res = messaging.publish(message).await; match res { Ok(()) => { - print_success(format!("published message to topic {}", topic)); + Ok(Some(format!("published message to topic {}", topic))) } Err(err) => { - print_error(format!("{}", err.to_string())); + Err(err) } } } } - Ok(()) } } diff --git a/src/cli/mod.rs b/src/cli/mod.rs index 808c51e..b09d777 100644 --- a/src/cli/mod.rs +++ b/src/cli/mod.rs @@ -13,7 +13,7 @@ pub struct Cli { } impl Cli { - pub async fn run(self) -> anyhow::Result<()> { + pub async fn run(self) -> anyhow::Result> { match self.command { Command::Experience(command) => command.run().await, Command::Messaging(command) => command.run().await, diff --git a/src/main.rs b/src/main.rs index bf5b56a..0a35e32 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,7 @@ mod rbx; mod cli; -mod util; + +use std::process; use clap::Parser; use cli::Cli; @@ -8,5 +9,15 @@ use cli::Cli; #[tokio::main] async fn main() { let args = Cli::parse(); - args.run().await.unwrap(); + match args.run().await { + Ok(str) => { + if let Some(s) = str { + log::info!("{}", s); + } + } + Err(err) => { + log::error!("{:?}", err); + process::exit(1); + } + } } diff --git a/src/util.rs b/src/util.rs deleted file mode 100644 index c7d69a9..0000000 --- a/src/util.rs +++ /dev/null @@ -1,18 +0,0 @@ -use std::io::Write; -use termcolor::{StandardStream, ColorSpec, Color, WriteColor}; - -pub fn print_error(msg: String) { - let mut stdout = StandardStream::stdout(termcolor::ColorChoice::Auto); - stdout.set_color(ColorSpec::new().set_fg(Some(Color::Red)).set_bold(true)).unwrap(); - write!(&mut stdout, "error: ").unwrap(); - stdout.reset().unwrap(); - write!(&mut stdout, "{}", msg).unwrap(); -} - -pub fn print_success(msg: String) { - let mut stdout = StandardStream::stdout(termcolor::ColorChoice::Auto); - stdout.set_color(ColorSpec::new().set_fg(Some(Color::Green)).set_bold(true)).unwrap(); - write!(&mut stdout, "success: ").unwrap(); - stdout.reset().unwrap(); - write!(&mut stdout, "{}", msg).unwrap(); -}