kanri 0.11.0

Manage your projects within the terminal.
Documentation
use std::{fs, process::exit};

use anyhow::{Result, anyhow};
use clap::Parser;
use kanri::{
    cli::{Cli, Commands, ConfigCommands, ProfilesCommands},
    commands::{blueprints, config, profiles, root},
    config::Config,
    platform,
    terminal::print_error,
};

fn check_env() -> Result<()> {
    let config_path = platform::config_file();
    if !config_path.exists() {
        let default_config: Config = Config::default();
        default_config
            .save(config_path)
            .map_err(|e| anyhow!(e.to_string()))?;
    }

    let blueprints_dir = platform::blueprints_dir();
    if !blueprints_dir.exists() {
        fs::create_dir_all(&blueprints_dir).map_err(|e| anyhow!(e.to_string()))?;
    }

    Ok(())
}

const KANRI_BUILD_MODE: &str = if cfg!(debug_assertions) {
    "debug"
} else {
    "release"
};

const KANRI_VERSION: &str = env!("CARGO_PKG_VERSION");

fn print_version() {
    println!("kanri {KANRI_VERSION} {KANRI_BUILD_MODE}");
}

fn main() {
    colored::control::set_override(std::env::var("NO_COLOR").is_err());
    let cli = Cli::parse();

    if cli.version {
        print_version();
        return;
    }

    if let Err(e) = check_env() {
        print_error(&e.to_string());
        exit(1);
    }

    let result = match cli.cmd.expect("clap guarantees subcommand") {
        Commands::New(args) => root::handle_new(args),
        Commands::Clone(args) => root::handle_clone(args),
        Commands::Open(args) => root::handle_open(args),
        Commands::List(args) => root::handle_list(args),
        Commands::Rename(args) => root::handle_rename(args),
        Commands::Remove(args) => root::handle_remove(args),
        Commands::Blueprints { command } => blueprints::handle(command),
        Commands::Config { command } => match command {
            ConfigCommands::Path => config::handle_path(),
            ConfigCommands::Edit => config::handle_edit(),
            ConfigCommands::Recent(args) => config::handle_recent(args),
            ConfigCommands::Reset => config::handle_reset(),
        },
        Commands::Profiles { command } => match command {
            ProfilesCommands::New => profiles::handle_new(),
            ProfilesCommands::Set(args) => profiles::handle_set(args),
            ProfilesCommands::Get(args) => profiles::handle_get(args),
            ProfilesCommands::List => profiles::handle_list(),
            ProfilesCommands::Remove(args) => profiles::handle_remove(args),
        },
        Commands::Backup(args) => root::handle_backup(args),
        Commands::Import(args) => root::handle_import(args),
        Commands::Zen => root::handle_zen(),
    };

    if let Err(e) = result {
        print_error(&e.to_string());
        exit(1);
    }
}