modde-cli 0.3.1

CLI interface for modde
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! `modde config` -- inspect, validate, and set modde's configuration,
//! including the database backend.
//!
//! `show` reports the resolved database configuration using the same precedence
//! as runtime startup: environment first, then `settings.toml`, then backend
//! defaults where the backend has them. The honored database environment
//! variables are `MODDE_DATABASE_BACKEND`, `MODDE_DATABASE_URL`,
//! `MODDE_DATABASE_HOST`, `MODDE_DATABASE_PORT`, `MODDE_DATABASE_NAME`,
//! `MODDE_DATABASE_USER`, and `MODDE_DB_PASSWORD_FILE`.

use std::path::PathBuf;

use anyhow::{Context, Result, anyhow, bail};
use clap::ValueEnum;

use modde_core::db::{ModdeDb, build_pg_options, describe_pg_options};
use modde_core::settings::{AppSettings, DatabaseSettings, DbBackend};

/// Stored database fields that `config set-database --clear` can unset.
#[derive(Clone, Debug, ValueEnum)]
pub enum ClearDatabaseField {
    Url,
    Host,
    Port,
    Dbname,
    User,
    PasswordFile,
}

#[derive(Clone, Copy)]
enum Source {
    Env(&'static str),
    Settings,
    Default,
    Unset,
}

impl Source {
    const fn label(self) -> &'static str {
        match self {
            Self::Env(key) => key,
            Self::Settings => "settings.toml",
            Self::Default => "backend default",
            Self::Unset => "unset",
        }
    }
}

struct ResolvedField<T> {
    value: T,
    source: Source,
}

fn env_var(key: &'static str) -> Option<String> {
    std::env::var(key).ok()
}

fn resolved_string(
    env_key: &'static str,
    setting: Option<String>,
    default: Option<&'static str>,
) -> ResolvedField<Option<String>> {
    if let Some(value) = env_var(env_key) {
        return ResolvedField {
            value: Some(value),
            source: Source::Env(env_key),
        };
    }
    if let Some(value) = setting {
        return ResolvedField {
            value: Some(value),
            source: Source::Settings,
        };
    }
    if let Some(value) = default {
        return ResolvedField {
            value: Some(value.to_string()),
            source: Source::Default,
        };
    }
    ResolvedField {
        value: None,
        source: Source::Unset,
    }
}

fn resolved_path(
    env_key: &'static str,
    setting: Option<PathBuf>,
) -> ResolvedField<Option<PathBuf>> {
    if let Some(value) = env_var(env_key) {
        return ResolvedField {
            value: Some(PathBuf::from(value)),
            source: Source::Env(env_key),
        };
    }
    if let Some(value) = setting {
        return ResolvedField {
            value: Some(value),
            source: Source::Settings,
        };
    }
    ResolvedField {
        value: None,
        source: Source::Unset,
    }
}

fn resolved_port(setting: Option<u16>) -> Result<ResolvedField<u16>> {
    if let Some(raw) = env_var("MODDE_DATABASE_PORT") {
        let port = raw.parse::<u16>().map_err(|e| {
            anyhow!("invalid MODDE_DATABASE_PORT value '{raw}': expected 0-65535 ({e})")
        })?;
        return Ok(ResolvedField {
            value: port,
            source: Source::Env("MODDE_DATABASE_PORT"),
        });
    }
    if let Some(port) = setting {
        return Ok(ResolvedField {
            value: port,
            source: Source::Settings,
        });
    }
    Ok(ResolvedField {
        value: 5432,
        source: Source::Default,
    })
}

fn print_field(name: &str, value: &str, source: Source) {
    println!("  {name}: {value} (from {})", source.label());
}

fn redact_url(raw: &str) -> String {
    let Ok(mut url) = url::Url::parse(raw) else {
        return "<unparseable url; redacted>".to_string();
    };
    if url.password().is_some() {
        let _ = url.set_password(Some("<redacted>"));
    }
    url.to_string()
}

fn postgres_summary(db: &DatabaseSettings) -> Result<String> {
    let opts = build_pg_options(db, &|key| std::env::var(key).ok())?;
    Ok(describe_pg_options(&opts))
}

