mirror of
https://github.com/astral-sh/uv.git
synced 2025-11-19 11:35:36 +00:00
Add --config-file support (#3047)
## Summary Users can now pass a config file on the command line, e.g., with `--config-file /path/to/file.py`.
This commit is contained in:
parent
7fb2bf816f
commit
c6e75f8b35
3 changed files with 33 additions and 3 deletions
|
|
@ -21,7 +21,7 @@ impl Workspace {
|
|||
/// found.
|
||||
pub fn find(path: impl AsRef<Path>) -> Result<Option<Self>, WorkspaceError> {
|
||||
for ancestor in path.as_ref().ancestors() {
|
||||
match read_options(ancestor) {
|
||||
match find_in_directory(ancestor) {
|
||||
Ok(Some(options)) => {
|
||||
return Ok(Some(Self {
|
||||
options,
|
||||
|
|
@ -43,10 +43,18 @@ impl Workspace {
|
|||
}
|
||||
Ok(None)
|
||||
}
|
||||
|
||||
/// Load a [`Workspace`] from a `pyproject.toml` or `uv.toml` file.
|
||||
pub fn from_file(path: impl AsRef<Path>) -> Result<Self, WorkspaceError> {
|
||||
Ok(Self {
|
||||
options: read_file(path.as_ref())?,
|
||||
root: path.as_ref().parent().unwrap().to_path_buf(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
/// Read a `uv.toml` or `pyproject.toml` file in the given directory.
|
||||
fn read_options(dir: &Path) -> Result<Option<Options>, WorkspaceError> {
|
||||
fn find_in_directory(dir: &Path) -> Result<Option<Options>, WorkspaceError> {
|
||||
// Read a `uv.toml` file in the current directory.
|
||||
let path = dir.join("uv.toml");
|
||||
match fs_err::read_to_string(&path) {
|
||||
|
|
@ -94,6 +102,20 @@ fn read_options(dir: &Path) -> Result<Option<Options>, WorkspaceError> {
|
|||
Ok(None)
|
||||
}
|
||||
|
||||
/// Load [`Options`] from a `pyproject.toml` or `ruff.toml` file.
|
||||
fn read_file(path: &Path) -> Result<Options, WorkspaceError> {
|
||||
let content = fs_err::read_to_string(path)?;
|
||||
if path.ends_with("pyproject.toml") {
|
||||
let pyproject: PyProjectToml = toml::from_str(&content)
|
||||
.map_err(|err| WorkspaceError::PyprojectToml(path.user_display().to_string(), err))?;
|
||||
Ok(pyproject.tool.and_then(|tool| tool.uv).unwrap_or_default())
|
||||
} else {
|
||||
let options: Options = toml::from_str(&content)
|
||||
.map_err(|err| WorkspaceError::UvToml(path.user_display().to_string(), err))?;
|
||||
Ok(options)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(thiserror::Error, Debug)]
|
||||
pub enum WorkspaceError {
|
||||
#[error(transparent)]
|
||||
|
|
|
|||
|
|
@ -30,6 +30,10 @@ pub(crate) struct Cli {
|
|||
|
||||
#[command(flatten)]
|
||||
pub(crate) cache_args: CacheArgs,
|
||||
|
||||
/// The path to a `pyproject.toml` or `uv.toml` file to use for configuration.
|
||||
#[arg(long, short, env = "UV_CONFIG_FILE", hide = true)]
|
||||
pub(crate) config_file: Option<PathBuf>,
|
||||
}
|
||||
|
||||
#[derive(Parser, Debug, Clone)]
|
||||
|
|
|
|||
|
|
@ -110,7 +110,11 @@ async fn run() -> Result<ExitStatus> {
|
|||
};
|
||||
|
||||
// Load the workspace settings.
|
||||
let workspace = uv_workspace::Workspace::find(env::current_dir()?)?;
|
||||
let workspace = if let Some(config_file) = cli.config_file.as_ref() {
|
||||
Some(uv_workspace::Workspace::from_file(config_file)?)
|
||||
} else {
|
||||
uv_workspace::Workspace::find(env::current_dir()?)?
|
||||
};
|
||||
|
||||
// Resolve the global settings.
|
||||
let globals = GlobalSettings::resolve(cli.global_args, workspace.as_ref());
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue