logoxide-cli 0.1.1

lx, logoxide's command line: Bibles, commentaries, lexicons, books, and media as versioned packages
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
//! `lx`: logoxide's command line

use std::process::ExitCode;

use clap::{CommandFactory, Parser};
use logoxide::{
    bible_data,
    manifest::MANIFEST,
    resolve::{Frozen, Update},
};

/// `println!` with what isn't safe for a terminal taken out ([`logoxide::text::printable`]): what
/// lx prints holds catalogs', manifests', and git's text, which could hold escape sequences
macro_rules! outln {
    () => { println!() };
    ($($arg:tt)*) => { println!("{}", logoxide::text::printable(&format!($($arg)*))) };
}

/// `print!` through [`logoxide::text::printable`], as [`outln`]
macro_rules! out {
    ($($arg:tt)*) => { print!("{}", logoxide::text::printable(&format!($($arg)*))) };
}

/// `eprintln!` through [`logoxide::text::printable`], as [`outln`]
macro_rules! errln {
    () => { eprintln!() };
    ($($arg:tt)*) => { eprintln!("{}", logoxide::text::printable(&format!($($arg)*))) };
}

mod cli;
mod cmd;
mod complete;
mod publish;

use cli::{CatalogCommand, Cli, Command, Format, MediaCommand, RegistryCommand};
use cmd::{
    catalog::{
        Filters, Shown, catalog_add, catalog_build, catalog_entry, catalog_list, catalog_refresh,
        catalog_remove, package_url, search, search_freshness, works,
    },
    convert::convert,
    import::{import, import_update},
    map::map,
    media::{media_add, media_check},
    package::{Lookup, check, diff, lookup, new, release, show, show_remote, verify},
    project::{add, clean, info, install, is_enabled, list, list_all, remove, repair, set_enabled},
    read::{compare, read},
};

/// Prints a value as JSON
fn print_json(value: &impl serde::Serialize) -> logoxide::Result<()> {
    let text = serde_json::to_string_pretty(value)
        .map_err(|e| logoxide::LogoxideError::Manifest(e.to_string()))?;
    println!("{text}");
    Ok(())
}

/// logoxide's config (`~/.config/logoxide/config.toml`), read once when lx starts
static CONFIG: std::sync::OnceLock<logoxide::config::Config> = std::sync::OnceLock::new();

fn config() -> &'static logoxide::config::Config {
    CONFIG.get_or_init(Default::default)
}

/// What lx works with (logoxide-app's facade: the config, library, downloads, fetcher, and
/// reader), made once the command line is read
static APP: std::sync::OnceLock<logoxide_app::Logoxide> = std::sync::OnceLock::new();

/// Whether downloads go unsaid (JSON output)
static QUIET: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false);

fn app() -> &'static logoxide_app::Logoxide {
    APP.get_or_init(|| {
        logoxide_app::Logoxide::default_native(
            config().clone(),
            config().library(None, None),
            &user_agent(),
            notes(),
        )
    })
}

/// What lx's downloads say they are
fn user_agent() -> String {
    format!(
        "lx/{} (+https://github.com/Logoxide/logoxide)",
        env!("CARGO_PKG_VERSION")
    )
}

/// Each download said on stderr (unless lx prints JSON), and each warning
fn notes() -> logoxide_app::Notes {
    std::sync::Arc::new(|note: &logoxide_app::Note| {
        if note.is_warning() {
            errln!("warning: {note}");
        } else if !QUIET.load(std::sync::atomic::Ordering::Relaxed) {
            errln!("{note}");
        }
    })
}

/// Prints each warning as it's found
fn warn() -> impl FnMut(String) {
    |warning: String| errln!("warning: {warning}")
}

