Enhance README with CLI room checking instructions and implement room name resolution in command handling. Updated handle_init_command and handle_join_command to use resolved room names, improving room access validation. Added CLI command handling in main.rs to list available rooms from the database or fallback configuration.
This commit is contained in:
45
src/main.rs
45
src/main.rs
@@ -38,6 +38,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
allowed_users: db::parse_allowed_users(),
|
||||
db_client,
|
||||
});
|
||||
if handle_cli_commands().as_deref() == Some("--list-rooms") {
|
||||
print_rooms_for_cli(&config).await?;
|
||||
return Ok(());
|
||||
}
|
||||
let rooms = db::load_room_configs(&config).await.unwrap_or_else(|_| {
|
||||
vec![types::RoomMeta {
|
||||
name: "lobby".to_string(),
|
||||
@@ -212,6 +216,47 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn handle_cli_commands() -> Option<String> {
|
||||
env::args().nth(1)
|
||||
}
|
||||
|
||||
async fn print_rooms_for_cli(
|
||||
config: &ServerConfig,
|
||||
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
|
||||
let rooms = db::load_room_configs(config)
|
||||
.await
|
||||
.map_err(std::io::Error::other)?;
|
||||
|
||||
println!(
|
||||
"yourchat2 rooms source: {}",
|
||||
if config.db_client.is_some() { "database" } else { "fallback" }
|
||||
);
|
||||
println!(
|
||||
"{:<30} {:<8} {:<8} {:<8} {:<10}",
|
||||
"name", "public", "min_age", "max_age", "password"
|
||||
);
|
||||
println!("{}", "-".repeat(72));
|
||||
for room in rooms {
|
||||
println!(
|
||||
"{:<30} {:<8} {:<8} {:<8} {:<10}",
|
||||
room.name,
|
||||
if room.is_public { "yes" } else { "no" },
|
||||
room.min_age
|
||||
.map(|v| v.to_string())
|
||||
.unwrap_or_else(|| "-".to_string()),
|
||||
room.max_age
|
||||
.map(|v| v.to_string())
|
||||
.unwrap_or_else(|| "-".to_string()),
|
||||
if room.password.as_deref().unwrap_or("").is_empty() {
|
||||
"none"
|
||||
} else {
|
||||
"set"
|
||||
},
|
||||
);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn handle_client<S>(
|
||||
stream: S,
|
||||
state: Arc<RwLock<ChatState>>,
|
||||
|
||||
Reference in New Issue
Block a user