treeflow 0.2.0

CLI tool for simplified Git worktree management to speed up switching contexts when working collaboratively.
Documentation
use treeflow::core::config::{Config, ConfigPaths};
use std::env;
use std::path::PathBuf;
use treeflow::utils::errors::CustomError;
use treeflow::utils::{Return, Shell};
use crate::cli::command;

pub fn handle_config(config_paths: &ConfigPaths) -> Result<Return, CustomError> {
    let config = Config::load(config_paths)?;
    config.print()?;
    Ok(Return::Null {})
}

pub fn handle_init(config_paths: &ConfigPaths, shell: &Shell, enable_shorthands: bool) -> Result<Return, CustomError> {
    Config::load(config_paths).and_then(|config| config.print_initialisation(shell, enable_shorthands, || command(&config)))?;
    Ok(Return::Null {})
}

pub fn handle_start(config_paths: &ConfigPaths, work_name: &String, work_type_name: &String) -> Result<Return, CustomError> {
    Config::load(config_paths)
        .and_then(|config| config.current_project()?.work_on(config, work_type_name, work_name))
}

pub fn handle_primary(config_paths: &ConfigPaths) -> Result<Return, CustomError> {
    let project = Config::load(&config_paths)
        .and_then(|config| config.current_project())?;
    Ok(Return::Cd { path: project.repository.clone() })
}

pub fn handle_review(config_paths: &ConfigPaths, branch: &str) -> Result<Return, CustomError> {
    Config::load(config_paths)
        .and_then(|config| config.current_project()?.review(branch))
}

pub fn handle_finish(config_paths: &ConfigPaths, force: bool) -> Result<Return, CustomError> {
    let project = Config::load(config_paths).and_then(|config| config.current_project())?;
    let result = project.current_work_item()?.finish(project, force)?;
    Ok(result)
}

pub fn handle_project_list(config_paths: &ConfigPaths) -> Result<Return, CustomError> {
    let config = Config::load(config_paths)?;
    config.print_projects()?;
    Ok(Return::Null {})
}

pub fn handle_project_add(config_paths: &ConfigPaths, repository: &PathBuf, worktrees: Option<&PathBuf>) -> Result<Return, CustomError> {
    Config::load(config_paths)
        .and_then(|config| config.add_project(repository, worktrees))
        .and_then(|config| config.save(config_paths.clone()))?;
    Ok(Return::Null {})
}

pub fn handle_project_remove(config_paths: &ConfigPaths, repository: &PathBuf) -> Result<Return, CustomError> {
    Config::load(config_paths)
        .and_then(|config| config.remove_project(repository))
        .and_then(|config| config.save(config_paths.clone()))?;
    Ok(Return::Null {})
}

pub fn handle_worktype_list(config_paths: &ConfigPaths) -> Result<Return, CustomError> {
    let config = Config::load(config_paths)?;
    config.print_work_types()?;
    Ok(Return::Null {})
}

pub fn handle_worktype_add(config_paths: &ConfigPaths, type_name: &String, prefix: &String) -> Result<Return, CustomError> {
    Config::load(config_paths)
        .and_then(|config| config.add_work_type(type_name.to_string(), prefix.to_string()))
        .and_then(|config| config.save(config_paths.clone()))?;
    Ok(Return::Null {})
}

pub fn handle_worktype_remove(config_paths: &ConfigPaths, type_name: &str) -> Result<Return, CustomError> {
    Config::load(config_paths)
        .and_then(|config| config.remove_work_type(type_name))
        .and_then(|config| config.save(config_paths.clone()))?;
    Ok(Return::Null {})
}