/// Print the resolved database configuration and its source.
pub fn handle_show() -> Result<()> {
    let settings = AppSettings::load();
    let db = &settings.database;

    let (effective_backend, backend_source) = match env_var("MODDE_DATABASE_BACKEND") {
        Some(env) => (
            DbBackend::parse(&env)
                .ok_or_else(|| anyhow!("invalid MODDE_DATABASE_BACKEND value '{env}'"))?,
            Source::Env("MODDE_DATABASE_BACKEND"),
        ),
        None => (db.backend, Source::Settings),
    };

    print_field(
        "database backend",
        effective_backend.as_str(),
        backend_source,
    );

    match effective_backend {
        DbBackend::Sqlite => {
            println!("  sqlite path: {}", modde_core::paths::db_path().display());
        }
        DbBackend::Postgres => {
            let url = resolved_string("MODDE_DATABASE_URL", db.url.clone(), None);
            let resolved = build_pg_options(db, &|key| std::env::var(key).ok());
            if let Some(value) = &url.value {
                print_field("url", &redact_url(value), url.source);
                if let Ok(opts) = &resolved {
                    println!("  resolved: {}", describe_pg_options(opts));
                }
            } else {
                let host = resolved_string(
                    "MODDE_DATABASE_HOST",
                    db.host.clone(),
                    Some("<backend default>"),
                );
                let port = resolved_port(db.port)?;
                let dbname = resolved_string("MODDE_DATABASE_NAME", db.dbname.clone(), None);
                let user = resolved_string(
                    "MODDE_DATABASE_USER",
                    db.user.clone(),
                    Some("<backend default>"),
                );

                let resolved_host = resolved
                    .as_ref()
                    .map(|opts| opts.get_host().to_string())
                    .ok()
                    .or(host.value);
                let resolved_port = match resolved.as_ref() {
                    Ok(opts) => opts.get_port(),
                    Err(_) => port.value,
                };
                let resolved_dbname = resolved
                    .as_ref()
                    .ok()
                    .and_then(|opts| opts.get_database())
                    .map(str::to_string)
                    .or(dbname.value);
                let resolved_user = resolved
                    .as_ref()
                    .map(|opts| opts.get_username().to_string())
                    .ok()
                    .or(user.value);

                print_field(
                    "host",
                    resolved_host.as_deref().unwrap_or("<unset>"),
                    host.source,
                );
                print_field("port", &resolved_port.to_string(), port.source);
                print_field(
                    "dbname",
                    resolved_dbname.as_deref().unwrap_or("<unset>"),
                    dbname.source,
                );
                print_field(
                    "user",
                    resolved_user.as_deref().unwrap_or("<unset>"),
                    user.source,
                );
            }

            let pw = resolved_path("MODDE_DB_PASSWORD_FILE", db.password_file.clone());
            let pw_value = pw
                .value
                .as_ref()
                .map(|path| path.display().to_string())
                .unwrap_or_else(|| "<none>".to_string());
            print_field("password file", &pw_value, pw.source);

            if let Err(e) = resolved {
                println!("  resolution error: {e}");
            }
        }
    }

    println!("config file: {}", AppSettings::config_path().display());
    Ok(())
}

/// Open the resolved database, run a trivial query, and report the result.
pub async fn handle_test() -> Result<()> {
    let settings = AppSettings::load();
    let db = &settings.database;
    let backend = env_var("MODDE_DATABASE_BACKEND")
        .as_deref()
        .and_then(DbBackend::parse)
        .unwrap_or(db.backend);

    println!("database backend: {}", backend.as_str());
    match backend {
        DbBackend::Sqlite => {
            println!("summary: path={}", modde_core::paths::db_path().display());
        }
        DbBackend::Postgres => {
            println!("summary: {}", postgres_summary(db)?);
        }
    }

    let db = ModdeDb::open()
        .await
        .with_context(|| format!("failed to open {} database", backend.as_str()))?;
    db.ping()
        .await
        .with_context(|| format!("failed to run database probe against {}", backend.as_str()))?;
    println!("OK");
    Ok(())
}

/// Reset stored database settings to `SQLite` and clear all `PostgreSQL` fields.
pub fn handle_reset_database() -> Result<()> {
    let mut settings = AppSettings::load();
    settings.database.backend = DbBackend::Sqlite;
    clear_postgres_fields(&mut settings.database);
    settings.save();
    println!("database backend reset to sqlite");
    println!("postgres connection fields cleared from settings.toml");
    Ok(())
}

fn clear_postgres_fields(db: &mut DatabaseSettings) {
    db.url = None;
    db.host = None;
    db.port = None;
    db.dbname = None;
    db.user = None;
    db.password_file = None;
}

