1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
|
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<String>,
},
}
#[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(())
}
|