fn main() -> ExitCode {
    // Rust ignores SIGPIPE, so printing after the reader has gone (`lx list | head`) panics; the
    // default ends lx quietly, as it does other commands
    #[cfg(unix)]
    // SAFETY: nothing else has started yet, and SIG_DFL is a valid disposition for SIGPIPE
    unsafe {
        libc::signal(libc::SIGPIPE, libc::SIG_DFL);
    }
    // `COMPLETE=bash lx` writes the shell's completion script; the shell then runs lx to complete
    clap_complete::CompleteEnv::with_factory(Cli::command).complete();
    let cli = Cli::parse();
    match logoxide::config::Config::load() {
        Ok(loaded) => {
            let _ = CONFIG.set(loaded);
        }
        Err(e) => {
            errln!("lx: {e}");
            return ExitCode::FAILURE;
        }
    }
    if let Err(error) = check_format(&cli) {
        errln!("lx: {error}");
        return ExitCode::FAILURE;
    }
    let json = cli.format == Format::Json;
    QUIET.store(
        json || matches!(cli.command, Command::Search { json: true, .. }),
        std::sync::atomic::Ordering::Relaxed,
    );
    let _ = APP.set(logoxide_app::Logoxide::default_native(
        config().clone(),
        cli.library(),
        &user_agent(),
        notes(),
    ));
    let format = cli.format.name();
    let result = match &cli.command {
        Command::Show {
            package,
            git: None,
            notes: Some(reference),
            stretch,
            ..
        } => lookup(package, Lookup::Notes(reference, *stretch), json),
        Command::Show {
            package,
            git: None,
            entry: Some(key),
            ..
        } => lookup(package, Lookup::Entry(key), json),
        Command::Show {
            package,
            git: None,
            keys: true,
            limit,
            ..
        } => lookup(package, Lookup::Keys(*limit), json),
        Command::Show {
            package,
            git: None,
            xref: Some(reference),
            ..
        } => lookup(package, Lookup::CrossReferences(reference), json),
        Command::Show {
            package,
            git: None,
            references,
            locations,
            ..
        } => show(&cli, package, references.then_some((format, *locations))),
        Command::Show {
            git: Some(url),
            tag,
            references,
            ..
        } => package_url(&cli, url)
            .and_then(|url| show_remote(&cli, &url, tag.as_deref(), references.then_some(format))),
        Command::Release {
            path,
            set_version,
            dry_run,
        } => release(&cli, path, *set_version, *dry_run),
        Command::Diff { path, from, to } => diff(&cli, path, from, to.as_deref()),
        Command::Verify(args) => verify(args, json),
        Command::Check { package, cache } => check(package, cache.clone(), json, &cli.library()),
        Command::New(args) => new(args),
        Command::Init { dir } => logoxide::Project::init(dir)
            .map(|p| outln!("created {}", p.dir().join(MANIFEST).display())),
        Command::Add(args) => add(&cli, args),
        Command::Remove { id } => remove(&cli, id),
        Command::Disable { name } => set_enabled(&cli, name, false),
        Command::Enable { name } => set_enabled(&cli, name, true),
        Command::Install { repair: true, .. } => repair(&cli),
        Command::Install {
            locked, offline, ..
        } => install(
            &cli,
            Update::Nothing,
            Frozen {
                locked: *locked,
                offline: *offline,
            },
        ),
        Command::Update { ids } => install(
            &cli,
            match ids.is_empty() {
                true => Update::All,
                false => Update::Only(ids.iter().cloned().collect()),
            },
            Frozen::default(),
        ),
        Command::List { all: true, .. } => list_all(&cli),
        Command::List {
            enabled, disabled, ..
        } => list(
            &cli,
            match (enabled, disabled) {
                (true, _) => Some(true),
                (_, true) => Some(false),
                _ => None,
            },
        ),
        // Like `systemctl is-enabled`: 1 is an answer (off), so an error is 2
        Command::IsEnabled { name } => {
            return match is_enabled(&cli, name) {
                Ok(true) => ExitCode::SUCCESS,
                Ok(false) => ExitCode::FAILURE,
                Err(error) => {
                    errln!("lx: {error}");
                    ExitCode::from(2)
                }
            };
        }
        Command::Info { id } => info(&cli, id),
        Command::Clean {
            remove,
            keep,
            prune_roots,
        } => clean(&cli, *remove, keep, *prune_roots),
        Command::Search {
            query,
            catalog,
            registry,
            books,
            category,
            language,
            by_language,
            limit,
            flat,
            json,
            refresh,
            offline,
        } => search(
            &cli,
            &query.join(" "),
            catalog,
            registry.as_deref(),
            Filters {
                books: books.filters(),
                category: category.as_deref(),
                languages: language,
            },
            Shown {
                limit: *limit,
                json: *json || cli.format == Format::Json,
                flat: *flat,
                by_language: *by_language,
            },
            search_freshness(*refresh, *offline),
        ),
        Command::Works {
            query,
            catalog,
            registry,
            packages,
            refresh,
            offline,
        } => works(
            &cli,
            &query.join(" "),
            catalog,
            registry.as_deref(),
            *packages,
            json,
            search_freshness(*refresh, *offline),
        ),
        Command::Import {
            source,
            update: true,
            dry_run,
            yes,
            ..
        } => import_update(&cli, source.as_deref(), *dry_run, *yes),
        Command::Import { source, vendor, .. } => {
            import(&cli, source.as_deref().unwrap_or_default(), *vendor)
        }
        Command::Catalog(CatalogCommand::Entry { git }) => {
            catalog_entry(&cli, &logoxide::git::expand_url(git))
        }
        Command::Catalog(CatalogCommand::List) => catalog_list(&cli),
        Command::Catalog(CatalogCommand::Refresh { catalogs }) => catalog_refresh(&cli, catalogs),
        Command::Registry(RegistryCommand::Init {
            host,
            name,
            private,
        }) => publish::init(&publish::Host::parse(host), name.as_deref(), *private),
        Command::Publish { path, to, all, yes } => {
            let library = cli.library();
            let (host, git) = (publish::Host::parse(to), cli.git(&library));
            match all {
                true => publish::publish_all(path, &host, &git, *yes),
                false => publish::publish(path, &host, &git, *yes),
            }
        }
        Command::Yank {
            id,
            version,
            from,
            undo,
        } => publish::yank(&publish::Host::parse(from), id, version, *undo),
        Command::Catalog(CatalogCommand::Build {
            dir,
            output,
            url,
            name,
            force,
        }) => catalog_build(
            dir,
            output.as_deref(),
            url.as_deref(),
            name.as_deref(),
            *force,
        ),
        Command::Catalog(CatalogCommand::Add { location, name }) => {
            catalog_add(&cli, location, name.as_deref())
        }
        Command::Catalog(CatalogCommand::Remove { location }) => catalog_remove(&cli, location),
        Command::Media(MediaCommand::Add {
            file,
            url,
            book,
            package,
        }) => media_add(file, url, book.as_deref(), package),
        Command::Media(MediaCommand::Check { package, offline }) => {
            media_check(package, *offline, &cli.library())
        }
        Command::Convert {
            input,
            to,
            from,
            output,
        } => convert(input, to, from.as_deref(), output.as_deref()),
        Command::Map {
            reference,
            from,
            to,
            with,
        } => map(&cli, reference, from, to, with),
        Command::Read { reference, text } => read(&cli, reference, text.style(), json),
        Command::Compare {
            reference,
            bibles,
            text,
        } => compare(&cli, reference, bibles, text.style(), json),
    };
    match result {
        Ok(()) => ExitCode::SUCCESS,
        Err(error) => {
            errln!("lx: {error}");
            ExitCode::FAILURE
        }
    }
}

