aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.rs
blob: 8470651f1d135d69fd01c41fe491de225b284548 (plain)
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
use anyhow::{Context, Result};
use clap::Parser;
use figment::{
    providers::{Env, Format, Serialized, Toml},
    Figment,
};
use projectr::{project::ProjectItem, search::Search, Cli, Config};
use tokio::signal;

#[tokio::main]
async fn main() -> Result<()> {
    let cli = Cli::parse();

    let config: Config = Figment::new()
        .merge(Figment::from(Serialized::defaults(&cli.projects)))
        .merge(Toml::file("projectr.toml"))
        .merge(Env::prefixed("PROJECTR_"))
        .extract()
        .context("Failed to extract config")?;

    tracing_subscriber::fmt::fmt()
        .pretty()
        .with_writer(std::io::stderr)
        .with_max_level(cli.verbosity)
        .init();

    let res = tokio::select! {
        res = signal::ctrl_c() => res.map_err(Into::into),
        res = run(&config) => res.context("Failed to run projectr"),
    };

    res
}

#[tracing::instrument]
pub async fn run(config: &Config) -> Result<()> {
    let mut projects: Vec<ProjectItem> = Search::from(config.paths.to_owned()).collect();

    projects.sort_unstable_by_key(|p| *p.timestamp());

    for project in projects {
        println!("{}", project.to_path_buf().to_string_lossy())
    }

    Ok(())
}