autocrate/src/config/cli.rs

121 lines
3.1 KiB
Rust
Raw Normal View History

2024-01-27 23:26:49 +01:00
use std::fmt::Display;
2024-01-24 23:04:01 +01:00
use libpt::log::{Level, Logger};
2024-01-27 23:26:49 +01:00
use clap::{Parser, Subcommand};
2024-01-24 23:04:01 +01:00
use clap_verbosity_flag::{InfoLevel, Verbosity};
#[derive(Debug, Clone, Parser)]
#[command(
author,
version,
2024-01-27 23:26:49 +01:00
about,
long_about,
help_template = r#"{about-section}
2024-01-24 23:04:01 +01:00
{usage-heading} {usage}
{all-args}{tab}
autocrate: {version}
Author: {author-with-newline}
"#
2024-01-27 23:26:49 +01:00
)]
/// Release Manager for Your Projects on Gitea, GitHub, and GitLab.
2024-01-24 23:04:01 +01:00
pub struct Cli {
// clap_verbosity_flag seems to make this a global option implicitly
/// set a verbosity, multiple allowed (f.e. -vvv)
#[command(flatten)]
2024-01-27 23:26:49 +01:00
pub verbose: Verbosity<InfoLevel>,
2024-01-24 23:04:01 +01:00
/// show additional logging meta data
#[arg(long)]
2024-01-27 23:26:49 +01:00
pub meta: bool,
/// the subcommands are part of this enum
#[command(subcommand)]
pub command: Commands,
}
#[derive(Debug, Clone, Subcommand)]
pub enum Commands {
Changelog {},
2024-02-24 13:24:14 +01:00
/// Create a new release on the server
Release {
2024-01-27 23:26:49 +01:00
// FIXME: allow taking a message like this:
// `autocrate changelog -m arg1 arg2 arg3`
// -> msg="arg1 arg2 arg3"
// Instead of only
// `autocrate changelog -m "arg1 arg2 arg3"`
// -> msg="arg1 arg2 arg3"
//
// TODO:
// Perhaps open the $EDITOR of the user if
// no message is provided, like git does
//
// TODO:
// find a way to make this a global option but only usable with specific subcommands
2024-02-19 22:07:51 +01:00
//
// TODO:
// integrate a CHANGELOG.md file
2024-02-24 13:24:14 +01:00
//
/// Message body of the release
#[arg(short, long)]
message: Option<Vec<String>>,
2024-02-24 13:24:14 +01:00
/// generate and add a changelog
2024-02-25 00:15:11 +01:00
#[arg(short, long)]
2024-02-24 13:24:14 +01:00
changelog: bool,
/// publish after releasing
2024-02-25 00:15:11 +01:00
#[arg(short, long)]
2024-02-24 13:24:14 +01:00
publish: bool,
},
2024-02-24 13:24:14 +01:00
/// Publish to a package registry
Publish {
// see Commands::Release { message }
2024-01-27 23:26:49 +01:00
#[arg(short, long)]
message: Option<Vec<String>>,
},
2024-02-24 13:24:14 +01:00
///
2024-02-19 22:07:51 +01:00
Version {},
2024-02-24 13:24:14 +01:00
Init {},
2024-01-27 23:26:49 +01:00
}
impl Display for Commands {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{}",
match self {
Self::Changelog { .. } => "Changelog",
Self::Release { .. } => "Release",
Self::Publish { .. } => "Publish",
2024-02-19 22:07:51 +01:00
Self::Version { .. } => "Version",
2024-02-24 13:24:14 +01:00
Self::Init { .. } => "Init",
2024-01-27 23:26:49 +01:00
}
)
}
2024-01-24 23:04:01 +01:00
}
impl Cli {
pub fn cli_parse() -> Self {
let cli = Self::parse();
let ll: Level = match cli.verbose.log_level().unwrap().as_str() {
"TRACE" => Level::TRACE,
"DEBUG" => Level::DEBUG,
"INFO" => Level::INFO,
"WARN" => Level::WARN,
"ERROR" => Level::ERROR,
_ => {
unreachable!();
}
};
if cli.meta {
Logger::init(None, Some(ll), true).expect("could not initialize Logger");
} else {
// less verbose version
Logger::init_mini(Some(ll)).expect("could not initialize Logger");
}
2024-02-16 15:43:33 +00:00
cli
2024-01-24 23:04:01 +01:00
}
}