use clap::{Parser, Subcommand}; use std::env; use sqlx::SqlitePool; use uuid::Uuid; use silmataivas::users::{UserRepository, UserRole}; use silmataivas::notifications::{NtfySettingsInput, NtfySettingsRepository}; use silmataivas::weather_thresholds::WeatherThresholdRepository; use tracing::{info, warn, error, debug, trace}; use tracing_subscriber::{EnvFilter, FmtSubscriber}; #[derive(Parser)] #[command(name = "silmataivas")] struct Cli { /// Increase output verbosity (-v, -vv, -vvv) #[arg(short, long, action = clap::ArgAction::Count, global = true)] verbose: u8, #[command(subcommand)] command: Commands, } #[derive(Subcommand)] enum Commands { /// Create a new user with optional UUID CreateUser { uuid: Option, }, } #[tokio::main] async fn main() -> anyhow::Result<()> { let cli = Cli::parse(); // Set up logging based on verbosity let filter = match cli.verbose { 0 => "warn", 1 => "info", 2 => "debug", _ => "trace", }; let subscriber = FmtSubscriber::builder() .with_env_filter(EnvFilter::new(filter)) .finish(); tracing::subscriber::set_global_default(subscriber).expect("setting default subscriber failed"); let db_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set"); let pool = SqlitePool::connect(&db_url).await?; match cli.command { Commands::CreateUser { uuid } => { let repo = UserRepository { db: &pool }; let user_id = uuid.unwrap_or_else(|| Uuid::new_v4().to_string()); let user = repo.create_user(Some(user_id.clone()), Some(UserRole::User)).await?; // Set up default NTFY settings let ntfy_repo = NtfySettingsRepository { db: &pool }; ntfy_repo.create(NtfySettingsInput { user_id: user.id, enabled: true, topic: user_id.clone(), server_url: "https://ntfy.sh".to_string(), priority: 3, title_template: None, message_template: None, }).await?; // Set up default weather thresholds let threshold_repo = WeatherThresholdRepository { db: &pool }; threshold_repo.create_threshold( user.id, "temperature".to_string(), 35.0, ">".to_string(), true, Some("Default: temperature > 35°C".to_string()), ).await?; threshold_repo.create_threshold( user.id, "rain".to_string(), 50.0, ">".to_string(), true, Some("Default: rain > 50mm".to_string()), ).await?; threshold_repo.create_threshold( user.id, "wind_speed".to_string(), 80.0, ">".to_string(), true, Some("Default: wind speed > 80km/h".to_string()), ).await?; info!("User {} created", user_id); } } Ok(()) }