feat(cli): implements subcommands

This commit is contained in:
Alexander Navarro 2024-07-17 14:37:54 -04:00
parent 7382b06bdf
commit 3fefadd5b5
5 changed files with 37 additions and 50 deletions

View file

@ -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<PathBuf>,
},
Play,
SkipSong,
Set,
}
#[derive(Parser)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(help = "Directory to scan for files")]
path: Option<PathBuf>,
#[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) {

View file

@ -13,11 +13,6 @@ pub mod grpc_juno {
tonic::include_proto!("juno");
}
#[async_trait]
pub trait Connection {
async fn connect(&self) -> Result<(), Box<dyn Error>>;
}
/// Return true if the addr is already in use, false otherwise
pub fn is_socket_in_use(addr: SocketAddr) -> bool {
match TcpListener::bind(addr) {

View file

@ -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<dyn std::error::Error>> {
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<JunoServicesClient<Channel>, Box<dyn std::error::Error>> {
let client =
@ -61,4 +43,18 @@ impl GRPCClient {
Ok(())
}
pub async fn list_files(&self) -> Result<(), Box<dyn std::error::Error>> {
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(())
}
}

View file

@ -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<dyn Error>> {
println!("Starting server on: \"{}\"", CONFIG.address.to_string());
Server::builder()
.add_service(JunoServicesServer::new(GRPCServer::default()))
.serve(CONFIG.address)
.await?;
Ok(())
}
}

View file

@ -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();
};
}
}