Skip to main content

dkdc_links/
cli.rs

1use anyhow::Result;
2use clap::Parser;
3
4use crate::config::{edit_config, init_config, load_config, print_config};
5use crate::open::open_links;
6
7#[derive(Parser, Debug)]
8#[command(name = "dkdc-links")]
9#[command(about = "Bookmarks in your terminal")]
10#[command(version)]
11pub struct Args {
12    /// Configure dkdc
13    #[arg(short, long)]
14    pub config: bool,
15
16    /// Open the graphical interface
17    #[cfg(feature = "gui")]
18    #[arg(long)]
19    pub gui: bool,
20
21    /// Things to open
22    pub links: Vec<String>,
23}
24
25pub fn run<I, T>(args: I) -> Result<()>
26where
27    I: IntoIterator<Item = T>,
28    T: Into<std::ffi::OsString> + Clone,
29{
30    let args = Args::parse_from(args);
31
32    #[cfg(feature = "gui")]
33    if args.gui {
34        return crate::gui::run().map_err(|e| anyhow::anyhow!("{e}"));
35    }
36
37    init_config()?;
38
39    if args.config {
40        edit_config()?;
41        return Ok(());
42    }
43
44    let config = load_config()?;
45
46    if args.links.is_empty() {
47        print_config(&config);
48    } else {
49        open_links(&args.links, &config)?;
50    }
51
52    Ok(())
53}