#![cfg_attr(test, allow(clippy::unwrap_used, clippy::expect_used))]
mod banner;
mod commands;
mod error;
mod policy_io;
mod progress;
mod summary;
mod templates;
use clap::{Parser, Subcommand, ValueEnum};
use commands::{
init::{self, InitArgs},
report::{self, ReportArgs},
run::{self, RunArgs},
schema::{self, SchemaArgs},
summary::{self as summary_cmd, SummaryArgs},
validate::{self, ValidateArgs},
version,
};
#[derive(Clone, Copy, Debug, ValueEnum)]
pub(crate) enum ColorWhen {
Auto,
Always,
Never,
}
pub(crate) fn resolve_color(cli_color: ColorWhen) {
match cli_color {
ColorWhen::Always => console::set_colors_enabled_stderr(true),
ColorWhen::Never => console::set_colors_enabled_stderr(false),
ColorWhen::Auto => {
if let Ok(val) = std::env::var("COBRE_COLOR") {
match val.to_ascii_lowercase().as_str() {
"always" => console::set_colors_enabled_stderr(true),
"never" => console::set_colors_enabled_stderr(false),
_ => {} }
} else if std::env::var_os("FORCE_COLOR").is_some() {
console::set_colors_enabled_stderr(true);
}
}
}
}
#[derive(Debug, Parser)]
#[command(
name = "cobre",
about = "Open infrastructure for power system computation"
)]
struct Cli {
#[arg(long, global = true, default_value = "auto")]
color: ColorWhen,
#[command(subcommand)]
command: Command,
}
#[derive(Debug, Subcommand)]
enum Command {
Init(InitArgs),
Run(RunArgs),
Validate(ValidateArgs),
Report(ReportArgs),
Summary(SummaryArgs),
Schema(SchemaArgs),
Version,
}
fn main() {
let cli = Cli::parse();
resolve_color(cli.color);
let result = match cli.command {
Command::Init(args) => init::execute(args),
Command::Run(ref args) => run::execute(args),
Command::Validate(args) => validate::execute(args),
Command::Report(args) => report::execute(args),
Command::Summary(args) => summary_cmd::execute(args),
Command::Schema(args) => schema::execute(args),
Command::Version => version::execute(),
};
match result {
Ok(()) => std::process::exit(0),
Err(e) => {
e.format_error(&console::Term::stderr());
std::process::exit(e.exit_code());
}
}
}
#[cfg(test)]
mod tests {
use super::{ColorWhen, resolve_color};
#[test]
fn test_resolve_color_always_enables_color() {
resolve_color(ColorWhen::Always);
assert!(
console::colors_enabled_stderr(),
"Always must set colors_enabled_stderr to true"
);
}
#[test]
fn test_resolve_color_never_disables_color() {
resolve_color(ColorWhen::Never);
assert!(
!console::colors_enabled_stderr(),
"Never must set colors_enabled_stderr to false"
);
}
}