/// Whether the command prints in `--format`: JSON where it has a form for it, TSV only for
/// `lx show --references`
fn check_format(cli: &Cli) -> logoxide::Result<()> {
    let references = matches!(
        cli.command,
        Command::Show {
            references: true,
            ..
        }
    );
    // A commentary's notes, a dictionary's entry or keys, a verse's cross-references
    let lookup = matches!(
        cli.command,
        Command::Show { notes: Some(_), .. }
            | Command::Show { entry: Some(_), .. }
            | Command::Show { keys: true, .. }
            | Command::Show { xref: Some(_), .. }
    );
    let json = references
        || lookup
        || matches!(
            cli.command,
            Command::List { .. }
                | Command::Info { .. }
                | Command::Check { .. }
                | Command::Diff { .. }
                | Command::Verify(_)
                | Command::Search { .. }
                | Command::Works { .. }
                | Command::Catalog(CatalogCommand::List)
                | Command::Catalog(CatalogCommand::Refresh { .. })
                | Command::Import { update: true, .. }
                | Command::Read { .. }
                | Command::Compare { .. }
        );
    match cli.format {
        Format::Text => Ok(()),
        Format::Json if json => Ok(()),
        Format::Tsv if references => Ok(()),
        Format::Json => Err(logoxide::LogoxideError::Manifest(String::from(
            "--format json: only for list, info, check, diff, verify, search, works, catalog list, catalog refresh, import --update, read, compare, and show --references, --notes, --entry, --keys, or --xref",
        ))),
        Format::Tsv => Err(logoxide::LogoxideError::Manifest(String::from(
            "--format tsv: only for show --references",
        ))),
    }
}

/// Books by their USFM codes, named: `ROM GAL` is `Romans, Galatians`
fn book_names(codes: &[String]) -> String {
    let books = bible_data().books();
    let names: Vec<String> = (codes.iter())
        .map(|code| {
            (books.search_usfm(code))
                .and_then(|id| books.get_name(id))
                .cloned()
                .unwrap_or_else(|| code.clone())
        })
        .collect();
    names.join(", ")
}