vbump 0.5.1

Bumps the version of a project, creating the appropiate git tags
Documentation
use std::process::Command;
use std::path::PathBuf;
use std::ffi::OsStr;

use clap::Parser;
use anyhow::Result;

use vbump::{Level, handlers, detect::{ProjectType, detect_project_type}};

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
struct Args {
    /// what part of the version to increase
    #[clap(arg_enum, value_parser)]
    level: Level,

    /// A message that will be used for the commit and tag
    msg: String,

    /// Run in this directory instead of where the command is invocated
    dir: Option<PathBuf>,

    /// Don't actually do anything, just print the new version
    #[clap(short, long)]
    fake: bool,
}

fn error_trap() -> Result<()> {
    let matches = Args::parse();

    let project_dir = matches.dir.unwrap_or_else(|| PathBuf::from(".")).canonicalize()?;
    let project_type = detect_project_type(&project_dir)?;

    let (new_ver, modified_files) = match project_type {
        ProjectType::Rust(f) => {
            handlers::rust::handle(&f, matches.level, matches.fake)?
        }
        ProjectType::Python(f) => {
            handlers::python::handle(&f, matches.level, matches.fake)?
        }
        ProjectType::Javascript(f) => {
            handlers::javascript::handle(&f, matches.level, matches.fake)?
        }
        _ => {
            handlers::nofile::handle(&project_dir, matches.level, matches.fake)?
        }
    };

    if !matches.fake {
        for filename in modified_files {
            Command::new("git").current_dir(&project_dir).args([OsStr::new("add"), filename.as_os_str()]).output()?;
        }

        Command::new("git").current_dir(&project_dir).args(["commit", "-m", &matches.msg]).output()?;
        Command::new("git").current_dir(&project_dir).args(["tag", &format!("v{}", new_ver), "-m", &matches.msg]).output()?;
    }

    println!("New version: v{new_ver}");

    Ok(())
}

fn main() {
    error_trap().unwrap();
}