use std::{path::PathBuf, process::ExitCode};
use clap::{Args, Parser, Subcommand, crate_version};
use semver::Version;
use yact::{Error, init, pre_commit};
#[derive(Parser)]
#[command(version, about)]
pub struct Cli {
#[command(subcommand)]
command: Option<Command>,
#[arg(short, long, value_name = "path to workspace")]
path: Option<PathBuf>,
}
#[derive(Subcommand)]
pub enum Command {
PreCommit,
Init(InitArgs),
}
#[derive(Args)]
pub struct InitArgs {
#[arg(short, long, default_value_t = false)]
force: bool,
}
pub fn main() -> ExitCode {
let args = Cli::parse();
let result = match args.command {
None | Some(Command::PreCommit) => {
let version = Version::parse(crate_version!()).ok();
pre_commit(args.path.unwrap_or(PathBuf::from(".")), version)
}
Some(Command::Init(init_args)) => {
init(args.path.unwrap_or(PathBuf::from(".")), init_args.force)
}
};
match result {
Err(Error::EmptyIndex) => {
eprintln!("Aborting commit; all staged changes were unwanted formatting changes.");
ExitCode::FAILURE
}
Err(Error::TransformerError(message)) => {
eprintln!(
"Error occurred in one of the pre-commit transformers: {}",
message
);
ExitCode::FAILURE
}
Err(Error::RepositoryNotFound) => {
eprintln!(
"Repository not found in current or parent directories. yact must be run from within a git repository."
);
ExitCode::FAILURE
}
Err(Error::UnableToDetermineYactPath) => {
eprintln!("Unable to determine path to yact executable.");
ExitCode::FAILURE
}
Err(Error::PreCommitHookAlreadyExists) => {
eprintln!(
"Cannot install yact in pre-commit hook path: member already exists at path. Rerun init command with --force to replace."
);
ExitCode::FAILURE
}
Err(Error::InvalidYactVersion(requirement, version)) => {
eprintln!(
"Repository is configured to require yact version {}, but current version is {}. Please install an appropriate version or update the requirement.",
requirement, version
);
ExitCode::FAILURE
}
Err(Error::GitError(err)) => {
eprintln!("Unexpected git error: {}", err);
ExitCode::FAILURE
}
Err(Error::IoError(err)) => {
eprintln!("Unexpected IO error: {}", err);
ExitCode::FAILURE
}
Err(Error::ConfigurationParseError(err)) => {
eprintln!("Failed to parse configuration: {}", err);
ExitCode::FAILURE
}
Err(Error::ConfigurationEncodingError(_)) => {
eprintln!("Configuration file was not valid UTF-8.");
ExitCode::FAILURE
}
Err(Error::RepositoryIsBare) => {
eprintln!("Cannot run yact on a bare repository.");
ExitCode::FAILURE
}
Err(Error::ConfigurationNotFound) => {
eprintln!(
"Could not resolve yactrc.toml configuration file. Ensure it is located at the root of the repository"
);
ExitCode::FAILURE
}
Err(Error::InvalidGlob(glob)) => {
eprintln!("Invalid glob found in configuration: '{}'", glob);
ExitCode::FAILURE
}
Ok(_) => ExitCode::SUCCESS,
}
}