use cargo_next::{bump_version, get_version, set_version, SemVer};
use clap::Parser;
use std::{env::current_dir, io, process::exit};
#[derive(Debug, Parser)]
#[clap(
author,
bin_name("cargo-next"),
version
)]
enum Cli {
#[clap(name = "next")]
Next(Args),
}
#[derive(Debug, Parser)]
struct Args {
#[clap(long)]
pub get: bool,
#[clap(long)]
pub major: bool,
#[clap(long)]
pub minor: bool,
#[clap(long)]
pub patch: bool,
pub version: Option<String>,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let cli = Cli::parse();
let mut cli = match cli {
Cli::Next(args) => args,
};
if !cli.major && !cli.minor && !cli.patch && !cli.get && cli.version.is_none() {
let mut piped = String::new();
io::stdin().read_line(&mut piped)?;
let piped_trim = piped.trim();
if !piped_trim.is_empty() {
cli.version = Some(piped_trim.to_string());
}
}
let cargo_project_dir_path = current_dir()?;
let cargo_toml_file_path = cargo_project_dir_path.join("Cargo.toml");
if !cargo_toml_file_path.exists() {
eprintln!("Not inside a cargo project folder!");
exit(1);
}
if cli.get {
println!("{}", get_version(&cargo_toml_file_path)?);
} else if cli.major {
bump_version(&cargo_toml_file_path, SemVer::Major)?;
} else if cli.minor {
bump_version(&cargo_toml_file_path, SemVer::Minor)?;
} else if cli.patch {
bump_version(&cargo_toml_file_path, SemVer::Patch)?;
} else {
set_version(&cargo_toml_file_path, cli.version.unwrap())?;
}
Ok(())
}