|
| 1 | +use std::fmt; |
| 2 | + |
1 | 3 | use anstream::eprintln; |
2 | 4 |
|
3 | 5 | use uv_cache::Refresh; |
4 | 6 | use uv_configuration::{BuildIsolation, Reinstall, Upgrade}; |
5 | 7 | use uv_distribution_types::{ConfigSettings, PackageConfigSettings, Requirement}; |
6 | 8 | use uv_resolver::{ExcludeNewer, ExcludeNewerPackage, PrereleaseMode}; |
7 | | -use uv_settings::{Combine, PipOptions, ResolverInstallerOptions, ResolverOptions}; |
| 9 | +use uv_settings::{Combine, EnvFlag, PipOptions, ResolverInstallerOptions, ResolverOptions}; |
8 | 10 | use uv_warnings::owo_colors::OwoColorize; |
9 | 11 |
|
10 | 12 | use crate::{ |
@@ -37,6 +39,150 @@ pub fn flag(yes: bool, no: bool, name: &str) -> Option<bool> { |
37 | 39 | } |
38 | 40 | } |
39 | 41 |
|
| 42 | +/// The source of a boolean flag value. |
| 43 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 44 | +pub enum FlagSource { |
| 45 | + /// The flag was set via command-line argument. |
| 46 | + Cli, |
| 47 | + /// The flag was set via environment variable. |
| 48 | + Env(&'static str), |
| 49 | + /// The flag was set via workspace/project configuration. |
| 50 | + Config, |
| 51 | +} |
| 52 | + |
| 53 | +impl fmt::Display for FlagSource { |
| 54 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 55 | + match self { |
| 56 | + Self::Cli => write!(f, "command-line argument"), |
| 57 | + Self::Env(name) => write!(f, "environment variable `{name}`"), |
| 58 | + Self::Config => write!(f, "workspace configuration"), |
| 59 | + } |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// A boolean flag value with its source. |
| 64 | +#[derive(Debug, Clone, Copy)] |
| 65 | +pub enum Flag { |
| 66 | + /// The flag is not set. |
| 67 | + Disabled, |
| 68 | + /// The flag is enabled with a known source. |
| 69 | + Enabled { |
| 70 | + source: FlagSource, |
| 71 | + /// The CLI flag name (e.g., "locked" for `--locked`). |
| 72 | + name: &'static str, |
| 73 | + }, |
| 74 | +} |
| 75 | + |
| 76 | +impl Flag { |
| 77 | + /// Create a flag that is explicitly disabled. |
| 78 | + pub const fn disabled() -> Self { |
| 79 | + Self::Disabled |
| 80 | + } |
| 81 | + |
| 82 | + /// Create an enabled flag from a CLI argument. |
| 83 | + pub const fn from_cli(name: &'static str) -> Self { |
| 84 | + Self::Enabled { |
| 85 | + source: FlagSource::Cli, |
| 86 | + name, |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /// Create an enabled flag from workspace/project configuration. |
| 91 | + pub const fn from_config(name: &'static str) -> Self { |
| 92 | + Self::Enabled { |
| 93 | + source: FlagSource::Config, |
| 94 | + name, |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + /// Returns `true` if the flag is set. |
| 99 | + pub fn is_enabled(self) -> bool { |
| 100 | + matches!(self, Self::Enabled { .. }) |
| 101 | + } |
| 102 | + |
| 103 | + /// Returns the source of the flag, if it is set. |
| 104 | + pub fn source(self) -> Option<FlagSource> { |
| 105 | + match self { |
| 106 | + Self::Disabled => None, |
| 107 | + Self::Enabled { source, .. } => Some(source), |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + /// Returns the CLI flag name, if the flag is enabled. |
| 112 | + pub fn name(self) -> Option<&'static str> { |
| 113 | + match self { |
| 114 | + Self::Disabled => None, |
| 115 | + Self::Enabled { name, .. } => Some(name), |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl From<Flag> for bool { |
| 121 | + fn from(flag: Flag) -> Self { |
| 122 | + flag.is_enabled() |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/// Resolve a boolean flag from CLI arguments and an environment variable. |
| 127 | +/// |
| 128 | +/// The CLI argument takes precedence over the environment variable. Returns a [`Flag`] with the |
| 129 | +/// resolved value and source. |
| 130 | +pub fn resolve_flag(cli_flag: bool, name: &'static str, env_flag: EnvFlag) -> Flag { |
| 131 | + if cli_flag { |
| 132 | + Flag::Enabled { |
| 133 | + source: FlagSource::Cli, |
| 134 | + name, |
| 135 | + } |
| 136 | + } else if env_flag.value == Some(true) { |
| 137 | + Flag::Enabled { |
| 138 | + source: FlagSource::Env(env_flag.env_var), |
| 139 | + name, |
| 140 | + } |
| 141 | + } else { |
| 142 | + Flag::Disabled |
| 143 | + } |
| 144 | +} |
| 145 | + |
| 146 | +/// Check if two flags conflict and exit with an error if they do. |
| 147 | +/// |
| 148 | +/// This function checks if both flags are enabled (truthy) and reports an error if so, including |
| 149 | +/// the source of each flag (CLI or environment variable) in the error message. |
| 150 | +pub fn check_conflicts(flag_a: Flag, flag_b: Flag) { |
| 151 | + if let ( |
| 152 | + Flag::Enabled { |
| 153 | + source: source_a, |
| 154 | + name: name_a, |
| 155 | + }, |
| 156 | + Flag::Enabled { |
| 157 | + source: source_b, |
| 158 | + name: name_b, |
| 159 | + }, |
| 160 | + ) = (flag_a, flag_b) |
| 161 | + { |
| 162 | + let display_a = match source_a { |
| 163 | + FlagSource::Cli => format!("`--{name_a}`"), |
| 164 | + FlagSource::Env(env) => format!("`{env}` (environment variable)"), |
| 165 | + FlagSource::Config => format!("`{name_a}` (workspace configuration)"), |
| 166 | + }; |
| 167 | + let display_b = match source_b { |
| 168 | + FlagSource::Cli => format!("`--{name_b}`"), |
| 169 | + FlagSource::Env(env) => format!("`{env}` (environment variable)"), |
| 170 | + FlagSource::Config => format!("`{name_b}` (workspace configuration)"), |
| 171 | + }; |
| 172 | + eprintln!( |
| 173 | + "{}{} the argument {} cannot be used with {}", |
| 174 | + "error".bold().red(), |
| 175 | + ":".bold(), |
| 176 | + display_a.green(), |
| 177 | + display_b.green(), |
| 178 | + ); |
| 179 | + #[allow(clippy::exit)] |
| 180 | + { |
| 181 | + std::process::exit(2); |
| 182 | + } |
| 183 | + } |
| 184 | +} |
| 185 | + |
40 | 186 | impl From<RefreshArgs> for Refresh { |
41 | 187 | fn from(value: RefreshArgs) -> Self { |
42 | 188 | let RefreshArgs { |
|
0 commit comments