Skip to main content

dkdc_links/
cli.rs

1use anyhow::Result;
2use clap::Parser;
3
4use crate::config::{edit_config, print_config};
5use crate::open::open_links;
6use crate::storage::Storage;
7use crate::toml_storage::TomlStorage;
8
9#[derive(Parser, Debug)]
10#[command(name = "dkdc-links")]
11#[command(about = "Bookmarks in your terminal")]
12#[command(version)]
13pub struct Args {
14    /// Configure dkdc
15    #[arg(short, long)]
16    pub config: bool,
17
18    /// Open the desktop app
19    #[cfg(feature = "app")]
20    #[arg(short = 'a', long)]
21    pub app: bool,
22
23    /// Open the webapp
24    #[cfg(feature = "webapp")]
25    #[arg(short = 'w', long)]
26    pub webapp: bool,
27
28    /// Things to open
29    pub links: Vec<String>,
30}
31
32pub fn run<I, T>(args: I) -> Result<()>
33where
34    I: IntoIterator<Item = T>,
35    T: Into<std::ffi::OsString> + Clone,
36{
37    let args = Args::parse_from(args);
38
39    let storage = TomlStorage::with_default_path()?;
40
41    #[cfg(feature = "app")]
42    if args.app {
43        storage.init()?;
44        return crate::app::run(Box::new(storage)).map_err(|e| anyhow::anyhow!("{e}"));
45    }
46
47    #[cfg(feature = "webapp")]
48    if args.webapp {
49        storage.init()?;
50        return crate::webapp::run(Box::new(storage));
51    }
52    storage.init()?;
53
54    if args.config {
55        if let Some(path) = storage.path() {
56            edit_config(path)?;
57        }
58        return Ok(());
59    }
60
61    let config = storage.load()?;
62
63    if args.links.is_empty() {
64        print_config(&config);
65    } else {
66        open_links(&args.links, &config)?;
67    }
68
69    Ok(())
70}