Make `--offline` a global argument (#3729)

This commit is contained in:
Charlie Marsh 2024-05-21 20:09:05 -04:00 committed by GitHub
parent 285adaed64
commit d33577fc16
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 60 additions and 129 deletions

View File

@ -42,6 +42,7 @@ impl Combine for Options {
fn combine(self, other: Options) -> Options { fn combine(self, other: Options) -> Options {
Options { Options {
native_tls: self.native_tls.combine(other.native_tls), native_tls: self.native_tls.combine(other.native_tls),
offline: self.offline.combine(other.offline),
no_cache: self.no_cache.combine(other.no_cache), no_cache: self.no_cache.combine(other.no_cache),
preview: self.preview.combine(other.preview), preview: self.preview.combine(other.preview),
cache_dir: self.cache_dir.combine(other.cache_dir), cache_dir: self.cache_dir.combine(other.cache_dir),
@ -68,7 +69,6 @@ impl Combine for PipOptions {
.break_system_packages .break_system_packages
.combine(other.break_system_packages), .combine(other.break_system_packages),
target: self.target.combine(other.target), target: self.target.combine(other.target),
offline: self.offline.combine(other.offline),
index_url: self.index_url.combine(other.index_url), index_url: self.index_url.combine(other.index_url),
extra_index_url: self.extra_index_url.combine(other.extra_index_url), extra_index_url: self.extra_index_url.combine(other.extra_index_url),
no_index: self.no_index.combine(other.no_index), no_index: self.no_index.combine(other.no_index),

View File

@ -32,6 +32,7 @@ pub(crate) struct Tools {
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))] #[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub struct Options { pub struct Options {
pub native_tls: Option<bool>, pub native_tls: Option<bool>,
pub offline: Option<bool>,
pub no_cache: Option<bool>, pub no_cache: Option<bool>,
pub preview: Option<bool>, pub preview: Option<bool>,
pub cache_dir: Option<PathBuf>, pub cache_dir: Option<PathBuf>,
@ -48,7 +49,6 @@ pub struct PipOptions {
pub system: Option<bool>, pub system: Option<bool>,
pub break_system_packages: Option<bool>, pub break_system_packages: Option<bool>,
pub target: Option<PathBuf>, pub target: Option<PathBuf>,
pub offline: Option<bool>,
pub index_url: Option<IndexUrl>, pub index_url: Option<IndexUrl>,
pub extra_index_url: Option<Vec<IndexUrl>>, pub extra_index_url: Option<Vec<IndexUrl>>,
pub no_index: Option<bool>, pub no_index: Option<bool>,

View File

@ -81,6 +81,13 @@ pub(crate) struct GlobalArgs {
#[arg(global = true, long, overrides_with("native_tls"), hide = true)] #[arg(global = true, long, overrides_with("native_tls"), hide = true)]
pub(crate) no_native_tls: bool, pub(crate) no_native_tls: bool,
/// Disable network access, relying only on locally cached data and locally available files.
#[arg(global = true, long, overrides_with("no_offline"))]
pub(crate) offline: bool,
#[arg(global = true, long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
/// Whether to enable experimental, preview features. /// Whether to enable experimental, preview features.
#[arg(global = true, long, hide = true, env = "UV_PREVIEW", value_parser = clap::builder::BoolishValueParser::new(), overrides_with("no_preview"))] #[arg(global = true, long, hide = true, env = "UV_PREVIEW", value_parser = clap::builder::BoolishValueParser::new(), overrides_with("no_preview"))]
pub(crate) preview: bool, pub(crate) preview: bool,
@ -366,23 +373,17 @@ pub(crate) struct PipCompileArgs {
pub(crate) custom_compile_command: Option<String>, pub(crate) custom_compile_command: Option<String>,
/// Run offline, i.e., without accessing the network. /// Run offline, i.e., without accessing the network.
#[arg(
global = true,
long,
conflicts_with = "refresh",
conflicts_with = "refresh_package",
overrides_with("no_offline")
)]
pub(crate) offline: bool,
#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
/// Refresh all cached data. /// Refresh all cached data.
#[arg(long, overrides_with("no_refresh"))] #[arg(long, conflicts_with("offline"), overrides_with("no_refresh"))]
pub(crate) refresh: bool, pub(crate) refresh: bool,
#[arg(long, overrides_with("refresh"), hide = true)] #[arg(
long,
conflicts_with("offline"),
overrides_with("refresh"),
hide = true
)]
pub(crate) no_refresh: bool, pub(crate) no_refresh: bool,
/// Refresh cached data for a specific package. /// Refresh cached data for a specific package.
@ -649,23 +650,16 @@ pub(crate) struct PipSyncArgs {
#[arg(long)] #[arg(long)]
pub(crate) reinstall_package: Vec<PackageName>, pub(crate) reinstall_package: Vec<PackageName>,
#[arg(
global = true,
long,
conflicts_with = "refresh",
conflicts_with = "refresh_package",
overrides_with("no_offline")
)]
pub(crate) offline: bool,
#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
/// Refresh all cached data. /// Refresh all cached data.
#[arg(long, overrides_with("no_refresh"))] #[arg(long, conflicts_with("offline"), overrides_with("no_refresh"))]
pub(crate) refresh: bool, pub(crate) refresh: bool,
#[arg(long, overrides_with("refresh"), hide = true)] #[arg(
long,
conflicts_with("offline"),
overrides_with("refresh"),
hide = true
)]
pub(crate) no_refresh: bool, pub(crate) no_refresh: bool,
/// Refresh cached data for a specific package. /// Refresh cached data for a specific package.
@ -1013,23 +1007,16 @@ pub(crate) struct PipInstallArgs {
#[arg(long)] #[arg(long)]
pub(crate) reinstall_package: Vec<PackageName>, pub(crate) reinstall_package: Vec<PackageName>,
#[arg(
global = true,
long,
conflicts_with = "refresh",
conflicts_with = "refresh_package",
overrides_with("no_offline")
)]
pub(crate) offline: bool,
#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
/// Refresh all cached data. /// Refresh all cached data.
#[arg(long, overrides_with("no_refresh"))] #[arg(long, conflicts_with("offline"), overrides_with("no_refresh"))]
pub(crate) refresh: bool, pub(crate) refresh: bool,
#[arg(long, overrides_with("refresh"), hide = true)] #[arg(
long,
conflicts_with("offline"),
overrides_with("refresh"),
hide = true
)]
pub(crate) no_refresh: bool, pub(crate) no_refresh: bool,
/// Refresh cached data for a specific package. /// Refresh cached data for a specific package.
@ -1430,13 +1417,6 @@ pub(crate) struct PipUninstallArgs {
/// or system Python interpreter. /// or system Python interpreter.
#[arg(long)] #[arg(long)]
pub(crate) target: Option<PathBuf>, pub(crate) target: Option<PathBuf>,
/// Run offline, i.e., without accessing the network.
#[arg(long, overrides_with("no_offline"))]
pub(crate) offline: bool,
#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
} }
#[derive(Args)] #[derive(Args)]
@ -1808,13 +1788,6 @@ pub(crate) struct VenvArgs {
#[arg(long, value_enum, env = "UV_KEYRING_PROVIDER")] #[arg(long, value_enum, env = "UV_KEYRING_PROVIDER")]
pub(crate) keyring_provider: Option<KeyringProviderType>, pub(crate) keyring_provider: Option<KeyringProviderType>,
/// Run offline, i.e., without accessing the network.
#[arg(long, overrides_with("no_offline"))]
pub(crate) offline: bool,
#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
/// Limit candidate packages to those that were uploaded prior to the given date. /// Limit candidate packages to those that were uploaded prior to the given date.
/// ///
/// Accepts both RFC 3339 timestamps (e.g., `2006-12-02T02:07:43Z`) and UTC dates in the same /// Accepts both RFC 3339 timestamps (e.g., `2006-12-02T02:07:43Z`) and UTC dates in the same
@ -1840,13 +1813,6 @@ pub(crate) struct RunArgs {
#[arg(long)] #[arg(long)]
pub(crate) with: Vec<String>, pub(crate) with: Vec<String>,
/// Run offline, i.e., without accessing the network.
#[arg(global = true, long, overrides_with("no_offline"))]
pub(crate) offline: bool,
#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
/// The Python interpreter to use to build the run environment. /// The Python interpreter to use to build the run environment.
/// ///
/// By default, `uv` uses the virtual environment in the current working directory or any parent /// By default, `uv` uses the virtual environment in the current working directory or any parent
@ -1961,11 +1927,4 @@ pub(crate) struct ToolRunArgs {
group = "discovery" group = "discovery"
)] )]
pub(crate) python: Option<String>, pub(crate) python: Option<String>,
/// Run offline, i.e., without accessing the network.
#[arg(global = true, long, overrides_with("no_offline"))]
pub(crate) offline: bool,
#[arg(long, overrides_with("offline"), hide = true)]
pub(crate) no_offline: bool,
} }

