faith 0.3.0

Agent-first Bible CLI. Multi-locale, deterministic, offline. Returns canonical JSON, supports batch and multi-translation parallel lookups.
Documentation
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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
use std::io::{self, Write};
use std::process::ExitCode;

use clap::{Parser, Subcommand, ValueEnum};

use faith::cli;
use faith::cli::OutputFormat;
use faith::error::FaithError;
use faith::store::{self, Store};

#[derive(Debug, Parser)]
#[command(name = "faith", version, about = "Agent-first Bible CLI")]
struct Cli {
    #[command(subcommand)]
    command: Cmd,
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum Format {
    Json,
    Text,
    Tsv,
    Csv,
}

impl From<Format> for OutputFormat {
    fn from(f: Format) -> Self {
        match f {
            Format::Json => OutputFormat::Json,
            Format::Text => OutputFormat::Text,
            Format::Tsv => OutputFormat::Tsv,
            Format::Csv => OutputFormat::Csv,
        }
    }
}

#[derive(Debug, Subcommand)]
enum Cmd {
    Get {
        reference: String,
        #[arg(long, value_delimiter = ',')]
        tr: Vec<String>,
        #[arg(long)]
        lang: Option<String>,
        #[arg(long, value_enum, default_value_t = Format::Json)]
        format: Format,
    },
    Batch {
        #[arg(long)]
        tr: String,
        #[arg(long, value_enum, default_value_t = Format::Json)]
        format: Format,
    },
    List {
        #[command(subcommand)]
        what: ListKind,
        #[arg(long, value_enum, default_value_t = Format::Json, global = true)]
        format: Format,
    },
    Install {
        #[arg(required = true)]
        translations: Vec<String>,
    },
    Manifest,
    Info {
        book: String,
        #[arg(long)]
        tr: Option<String>,
    },
    Random {
        #[arg(long)]
        tr: Option<String>,
        #[arg(long)]
        lang: Option<String>,
        #[arg(long)]
        book: Option<String>,
        #[arg(long, value_enum, default_value_t = ScopeArg::All)]
        scope: ScopeArg,
        #[arg(long)]
        seed: Option<u64>,
        #[arg(long, value_enum, default_value_t = Format::Json)]
        format: Format,
    },
    Diff {
        reference: String,
        #[arg(long, value_delimiter = ',', required = true)]
        tr: Vec<String>,
        #[arg(long)]
        lang: Option<String>,
        #[arg(long, value_enum, default_value_t = Format::Json)]
        format: Format,
    },
    Stats {
        #[arg(long)]
        tr: Option<String>,
    },
    Completions {
        shell: String,
    },
    Cache {
        #[command(subcommand)]
        subcommand: CacheKind,
    },
    Search {
        query: String,
        #[arg(long)]
        tr: Option<String>,
        #[arg(long)]
        lang: Option<String>,
        #[arg(long)]
        limit: Option<u32>,
        #[arg(long, value_enum, default_value_t = Format::Json)]
        format: Format,
    },
    /// Manage Faith configuration
    Config {
        #[command(subcommand)]
        action: ConfigAction,
    },
}

#[derive(Debug, Subcommand)]
enum ConfigAction {
    /// Show current configuration
    Get {
        /// Config key to get (e.g., "lang", "translation")
        key: Option<String>,
    },
    /// Set configuration value
    Set {
        /// Config key (e.g., "lang", "translation", "format")
        key: String,
        /// Value to set
        value: String,
    },
    /// Show config file path
    Path,
    /// Reset to defaults (deletes config.toml)
    Reset {
        #[arg(long)]
        confirm: bool,
    },
}

#[derive(Debug, Clone, Copy, ValueEnum)]
enum ScopeArg {
    All,
    Ot,
    Nt,
}

#[derive(Debug, Subcommand)]
enum ListKind {
    Translations {
        #[arg(long)]
        lang: Option<String>,
        #[arg(long)]
        installed: bool,
    },
    Books {
        #[arg(long)]
        tr: String,
    },
}

#[derive(Debug, Subcommand)]
enum CacheKind {
    /// Show cache and DB sizes
    Size,
    /// Clear cache directory (requires --confirm)
    Clear {
        #[arg(long)]
        confirm: bool,
    },
    /// Print cache directory path
    Path,
}

fn main() -> ExitCode {
    let cli = Cli::parse();
    match dispatch(cli) {
        Ok(code) => ExitCode::from(code as u8),
        Err(e) => {
            let err = faith::schema::ErrorOut::from_err(&e);
            let stdout = io::stdout();
            let mut h = stdout.lock();
            let _ = serde_json::to_writer(&mut h, &err);
            let _ = writeln!(h);
            ExitCode::from(e.exit_code_int() as u8)
        }
    }
}

fn dispatch(cli: Cli) -> Result<i32, FaithError> {
    let path = store::default_db_path()?;
    let stdout = io::stdout();
    let mut out = stdout.lock();

    // Load config for translation resolution
    let config = cli::config::load_config().unwrap_or_default();

    match cli.command {
        Cmd::Get {
            reference,
            tr,
            lang,
            format,
        } => {
            let store = Store::open(&path)?;
            let trs = resolve_lang_or_tr_with_config(&tr, lang.as_deref(), &config);
            cli::get::run(&store, &reference, &trs, format.into(), &mut out)
        }
        Cmd::Batch { tr, format } => {
            let store = Store::open(&path)?;
            let stdin = io::stdin();
            let mut lock = stdin.lock();
            cli::batch::run(&store, &tr, format.into(), &mut lock, &mut out)
        }
        Cmd::List { what, format } => {
            let store = Store::open(&path)?;
            match what {
                ListKind::Translations { lang, installed } => cli::list::run_translations(
                    &store,
                    lang.as_deref(),
                    installed,
                    format.into(),
                    &mut out,
                ),
                ListKind::Books { tr } => {
                    cli::list::run_books(&store, &tr, format.into(), &mut out)
                }
            }
        }
        Cmd::Install { translations } => {
            let mut store = Store::open(&path)?;
            cli::install::run(&mut store, &translations, &mut out)
        }
        Cmd::Manifest => {
            let store = Store::open(&path)?;
            cli::manifest::run(&store, &mut out)
        }
        Cmd::Info { book, tr } => {
            let store = Store::open(&path)?;
            cli::info::run(&store, &book, tr.as_deref(), &mut out)
        }
        Cmd::Random {
            tr,
            lang,
            book,
            scope,
            seed,
            format,
        } => {
            let store = Store::open(&path)?;
            let s = match scope {
                ScopeArg::All => cli::random::Scope::All,
                ScopeArg::Ot => cli::random::Scope::Ot,
                ScopeArg::Nt => cli::random::Scope::Nt,
            };
            // Resolve translation using config if neither --tr nor --lang provided
            let resolved = if tr.is_none() && lang.is_none() {
                let trs = resolve_lang_or_tr_with_config(&[], None, &config);
                Some(trs[0].clone())
            } else {
                tr
            };
            cli::random::run(
                &store,
                resolved.as_deref(),
                lang.as_deref(),
                book.as_deref(),
                s,
                seed,
                format.into(),
                &mut out,
            )
        }
        Cmd::Diff {
            reference,
            tr,
            lang,
            format,
        } => {
            let store = Store::open(&path)?;
            let trs = resolve_lang_or_tr_with_config(&tr, lang.as_deref(), &config);
            cli::diff::run(&store, &reference, &trs, format.into(), &mut out)
        }
        Cmd::Stats { tr } => {
            let store = Store::open(&path)?;
            let dir = store::data_dir()?;
            cli::stats::run(&store, tr.as_deref(), &dir, &mut out)
        }
        Cmd::Completions { shell } => cli::completions::run(&shell, &mut out),
        Cmd::Cache { subcommand } => {
            let sub_name = match subcommand {
                CacheKind::Size => "size",
                CacheKind::Clear { .. } => "clear",
                CacheKind::Path => "path",
            };
            let confirm = matches!(subcommand, CacheKind::Clear { confirm: true });
            cli::cache::run(sub_name, confirm, &mut out)
        }
        Cmd::Search {
            query,
            tr,
            lang,
            limit,
            format,
        } => {
            let store = Store::open(&path)?;
            // Resolve translation with config support
            let fallback_tr = if tr.is_none() && lang.is_none() {
                let trs = resolve_lang_or_tr_with_config(&[], None, &config);
                Some(trs[0].clone())
            } else {
                None
            };
            let resolved_tr = match (&tr, &lang, &fallback_tr) {
                (Some(t), _, _) => Some(t.as_str()),
                (None, Some(l), _) => cli::resolve_by_lang(l),
                (None, None, Some(f)) => Some(f.as_str()),
                _ => None,
            };
            cli::search::run(&store, &query, resolved_tr, limit, format.into(), &mut out)
        }
        Cmd::Config { action } => handle_config(action, &mut out),
    }
}

/// Resolve translation with config support and 7-layer precedence:
/// 1. CLI --tr flag (highest priority)
/// 2. CLI --lang flag
/// 3. FAITH_LANG environment variable
/// 4. Config file translation preference
/// 5. Config file lang preference
/// 6. System locale (LANG env var)
/// 7. Hardcoded default (KJV)
fn resolve_lang_or_tr_with_config(
    cli_tr: &[String],
    cli_lang: Option<&str>,
    config: &cli::config::Config,
) -> Vec<String> {
    // 1. CLI --tr flag (highest priority)
    if !cli_tr.is_empty() {
        return cli_tr.to_vec();
    }

    // 2. CLI --lang flag
    if let Some(l) = cli_lang {
        if let Some(alias) = cli::resolve_by_lang(l) {
            return vec![alias.to_string()];
        }
    }

    // 3. FAITH_LANG environment variable
    if let Ok(env_lang) = std::env::var("FAITH_LANG") {
        if let Some(alias) = cli::resolve_by_lang(&env_lang) {
            return vec![alias.to_string()];
        }
    }

    // 4. Config file translation preference
    if let Some(ref tr) = config.preferences.translation {
        return vec![tr.clone()];
    }

    // 5. Config file lang preference
    if let Some(ref lang) = config.preferences.lang {
        if let Some(alias) = cli::resolve_by_lang(lang) {
            return vec![alias.to_string()];
        }
    }

    // 6. System locale (LANG env var)
    if let Ok(sys_lang) = std::env::var("LANG") {
        // Parse LANG=pt_BR.UTF-8 → "pt"
        if let Some(iso2) = sys_lang.split('_').next() {
            if let Some(alias) = cli::resolve_by_lang(iso2) {
                return vec![alias.to_string()];
            }
        }
    }

    // 7. Hardcoded default (English KJV)
    vec!["KJV".to_string()]
}

/// Handle the `faith config` subcommand.
fn handle_config(action: ConfigAction, out: &mut dyn Write) -> Result<i32, FaithError> {
    let config = cli::config::load_config()?;

    match action {
        ConfigAction::Get { key } => {
            if let Some(k) = key {
                // Show specific key
                let value = match k.as_str() {
                    "lang" => config.preferences.lang.as_deref().unwrap_or("(not set)"),
                    "translation" => config.preferences.translation.as_deref().unwrap_or("(not set)"),
                    "format" => config.preferences.format.as_deref().unwrap_or("(not set)"),
                    "seed" => {
                        if let Some(s) = config.preferences.seed {
                            writeln!(out, "{}", s).ok();
                            return Ok(0);
                        } else {
                            "(not set)"
                        }
                    }
                    _ => {
                        return Err(FaithError::Io(format!("Unknown config key: {}", k)));
                    }
                };
                writeln!(out, "{}", value).ok();
            } else {
                // Show entire config
                let toml_str = toml::to_string_pretty(&config)
                    .map_err(|e| FaithError::Io(format!("Failed to serialize config: {}", e)))?;
                write!(out, "{}", toml_str).ok();
            }
            Ok(0)
        }
        ConfigAction::Set { key, value } => {
            let mut cfg = config.clone();
            match key.as_str() {
                "lang" => cfg.preferences.lang = Some(value.clone()),
                "translation" => cfg.preferences.translation = Some(value.clone()),
                "format" => cfg.preferences.format = Some(value.clone()),
                "seed" => {
                    let seed = value.parse::<u64>()
                        .map_err(|_| FaithError::Io(format!("Invalid seed value: {}", value)))?;
                    cfg.preferences.seed = Some(seed);
                }
                _ => {
                    return Err(FaithError::Io(format!("Unknown config key: {}", key)));
                }
            }
            cli::config::save_config(&cfg)?;
            writeln!(out, "✓ Set {} = {}", key, value).ok();
            Ok(0)
        }
        ConfigAction::Path => {
            let path = cli::config::config_path()?;
            writeln!(out, "{}", path.display()).ok();
            Ok(0)
        }
        ConfigAction::Reset { confirm } => {
            if !confirm {
                writeln!(out, "⚠️  Use --confirm to reset configuration").ok();
                return Ok(1);
            }
            let path = cli::config::config_path()?;
            if path.exists() {
                std::fs::remove_file(&path)
                    .map_err(|e| FaithError::Io(format!("Failed to delete config: {}", e)))?;
                writeln!(out, "✓ Configuration reset (deleted {})", path.display()).ok();
            } else {
                writeln!(out, "No configuration file to reset").ok();
            }
            Ok(0)
        }
    }
}