pub mod commands;
pub mod filter;
pub(crate) mod namespace_resolution;
use clap::{Parser, Subcommand, ValueEnum};
use crate::Result;
#[derive(Debug, Clone, Copy, Default, ValueEnum)]
pub enum ColorChoice {
#[default]
Auto,
Always,
Never,
}
#[derive(Parser, Debug)]
#[command(name = "nyl")]
#[command(version, about, long_about = None)]
pub struct Cli {
#[command(subcommand)]
command: Commands,
#[arg(short, long, global = true)]
pub verbose: bool,
#[arg(long, value_enum, default_value = "auto", global = true)]
pub color: ColorChoice,
}
#[derive(Subcommand, Debug)]
enum Commands {
Render(commands::render::RenderArgs),
Diff(commands::diff::DiffArgs),
Apply(commands::apply::ApplyArgs),
Generate(commands::generate::GenerateArgs),
New(commands::new::NewArgs),
Validate(commands::validate::ValidateArgs),
ClusterInfo(commands::cluster_info::ClusterInfoArgs),
Release(commands::release::ReleaseArgs),
}
impl Cli {
pub fn render_input_path(&self) -> Option<&str> {
match &self.command {
Commands::Render(args) => Some(args.common.path.as_str()),
_ => None,
}
}
pub async fn execute(self) -> Result<()> {
match self.command {
Commands::Render(args) => commands::render::execute(args).await,
Commands::Diff(args) => commands::diff::execute(args).await,
Commands::Apply(args) => commands::apply::execute(args).await,
Commands::Generate(args) => commands::generate::execute(args),
Commands::New(args) => commands::new::execute(args),
Commands::Validate(args) => commands::validate::execute(args),
Commands::ClusterInfo(args) => commands::cluster_info::execute(args).await,
Commands::Release(args) => commands::release::execute(args).await,
}
}
}
impl ColorChoice {
pub fn apply(&self) {
match self {
ColorChoice::Auto => {
colored::control::unset_override();
}
ColorChoice::Always => {
colored::control::set_override(true);
}
ColorChoice::Never => {
colored::control::set_override(false);
}
}
}
pub fn should_use_ansi(&self) -> bool {
match self {
ColorChoice::Auto => {
std::io::IsTerminal::is_terminal(&std::io::stderr())
}
ColorChoice::Always => true,
ColorChoice::Never => false,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_color_choice_default() {
let choice = ColorChoice::default();
assert!(matches!(choice, ColorChoice::Auto));
}
#[test]
fn test_color_choice_apply_always() {
let initial = colored::control::SHOULD_COLORIZE.should_colorize();
ColorChoice::Always.apply();
assert!(colored::control::SHOULD_COLORIZE.should_colorize());
colored::control::unset_override();
let _ = initial;
}
#[test]
fn test_color_choice_apply_never() {
let initial = colored::control::SHOULD_COLORIZE.should_colorize();
ColorChoice::Never.apply();
assert!(!colored::control::SHOULD_COLORIZE.should_colorize());
colored::control::unset_override();
let _ = initial;
}
#[test]
fn test_color_choice_apply_auto() {
ColorChoice::Auto.apply();
let _ = colored::control::SHOULD_COLORIZE.should_colorize();
}
}