Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 69 additions & 23 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use bottles_core::proto::NotifyRequest;
pub use bottles_core::proto::{HealthRequest, bottles_client::BottlesClient};
use bottles_core::proto::bottles::{management_client::ManagementClient, CreateBottleRequest, DeleteBottleRequest, ListBottlesRequest, BottleRequest};
use clap::{Parser, Subcommand};
use tracing_subscriber::EnvFilter;

Expand All @@ -12,12 +11,23 @@ struct Cli {

#[derive(Subcommand)]
enum Command {
#[command(about = "Check the health of the server")]
Health,
#[command(about = "Notify the server")]
Notify {
#[arg(help = "The message to send")]
message: String,
Create {
name: String,
#[arg(short, long, default_value = "Gaming")]
r#type: String,
},
Delete {
name: String,
},
List,
Start {
name: String,
},
Stop {
name: String,
},
Restart {
name: String,
},
}

Expand All @@ -28,28 +38,64 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
.init();

let args = Cli::parse();
// Connect to Server
let url = "http://[::1]:50052";
let mut client = BottlesClient::connect(url).await?;
let mut client = ManagementClient::connect(url).await?;

match args.command {
Command::Health => {
let request = HealthRequest {};
let response = client.health(request).await?;
let response = response.get_ref();
if response.ok {
tracing::info!("Server is healthy");
Command::Create { name, r#type } => {
let request = CreateBottleRequest {
name,
r#type,
runner: String::new(),
};
let response = client.create_bottle(request).await?;
let bottle = response.get_ref();
println!("Created bottle: {} ({}) at {}", bottle.name, bottle.r#type, bottle.path);
}
Command::Delete { name } => {
let request = DeleteBottleRequest { name };
let response = client.delete_bottle(request).await?;
if response.get_ref().success {
println!("Deleted bottle successfully");
} else {
eprintln!("Failed to delete bottle: {}", response.get_ref().error_message);
}
}
Command::List => {
let request = ListBottlesRequest {};
let response = client.list_bottles(request).await?;
let list = response.get_ref();
println!("Bottles:");
for bottle in &list.bottles {
println!("- {} ({}) [{}]", bottle.name, bottle.r#type, if bottle.active { "Running" } else { "Stopped" });
}
}
Command::Start { name } => {
let request = BottleRequest { name };
let response = client.start_bottle(request).await?;
if response.get_ref().success {
println!("Bottle started successfully");
} else {
eprintln!("Failed to start bottle: {}", response.get_ref().error_message);
}
}
Command::Stop { name } => {
let request = BottleRequest { name };
let response = client.stop_bottle(request).await?;
if response.get_ref().success {
println!("Bottle stopped successfully");
} else {
tracing::info!("Server is unhealthy");
eprintln!("Failed to stop bottle: {}", response.get_ref().error_message);
}
}
Command::Notify { message } => {
let request = NotifyRequest { message };
let response = client.notify(request).await?;
let response = response.get_ref();
if response.success {
tracing::info!("Message sent successfully");
Command::Restart { name } => {
let request = BottleRequest { name };
let response = client.restart_bottle(request).await?;
if response.get_ref().success {
println!("Bottle restarted successfully");
} else {
tracing::info!("Failed to send message");
eprintln!("Failed to restart bottle: {}", response.get_ref().error_message);
}
}
}
Expand Down