fn apply_clears(db: &mut DatabaseSettings, clear: &[ClearDatabaseField]) {
    for field in clear {
        match field {
            ClearDatabaseField::Url => db.url = None,
            ClearDatabaseField::Host => db.host = None,
            ClearDatabaseField::Port => db.port = None,
            ClearDatabaseField::Dbname => db.dbname = None,
            ClearDatabaseField::User => db.user = None,
            ClearDatabaseField::PasswordFile => db.password_file = None,
        }
    }
}

enum FieldUpdate<T> {
    Unchanged,
    Clear,
    Set(T),
}

fn normalize_string(value: Option<String>) -> FieldUpdate<String> {
    value
        .map(|s| {
            if s.trim().is_empty() {
                FieldUpdate::Clear
            } else {
                FieldUpdate::Set(s)
            }
        })
        .unwrap_or(FieldUpdate::Unchanged)
}

fn normalize_path(value: Option<PathBuf>) -> FieldUpdate<PathBuf> {
    value
        .map(|path| {
            if path.as_os_str().is_empty() {
                FieldUpdate::Clear
            } else {
                FieldUpdate::Set(path)
            }
        })
        .unwrap_or(FieldUpdate::Unchanged)
}

fn has_discrete(db: &DatabaseSettings) -> bool {
    db.host.is_some() || db.port.is_some() || db.dbname.is_some() || db.user.is_some()
}

fn validate_database(db: &DatabaseSettings) -> Result<()> {
    match db.backend {
        DbBackend::Sqlite => {
            if db.url.is_some()
                || db.host.is_some()
                || db.port.is_some()
                || db.dbname.is_some()
                || db.user.is_some()
                || db.password_file.is_some()
            {
                bail!(
                    "connection fields (url/host/port/name/user/password-file) are only valid when backend = postgres"
                );
            }
        }
        DbBackend::Postgres => {
            if db.url.is_none() && db.dbname.is_none() {
                bail!(
                    "backend = postgres requires either --url, or at least --name (host/port/user optional, default to the local socket)"
                );
            }
            if db.url.is_some() && has_discrete(db) {
                bail!("set --url OR the discrete --host/--port/--name/--user fields, not both");
            }
        }
    }
    Ok(())
}

/// Update the stored database backend and `PostgreSQL` connection parameters.
#[allow(clippy::too_many_arguments)]
pub fn handle_set_database(
    backend: &str,
    url: Option<String>,
    host: Option<String>,
    port: Option<u16>,
    dbname: Option<String>,
    user: Option<String>,
    password_file: Option<PathBuf>,
    clear: &[ClearDatabaseField],
) -> Result<()> {
    let backend = DbBackend::parse(backend)
        .ok_or_else(|| anyhow!("invalid backend '{backend}' (expected 'sqlite' or 'postgres')"))?;

    let mut settings = AppSettings::load();
    settings.database.backend = backend;

    if backend == DbBackend::Sqlite {
        clear_postgres_fields(&mut settings.database);
    } else {
        apply_clears(&mut settings.database, clear);
    }

    match normalize_string(url) {
        FieldUpdate::Unchanged => {}
        FieldUpdate::Clear => settings.database.url = None,
        FieldUpdate::Set(value) => settings.database.url = Some(value),
    }
    match normalize_string(host) {
        FieldUpdate::Unchanged => {}
        FieldUpdate::Clear => settings.database.host = None,
        FieldUpdate::Set(value) => settings.database.host = Some(value),
    }
    if let Some(value) = port {
        settings.database.port = Some(value);
    }
    match normalize_string(dbname) {
        FieldUpdate::Unchanged => {}
        FieldUpdate::Clear => settings.database.dbname = None,
        FieldUpdate::Set(value) => settings.database.dbname = Some(value),
    }
    match normalize_string(user) {
        FieldUpdate::Unchanged => {}
        FieldUpdate::Clear => settings.database.user = None,
        FieldUpdate::Set(value) => settings.database.user = Some(value),
    }
    match normalize_path(password_file) {
        FieldUpdate::Unchanged => {}
        FieldUpdate::Clear => settings.database.password_file = None,
        FieldUpdate::Set(value) => settings.database.password_file = Some(value),
    }

    validate_database(&settings.database)?;
    settings.save();

    println!("database backend set to {}", backend.as_str());
    if backend == DbBackend::Sqlite {
        println!("postgres connection fields cleared from settings.toml");
    } else {
        println!(
            "note: the PostgreSQL password is read at runtime from the configured password file; \
             it is never stored in settings.toml."
        );
    }
    Ok(())
}