Improve error handling
This commit is contained in:
parent
d296105443
commit
0449580b9b
1 changed files with 22 additions and 18 deletions
40
src/main.rs
40
src/main.rs
|
@ -1,9 +1,11 @@
|
||||||
use std::{io::Read, path::PathBuf};
|
use std::io::Read;
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use clap::Parser;
|
use clap::Parser;
|
||||||
use color_eyre::Report;
|
use color_eyre::{eyre::WrapErr, Result};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
use tracing::{debug, error};
|
use std::fmt::Display;
|
||||||
|
use tracing::debug;
|
||||||
use tracing_subscriber::EnvFilter;
|
use tracing_subscriber::EnvFilter;
|
||||||
|
|
||||||
/// Bert
|
/// Bert
|
||||||
|
@ -46,13 +48,18 @@ enum Command {
|
||||||
/// Lower volume
|
/// Lower volume
|
||||||
VolumeDown,
|
VolumeDown,
|
||||||
}
|
}
|
||||||
fn main() {
|
|
||||||
if let Err(e) = do_main() {
|
impl Display for Command {
|
||||||
error!("Fatal error: {}", e);
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Command::PlayPause => write!(f, "Play/Pause"),
|
||||||
|
Command::VolumeUp => write!(f, "Volume Up"),
|
||||||
|
Command::VolumeDown => write!(f, "Volume Down"),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn do_main() -> Result<(), Box<dyn std::error::Error>> {
|
fn main() -> Result<()> {
|
||||||
let opts = Opts::parse();
|
let opts = Opts::parse();
|
||||||
|
|
||||||
let Opts {
|
let Opts {
|
||||||
|
@ -65,7 +72,7 @@ fn do_main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
token_file,
|
token_file,
|
||||||
} = opts;
|
} = opts;
|
||||||
|
|
||||||
setup(debug)?;
|
setup(debug).wrap_err("Setup failed")?;
|
||||||
|
|
||||||
let token = if let Some(token) = token {
|
let token = if let Some(token) = token {
|
||||||
Some(token)
|
Some(token)
|
||||||
|
@ -84,16 +91,18 @@ fn do_main() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
|
|
||||||
if let Some(token) = token {
|
if let Some(token) = token {
|
||||||
if let Some(cmd) = cmd {
|
if let Some(cmd) = cmd {
|
||||||
call_service(cmd, host, entity, insecure, token)?;
|
call_service(cmd, host, entity, insecure, token)
|
||||||
|
.wrap_err_with(|| format!("Unable to execute command {cmd}"))?;
|
||||||
} else {
|
} else {
|
||||||
get_now_playing(host, entity, insecure, token)?;
|
get_now_playing(host, entity, insecure, token)
|
||||||
|
.wrap_err("Unable to retrieve now playing info")?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// println!("{:#?}", response);
|
// println!("{:#?}", response);
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn setup(debug: bool) -> Result<(), Report> {
|
fn setup(debug: bool) -> Result<()> {
|
||||||
if debug {
|
if debug {
|
||||||
std::env::set_var("RUST_LOG", "debug");
|
std::env::set_var("RUST_LOG", "debug");
|
||||||
}
|
}
|
||||||
|
@ -106,12 +115,7 @@ fn setup(debug: bool) -> Result<(), Report> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_now_playing(
|
fn get_now_playing(host: String, entity: String, insecure: bool, token: String) -> Result<()> {
|
||||||
host: String,
|
|
||||||
entity: String,
|
|
||||||
insecure: bool,
|
|
||||||
token: String,
|
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
|
||||||
let url = format!(
|
let url = format!(
|
||||||
"{}://{}/api/states/{}",
|
"{}://{}/api/states/{}",
|
||||||
if insecure { "http" } else { "https" },
|
if insecure { "http" } else { "https" },
|
||||||
|
@ -174,7 +178,7 @@ fn call_service(
|
||||||
entity: String,
|
entity: String,
|
||||||
insecure: bool,
|
insecure: bool,
|
||||||
token: String,
|
token: String,
|
||||||
) -> Result<(), Box<dyn std::error::Error>> {
|
) -> Result<()> {
|
||||||
let cmd = match command {
|
let cmd = match command {
|
||||||
Command::PlayPause => "media_play_pause",
|
Command::PlayPause => "media_play_pause",
|
||||||
Command::VolumeUp => "volume_up",
|
Command::VolumeUp => "volume_up",
|
||||||
|
|
Loading…
Reference in a new issue