Skip to content

Commit

Permalink
Support --config-file support
Browse files Browse the repository at this point in the history
  • Loading branch information
charliermarsh committed Apr 16, 2024
1 parent 4999c00 commit 84e48b3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
26 changes: 24 additions & 2 deletions crates/uv-workspace/src/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,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,
Expand All @@ -42,10 +42,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) {
Expand Down Expand Up @@ -81,6 +89,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)]
Expand Down
4 changes: 4 additions & 0 deletions crates/uv/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
pub(crate) config_file: Option<PathBuf>,
}

#[derive(Parser, Debug, Clone)]
Expand Down
5 changes: 5 additions & 0 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ async fn run() -> Result<ExitStatus> {
};

// Load the workspace settings.
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()?)?
};
let workspace = uv_workspace::Workspace::find(env::current_dir()?)?;

// Resolve the global settings.
Expand Down

0 comments on commit 84e48b3

Please sign in to comment.