error if add cmd is invoked without files

This commit is contained in:
raf 2024-12-12 22:16:27 +03:00
commit fe89860f5b
Signed by: NotAShelf
GPG key ID: AF26552424E53993

View file

@ -2,10 +2,9 @@ use clap::{Parser, Subcommand};
use serde_json::json; use serde_json::json;
use std::io::{self}; use std::io::{self};
use std::path::PathBuf; use std::path::PathBuf;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::UnixStream; use tokio::net::UnixStream;
use tokio::io::{AsyncWriteExt, AsyncReadExt}; use tracing::{debug, error, info};
use tracing::{debug, info, error};
use tracing_subscriber;
#[derive(Parser)] #[derive(Parser)]
#[command(author, version, about)] #[command(author, version, about)]
@ -35,8 +34,14 @@ enum CommandOptions {
} }
const SOCKET_PATH: &str = "/tmp/mpvsocket"; const SOCKET_PATH: &str = "/tmp/mpvsocket";
async fn send_ipc_command(command: &str, args: &[serde_json::Value]) -> io::Result<Option<serde_json::Value>> { async fn send_ipc_command(
debug!("Sending IPC command: {} with arguments: {:?}", command, args); command: &str,
args: &[serde_json::Value],
) -> io::Result<Option<serde_json::Value>> {
debug!(
"Sending IPC command: {} with arguments: {:?}",
command, args
);
match UnixStream::connect(SOCKET_PATH).await { match UnixStream::connect(SOCKET_PATH).await {
Ok(mut socket) => { Ok(mut socket) => {
@ -99,30 +104,37 @@ async fn main() -> io::Result<()> {
info!("Unpausing playback"); info!("Unpausing playback");
send_ipc_command("set_property", &[json!("pause"), json!(false)]).await?; send_ipc_command("set_property", &[json!("pause"), json!(false)]).await?;
} }
CommandOptions::Pause => { CommandOptions::Pause => {
info!("Pausing playback"); info!("Pausing playback");
send_ipc_command("set_property", &[json!("pause"), json!(true)]).await?; send_ipc_command("set_property", &[json!("pause"), json!(true)]).await?;
} }
CommandOptions::Stop => { CommandOptions::Stop => {
info!("Stopping playback and quitting MPV"); info!("Stopping playback and quitting MPV");
send_ipc_command("quit", &[]).await?; send_ipc_command("quit", &[]).await?;
} }
CommandOptions::Next => { CommandOptions::Next => {
info!("Skipping to next item in the playlist"); info!("Skipping to next item in the playlist");
send_ipc_command("playlist-next", &[]).await?; send_ipc_command("playlist-next", &[]).await?;
} }
CommandOptions::Prev => { CommandOptions::Prev => {
info!("Skipping to previous item in the playlist"); info!("Skipping to previous item in the playlist");
send_ipc_command("playlist-prev", &[]).await?; send_ipc_command("playlist-prev", &[]).await?;
} }
CommandOptions::Seek { seconds } => { CommandOptions::Seek { seconds } => {
info!("Seeking to {} seconds", seconds); info!("Seeking to {} seconds", seconds);
send_ipc_command("seek", &[json!(seconds)]).await?; send_ipc_command("seek", &[json!(seconds)]).await?;
} }
CommandOptions::Move { index1, index2 } => { CommandOptions::Move { index1, index2 } => {
info!("Moving item from index {} to {}", index1, index2); info!("Moving item from index {} to {}", index1, index2);
send_ipc_command("playlist-move", &[json!(index1), json!(index2)]).await?; send_ipc_command("playlist-move", &[json!(index1), json!(index2)]).await?;
} }
CommandOptions::Remove { index } => { CommandOptions::Remove { index } => {
if let Some(idx) = index { if let Some(idx) = index {
info!("Removing item at index {}", idx); info!("Removing item at index {}", idx);
@ -132,22 +144,32 @@ async fn main() -> io::Result<()> {
send_ipc_command("playlist-remove", &[json!("current")]).await?; send_ipc_command("playlist-remove", &[json!("current")]).await?;
} }
} }
CommandOptions::Clear => { CommandOptions::Clear => {
info!("Clearing the playlist"); info!("Clearing the playlist");
send_ipc_command("playlist-clear", &[]).await?; send_ipc_command("playlist-clear", &[]).await?;
} }
CommandOptions::List => { CommandOptions::List => {
info!("Listing playlist items"); info!("Listing playlist items");
if let Some(data) = send_ipc_command("get_property", &[json!("playlist")]).await? { if let Some(data) = send_ipc_command("get_property", &[json!("playlist")]).await? {
println!("{}", serde_json::to_string_pretty(&data)?); println!("{}", serde_json::to_string_pretty(&data)?);
} }
} }
CommandOptions::Add { filenames } => { CommandOptions::Add { filenames } => {
if filenames.is_empty() {
let e = "No files provided to add to the playlist";
error!("{}", e);
return Err(io::Error::new(io::ErrorKind::InvalidInput, e));
}
info!("Adding {} files to the playlist", filenames.len()); info!("Adding {} files to the playlist", filenames.len());
for filename in filenames { for filename in filenames {
send_ipc_command("loadfile", &[json!(filename), json!("append-play")]).await?; send_ipc_command("loadfile", &[json!(filename), json!("append-play")]).await?;
} }
} }
CommandOptions::Replace { filenames } => { CommandOptions::Replace { filenames } => {
info!("Replacing current playlist with {} files", filenames.len()); info!("Replacing current playlist with {} files", filenames.len());
if let Some(first_file) = filenames.first() { if let Some(first_file) = filenames.first() {
@ -157,6 +179,7 @@ async fn main() -> io::Result<()> {
} }
} }
} }
CommandOptions::Prop { properties } => { CommandOptions::Prop { properties } => {
info!("Fetching properties: {:?}", properties); info!("Fetching properties: {:?}", properties);
for property in properties { for property in properties {