View File

@ -7,12 +7,11 @@ use anstream::eprintln;
use anyhow::Result; use anyhow::Result;
use clap::error::{ContextKind, ContextValue}; use clap::error::{ContextKind, ContextValue};
use clap::{CommandFactory, Parser}; use clap::{CommandFactory, Parser};
use cli::{ToolCommand, ToolNamespace};
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
use tracing::instrument; use tracing::instrument;
use cli::{ToolCommand, ToolNamespace};
use uv_cache::Cache; use uv_cache::Cache;
use uv_client::Connectivity;
use uv_requirements::RequirementsSource; use uv_requirements::RequirementsSource;
use uv_workspace::Combine; use uv_workspace::Combine;
@ -227,7 +226,7 @@ async fn run() -> Result<ExitStatus> {
args.shared.keyring_provider, args.shared.keyring_provider,
args.shared.setup_py, args.shared.setup_py,
args.shared.config_setting, args.shared.config_setting,
args.shared.connectivity, globals.connectivity,
args.shared.no_build_isolation, args.shared.no_build_isolation,
args.shared.no_build, args.shared.no_build,
args.shared.python_version, args.shared.python_version,
@ -277,7 +276,7 @@ async fn run() -> Result<ExitStatus> {
args.shared.index_strategy, args.shared.index_strategy,
args.shared.keyring_provider, args.shared.keyring_provider,
args.shared.setup_py, args.shared.setup_py,
args.shared.connectivity, globals.connectivity,
&args.shared.config_setting, &args.shared.config_setting,
args.shared.no_build_isolation, args.shared.no_build_isolation,
args.shared.no_build, args.shared.no_build,
@ -350,7 +349,7 @@ async fn run() -> Result<ExitStatus> {
args.shared.compile_bytecode, args.shared.compile_bytecode,
args.shared.require_hashes, args.shared.require_hashes,
args.shared.setup_py, args.shared.setup_py,
args.shared.connectivity, globals.connectivity,
&args.shared.config_setting, &args.shared.config_setting,
args.shared.no_build_isolation, args.shared.no_build_isolation,
args.shared.no_build, args.shared.no_build,
@ -399,7 +398,7 @@ async fn run() -> Result<ExitStatus> {
args.shared.break_system_packages, args.shared.break_system_packages,
args.shared.target, args.shared.target,
cache, cache,
args.shared.connectivity, globals.connectivity,
globals.native_tls, globals.native_tls,
globals.preview, globals.preview,
args.shared.keyring_provider, args.shared.keyring_provider,
@ -522,7 +521,7 @@ async fn run() -> Result<ExitStatus> {
args.shared.keyring_provider, args.shared.keyring_provider,
uv_virtualenv::Prompt::from_args(prompt), uv_virtualenv::Prompt::from_args(prompt),
args.system_site_packages, args.system_site_packages,
args.shared.connectivity, globals.connectivity,
args.seed, args.seed,
args.allow_existing, args.allow_existing,
args.shared.exclude_newer, args.shared.exclude_newer,
@ -566,7 +565,7 @@ async fn run() -> Result<ExitStatus> {
args.python, args.python,
globals.isolated, globals.isolated,
globals.preview, globals.preview,
args.connectivity, globals.connectivity,
&cache, &cache,
printer, printer,
) )
@ -605,18 +604,13 @@ async fn run() -> Result<ExitStatus> {
Commands::Tool(ToolNamespace { Commands::Tool(ToolNamespace {
command: ToolCommand::Run(args), command: ToolCommand::Run(args),
}) => { }) => {
let connectivity = if args.offline {
Connectivity::Offline
} else {
Connectivity::Online
};
commands::run_tool( commands::run_tool(
args.target, args.target,
args.args, args.args,
args.python, args.python,
globals.isolated, globals.isolated,
globals.preview, globals.preview,
connectivity, globals.connectivity,
&cache, &cache,
printer, printer,
) )

View File

@ -34,6 +34,7 @@ pub(crate) struct GlobalSettings {
pub(crate) verbose: u8, pub(crate) verbose: u8,
pub(crate) color: ColorChoice, pub(crate) color: ColorChoice,
pub(crate) native_tls: bool, pub(crate) native_tls: bool,
pub(crate) connectivity: Connectivity,
pub(crate) isolated: bool, pub(crate) isolated: bool,
pub(crate) preview: PreviewMode, pub(crate) preview: PreviewMode,
} }
@ -52,6 +53,14 @@ impl GlobalSettings {
native_tls: flag(args.native_tls, args.no_native_tls) native_tls: flag(args.native_tls, args.no_native_tls)
.combine(workspace.and_then(|workspace| workspace.options.native_tls)) .combine(workspace.and_then(|workspace| workspace.options.native_tls))
.unwrap_or(false), .unwrap_or(false),
connectivity: if flag(args.offline, args.no_offline)
.combine(workspace.and_then(|workspace| workspace.options.offline))
.unwrap_or(false)
{
Connectivity::Offline
} else {
Connectivity::Online
},
isolated: args.isolated, isolated: args.isolated,
preview: PreviewMode::from( preview: PreviewMode::from(
flag(args.preview, args.no_preview) flag(args.preview, args.no_preview)
@ -94,10 +103,6 @@ pub(crate) struct RunSettings {
pub(crate) args: Vec<OsString>, pub(crate) args: Vec<OsString>,
pub(crate) with: Vec<String>, pub(crate) with: Vec<String>,
pub(crate) python: Option<String>, pub(crate) python: Option<String>,
// Shared settings.
// TODO(zanieb): should be moved to a global setting
pub(crate) connectivity: Connectivity,
} }
impl RunSettings { impl RunSettings {
@ -109,8 +114,6 @@ impl RunSettings {
args, args,
with, with,
python, python,
offline,
no_offline,
} = args; } = args;
Self { Self {
@ -119,16 +122,6 @@ impl RunSettings {
args, args,
with, with,
python, python,
// Shared settings
connectivity: flag(offline, no_offline)
.map(|offline| {
if offline {
Connectivity::Offline
} else {
Connectivity::Online
}
})
.unwrap_or_default(),
} }
} }
} }
@ -215,8 +208,7 @@ impl PipCompileSettings {
header, header,
annotation_style, annotation_style,
custom_compile_command, custom_compile_command,
offline,
no_offline,
refresh, refresh,
no_refresh, no_refresh,
refresh_package, refresh_package,
@ -277,7 +269,7 @@ impl PipCompileSettings {
PipOptions { PipOptions {
python, python,
system: flag(system, no_system), system: flag(system, no_system),
offline: flag(offline, no_offline),
index_url: index_url.and_then(Maybe::into_option), index_url: index_url.and_then(Maybe::into_option),
extra_index_url: extra_index_url.map(|extra_index_urls| { extra_index_url: extra_index_url.map(|extra_index_urls| {
extra_index_urls extra_index_urls
@ -353,9 +345,7 @@ impl PipSyncSettings {
reinstall, reinstall,
no_reinstall, no_reinstall,
reinstall_package, reinstall_package,
offline,
refresh, refresh,
no_offline,
no_refresh, no_refresh,
refresh_package, refresh_package,
link_mode, link_mode,
@ -404,7 +394,7 @@ impl PipSyncSettings {
system: flag(system, no_system), system: flag(system, no_system),
break_system_packages: flag(break_system_packages, no_break_system_packages), break_system_packages: flag(break_system_packages, no_break_system_packages),
target, target,
offline: flag(offline, no_offline),
index_url: index_url.and_then(Maybe::into_option), index_url: index_url.and_then(Maybe::into_option),
extra_index_url: extra_index_url.map(|extra_index_urls| { extra_index_url: extra_index_url.map(|extra_index_urls| {
extra_index_urls extra_index_urls
@ -478,9 +468,7 @@ impl PipInstallSettings {
reinstall, reinstall,
no_reinstall, no_reinstall,
reinstall_package, reinstall_package,
offline,
refresh, refresh,
no_offline,
no_refresh, no_refresh,
refresh_package, refresh_package,
no_deps, no_deps,
@ -547,7 +535,7 @@ impl PipInstallSettings {
system: flag(system, no_system), system: flag(system, no_system),
break_system_packages: flag(break_system_packages, no_break_system_packages), break_system_packages: flag(break_system_packages, no_break_system_packages),
target, target,
offline: flag(offline, no_offline),
index_url: index_url.and_then(Maybe::into_option), index_url: index_url.and_then(Maybe::into_option),
extra_index_url: extra_index_url.map(|extra_index_urls| { extra_index_url: extra_index_url.map(|extra_index_urls| {
extra_index_urls extra_index_urls
@ -618,8 +606,6 @@ impl PipUninstallSettings {
break_system_packages, break_system_packages,
no_break_system_packages, no_break_system_packages,
target, target,
offline,
no_offline,
} = args; } = args;
Self { Self {
@ -634,7 +620,7 @@ impl PipUninstallSettings {
system: flag(system, no_system), system: flag(system, no_system),
break_system_packages: flag(break_system_packages, no_break_system_packages), break_system_packages: flag(break_system_packages, no_break_system_packages),
target, target,
offline: flag(offline, no_offline),
keyring_provider, keyring_provider,
..PipOptions::default() ..PipOptions::default()
}, },
@ -842,8 +828,7 @@ impl VenvSettings {
no_index, no_index,
index_strategy, index_strategy,
keyring_provider, keyring_provider,
offline,
no_offline,
exclude_newer, exclude_newer,
compat_args: _, compat_args: _,
} = args; } = args;
@ -861,7 +846,7 @@ impl VenvSettings {
PipOptions { PipOptions {
python, python,
system: flag(system, no_system), system: flag(system, no_system),
offline: flag(offline, no_offline),
index_url: index_url.and_then(Maybe::into_option), index_url: index_url.and_then(Maybe::into_option),
extra_index_url: extra_index_url.map(|extra_index_urls| { extra_index_url: extra_index_url.map(|extra_index_urls| {
extra_index_urls extra_index_urls
@ -894,7 +879,6 @@ pub(crate) struct PipSharedSettings {
pub(crate) extras: ExtrasSpecification, pub(crate) extras: ExtrasSpecification,
pub(crate) break_system_packages: bool, pub(crate) break_system_packages: bool,
pub(crate) target: Option<Target>, pub(crate) target: Option<Target>,
pub(crate) connectivity: Connectivity,
pub(crate) index_strategy: IndexStrategy, pub(crate) index_strategy: IndexStrategy,
pub(crate) keyring_provider: KeyringProviderType, pub(crate) keyring_provider: KeyringProviderType,
pub(crate) no_binary: NoBinary, pub(crate) no_binary: NoBinary,
@ -935,7 +919,6 @@ impl PipSharedSettings {
system, system,
break_system_packages, break_system_packages,
target, target,
offline,
index_url, index_url,
extra_index_url, extra_index_url,
no_index, no_index,
@ -1011,11 +994,6 @@ impl PipSharedSettings {
.annotation_style .annotation_style
.combine(annotation_style) .combine(annotation_style)
.unwrap_or_default(), .unwrap_or_default(),
connectivity: if args.offline.combine(offline).unwrap_or_default() {
Connectivity::Offline
} else {
Connectivity::Online
},
index_strategy: args index_strategy: args
.index_strategy .index_strategy
.combine(index_strategy) .combine(index_strategy)

12
uv.schema.json generated
View File

@ -22,6 +22,12 @@
"null" "null"
] ]
}, },
"offline": {
"type": [
"boolean",
"null"
]
},
"pip": { "pip": {
"anyOf": [ "anyOf": [
{ {
@ -460,12 +466,6 @@
"null" "null"
] ]
}, },
"offline": {
"type": [
"boolean",
"null"
]
},
"only-binary": { "only-binary": {
"type": [ "type": [
"array", "array",