Skip to content

Commit

Permalink
handle env vars with flags
Browse files Browse the repository at this point in the history
we had to make our own colorchoice clap handler
because the mixin doesn't work with Option<_>
  • Loading branch information
suaviloquence committed Apr 14, 2024
1 parent dd0207b commit 0f05b40
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 33 deletions.
11 changes: 0 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ rustc_version = "0.4.0"
rayon = "1.8.0"
anstyle = "1.0.6"
anstream = "0.6.13"
colorchoice-clap = "1.0.3"

[dev-dependencies]
assert_cmd = "2.0"
Expand Down
47 changes: 26 additions & 21 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use std::{env, path::PathBuf};

use anstream::ColorChoice;
use cargo_semver_checks::{
GlobalConfig, PackageSelection, ReleaseType, Rustdoc, ScopeSelection, SemverQuery,
};
Expand All @@ -13,7 +12,7 @@ fn main() {

let Cargo::SemverChecks(args) = Cargo::parse();

configure_color(args.color_choice.as_choice());
configure_color(args.color_choice);

if args.bugreport {
use bugreport::{bugreport, collector::*, format::Markdown};
Expand Down Expand Up @@ -112,25 +111,29 @@ fn exit_on_error<T>(log_errors: bool, inner: impl Fn() -> anyhow::Result<T>) ->

/// helper function to determine whether to use colors based on the (passed) `--color` flag
/// and the value of the `CARGO_TERM_COLOR` variable.
fn configure_color(cli_choice: ColorChoice) {
// if the --color flag is explicitly set to something other than `auto`,
// this takes precedence over the environment variable, so we write that
if cli_choice != ColorChoice::Auto {
cli_choice.write_global();
} else {
///
/// If the `--color` flag is set to something valid, it overrides anything in
/// the `CARGO_TERM_COLOR` environment variable
fn configure_color(cli_choice: Option<clap::ColorChoice>) {
use anstream::ColorChoice as AnstreamChoice;
use clap::ColorChoice as ClapChoice;
let choice = match cli_choice {
Some(ClapChoice::Always) => AnstreamChoice::Always,
Some(ClapChoice::Auto) => AnstreamChoice::Auto,
Some(ClapChoice::Never) => AnstreamChoice::Never,
// we match the behavior of cargo in
// https://doc.rust-lang.org/cargo/reference/config.html#termcolor
// note that [`ColorChoice::AlwaysAnsi`] is not supported by cargo.
match env::var("CARGO_TERM_COLOR").as_deref() {
Ok("always") => ColorChoice::Always.write_global(),
Ok("never") => ColorChoice::Never.write_global(),
// note: the default global color value is `Auto`
Ok("auto") => ColorChoice::Auto.write_global(),
// ignore invalid or not set environment variables,
// color choice will default to `Auto`
_ => (),
};
}
None => match env::var("CARGO_TERM_COLOR").as_deref() {
Ok("always") => AnstreamChoice::Always,
Ok("never") => AnstreamChoice::Never,
// if `auto` is set, or the env var is invalid
// or both the env var and flag are not set, we set the choice to auto
_ => AnstreamChoice::Auto,
},
};

choice.write_global();
}

#[derive(Debug, Parser)]
Expand Down Expand Up @@ -159,9 +162,11 @@ struct SemverChecks {
#[command(subcommand)]
command: Option<SemverChecksCommands>,

// docstring for help is on the `colorchoice_clap::Color` struct itself
#[command(flatten)]
color_choice: colorchoice_clap::Color,
// we need to use clap::ColorChoice instead of anstream::ColorChoice
// because ValueEnum is implemented for it.
/// Choose whether to output colors
#[arg(long = "color", global = true, value_name = "WHEN", value_enum)]
color_choice: Option<clap::ColorChoice>,
}

/// Check your crate for semver violations.
Expand Down

0 comments on commit 0f05b40

Please sign in to comment.