use clap::ColorChoice;
use clap::Parser;
use mago_reporting::Level;
use mago_reporting::ReportingFormat;
use mago_reporting::ReportingTarget;
use crate::enum_variants;
use crate::service::IssueProcessor;
#[derive(Parser, Debug, Clone)]
pub struct ReportingArgs {
#[arg(long, short = 'f')]
pub fixable_only: bool,
#[arg(long)]
pub sort: bool,
#[arg(long, conflicts_with = "fixable_only")]
pub fix: bool,
#[arg(long, requires = "fix")]
pub r#unsafe: bool,
#[arg(long, requires = "fix")]
pub potentially_unsafe: bool,
#[arg(long, alias = "fmt", requires = "fix")]
pub format_after_fix: bool,
#[arg(long, short = 'd', requires = "fix", alias = "diff")]
pub dry_run: bool,
#[arg(long, requires = "fix")]
pub fail_on_remaining: bool,
#[arg(
long,
default_value_t,
ignore_case = true,
value_parser = enum_variants!(ReportingTarget),
conflicts_with = "fix"
)]
pub reporting_target: ReportingTarget,
#[arg(
long,
default_value_t = default_reporting_format(),
ignore_case = true,
value_parser = enum_variants!(ReportingFormat),
conflicts_with = "fix"
)]
pub reporting_format: ReportingFormat,
#[arg(
long,
short = 'm',
value_parser = enum_variants!(Level),
conflicts_with = "fix"
)]
pub minimum_fail_level: Option<Level>,
#[arg(
long,
value_parser = enum_variants!(Level)
)]
pub minimum_report_level: Option<Level>,
#[arg(long, value_name = "CODE")]
pub retain_code: Vec<String>,
}
impl ReportingArgs {
pub fn get_processor(
&self,
color_choice: ColorChoice,
editor_url: Option<String>,
config_minimum_fail_level: Level,
) -> IssueProcessor {
IssueProcessor {
fixable_only: self.fixable_only,
sort: self.sort,
fix: self.fix,
r#unsafe: self.r#unsafe,
potentially_unsafe: self.potentially_unsafe,
format_after_fix: self.format_after_fix,
dry_run: self.dry_run,
fail_on_remaining: self.fail_on_remaining,
reporting_target: self.reporting_target.clone(),
reporting_format: self.reporting_format,
minimum_fail_level: self.minimum_fail_level.unwrap_or(config_minimum_fail_level),
minimum_report_level: self.minimum_report_level,
retain_code: self.retain_code.clone(),
color_choice,
editor_url,
}
}
}
fn default_reporting_format() -> ReportingFormat {
if is_github_actions() {
ReportingFormat::Github
} else if is_gitlab_ci() {
ReportingFormat::Gitlab
} else if is_slop_environment() {
ReportingFormat::Medium
} else {
ReportingFormat::default()
}
}
fn is_github_actions() -> bool {
std::env::var_os("GITHUB_ACTIONS").is_some()
}
fn is_gitlab_ci() -> bool {
std::env::var_os("GITLAB_CI").is_some()
}
fn is_slop_environment() -> bool {
std::env::var_os("CLAUDECODE").is_some()
|| std::env::var_os("GEMINI_CLI").is_some()
|| std::env::var_os("CODEX_SANDBOX").is_some()
|| std::env::var_os("OPENCODE_CLIENT").is_some()
}