From 3fefadd5b5a5e7eb54158f796b99efb4c505eb00 Mon Sep 17 00:00:00 2001 From: aleidk Date: Wed, 17 Jul 2024 14:37:54 -0400 Subject: [PATCH] feat(cli): implements subcommands --- src/configuration.rs | 28 +++++++++++++++++++++++----- src/grpc.rs | 5 ----- src/grpc/client.rs | 32 ++++++++++++++------------------ src/grpc/server.rs | 14 -------------- src/player.rs | 8 -------- 5 files changed, 37 insertions(+), 50 deletions(-) diff --git a/src/configuration.rs b/src/configuration.rs index 49feaeb..118f2fb 100644 --- a/src/configuration.rs +++ b/src/configuration.rs @@ -1,4 +1,4 @@ -use clap::Parser; +use clap::{Parser, Subcommand}; use lazy_static::lazy_static; use std::env; use std::net::SocketAddr; @@ -17,13 +17,26 @@ pub enum ConfigMode { Client, } +#[derive(Subcommand, Debug, Clone)] +pub enum Commands { + Start { + #[arg(help = "Directory to scan for files")] + path: Option, + }, + Play, + SkipSong, + Set, +} + #[derive(Parser)] #[command(version, about, long_about = None)] struct Args { - #[arg(help = "Directory to scan for files")] - path: Option, + #[command(subcommand)] + cmd: Commands, + #[arg(short, long, help = "the port to bind to", default_value = "50051")] port: u16, + #[arg( long, help = "The value 1.0 is the “normal” volume. Any value other than 1.0 will multiply each sample by this value.", @@ -34,6 +47,7 @@ struct Args { #[derive(Debug)] pub struct Config { + pub command: Commands, pub base_path: PathBuf, pub address: SocketAddr, pub mode: ConfigMode, @@ -43,6 +57,7 @@ pub struct Config { impl Default for Config { fn default() -> Self { Config { + command: Commands::Play, base_path: env::current_dir().expect("Current directory is not available."), mode: ConfigMode::Server, address: SocketAddr::from_str("[::1]:50051").unwrap(), @@ -57,9 +72,12 @@ impl Config { let mut config = Self::default(); config.address = SocketAddr::from_str(format!("[::1]:{}", cli.port).as_str()).unwrap(); config.volume = cli.volume; + config.command = cli.cmd.to_owned(); - if let Some(path) = cli.path { - config.base_path = path; + if let Commands::Start { path } = cli.cmd { + if let Some(path) = path { + config.base_path = path; + } } if grpc::is_socket_in_use(config.address) { diff --git a/src/grpc.rs b/src/grpc.rs index 20bf2e0..495e524 100644 --- a/src/grpc.rs +++ b/src/grpc.rs @@ -13,11 +13,6 @@ pub mod grpc_juno { tonic::include_proto!("juno"); } -#[async_trait] -pub trait Connection { - async fn connect(&self) -> Result<(), Box>; -} - /// Return true if the addr is already in use, false otherwise pub fn is_socket_in_use(addr: SocketAddr) -> bool { match TcpListener::bind(addr) { diff --git a/src/grpc/client.rs b/src/grpc/client.rs index 816d2ed..9523785 100644 --- a/src/grpc/client.rs +++ b/src/grpc/client.rs @@ -12,24 +12,6 @@ use tonic::Request; #[derive(Debug, Default)] pub struct GRPCClient {} -#[async_trait] -impl super::Connection for GRPCClient { - async fn connect(&self) -> Result<(), Box> { - let mut client = - JunoServicesClient::connect(format!("http://{}", CONFIG.address.to_string())).await?; - - let request = Request::new(GetFilesRequest { - path: CONFIG.base_path.display().to_string(), - }); - - let response = client.get_files(request).await?.into_inner(); - - println!("RESPONSE={:?}", response.files); - - Ok(()) - } -} - impl GRPCClient { async fn get_client(&self) -> Result, Box> { let client = @@ -61,4 +43,18 @@ impl GRPCClient { Ok(()) } + + pub async fn list_files(&self) -> Result<(), Box> { + let mut client = self.get_client().await?; + + let request = Request::new(GetFilesRequest { + path: CONFIG.base_path.display().to_string(), + }); + + let response = client.get_files(request).await?.into_inner(); + + println!("RESPONSE={:?}", response.files); + + Ok(()) + } } diff --git a/src/grpc/server.rs b/src/grpc/server.rs index 77b1aff..c80641c 100644 --- a/src/grpc/server.rs +++ b/src/grpc/server.rs @@ -86,17 +86,3 @@ impl JunoServices for GRPCServer { Ok(Response::new(StatusResponse {})) } } - -#[async_trait] -impl super::Connection for GRPCServer { - async fn connect(&self) -> Result<(), Box> { - println!("Starting server on: \"{}\"", CONFIG.address.to_string()); - - Server::builder() - .add_service(JunoServicesServer::new(GRPCServer::default())) - .serve(CONFIG.address) - .await?; - - Ok(()) - } -} diff --git a/src/player.rs b/src/player.rs index dd1c4d0..3fe7363 100644 --- a/src/player.rs +++ b/src/player.rs @@ -106,12 +106,4 @@ impl Player { self.sink.pause(); }; } - - fn set_playback_state(&self, is_paused: bool) { - if is_paused { - self.sink.pause(); - } else { - self.sink.play(); - }; - } }