Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add base for settings, and relocate venv-name default to it. #1509

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ directories = { version = "5.0.1" }
dirs = { version = "5.0.1" }
dunce = { version = "1.0.4" }
either = { version = "1.9.0" }
etcetera = { version = "0.8.0" }
flate2 = { version = "1.0.28", default-features = false }
fs-err = { version = "2.11.0" }
fs2 = { version = "0.4.3" }
Expand Down Expand Up @@ -97,6 +98,7 @@ tokio-stream = { version = "0.1.14" }
tokio-tar = { version = "0.3.1" }
tokio-util = { version = "0.7.10", features = ["compat"] }
toml = { version = "0.8.8" }
toml_edit = { version = "0.22" }
tracing = { version = "0.1.40" }
tracing-durations-export = { version = "0.2.0", features = ["plot"] }
tracing-indicatif = { version = "0.3.6" }
Expand Down
2 changes: 2 additions & 0 deletions crates/uv/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ clap = { workspace = true, features = ["derive"] }
console = { workspace = true }
ctrlc = { workspace = true }
dunce = { workspace = true }
etcetera = { workspace = true }
flate2 = { workspace = true, default-features = false }
fs-err = { workspace = true, features = ["tokio"] }
futures = { workspace = true }
Expand All @@ -59,6 +60,7 @@ textwrap = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
toml = { workspace = true }
toml_edit = { workspace = true }
tracing = { workspace = true }
tracing-durations-export = { workspace = true, features = ["plot"], optional = true }
tracing-subscriber = { workspace = true }
Expand Down
11 changes: 7 additions & 4 deletions crates/uv/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ mod confirm;
mod logging;
mod printer;
mod requirements;
mod settings;

#[derive(Parser)]
#[command(author, version, about)]
Expand Down Expand Up @@ -647,9 +648,8 @@ struct VenvArgs {
#[clap(long)]
seed: bool,

/// The path to the virtual environment to create.
#[clap(default_value = ".venv")]
name: PathBuf,
/// The path to the virtual environment to create. Defaults to .venv.
name: Option<PathBuf>,

/// The URL of the Python Package Index.
#[clap(long, short, default_value = IndexUrl::Pypi.as_str(), env = "UV_INDEX_URL")]
Expand Down Expand Up @@ -1003,6 +1003,9 @@ async fn run() -> Result<ExitStatus> {
}) => commands::freeze(&cache, args.strict, printer),
Commands::Clean(args) => commands::clean(&cache, &args.package, printer),
Commands::Venv(args) => {
let mut settings = settings::Settings::read()?;
settings.set_venv_name(args.name);

args.compat_args.validate()?;

let index_locations = IndexLocations::from_args(
Expand All @@ -1013,7 +1016,7 @@ async fn run() -> Result<ExitStatus> {
args.no_index,
);
commands::venv(
&args.name,
&settings.venv_name,
args.python.as_deref(),
&index_locations,
if args.offline {
Expand Down
49 changes: 49 additions & 0 deletions crates/uv/src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
use etcetera::base_strategy::{BaseStrategy, Xdg};
use std::fs::File;
use std::io::{BufReader, Read};
use std::path::{Path, PathBuf};
use toml_edit::Document;

pub(crate) struct Settings {
pub(crate) venv_name: PathBuf,
}

impl Settings {
pub(crate) fn read() -> anyhow::Result<Self> {
let strategy = Xdg::new()?;
let config_dir = strategy.config_dir().join("uv");

let config_file = config_dir.with_extension("toml");

let content = read_file(&config_file);
let document = content.parse::<Document>().unwrap_or(Document::new());

let venv_name = PathBuf::from(
document
.get("venv-name")
.and_then(|v| v.as_str())
.unwrap_or(".venv")
.to_string(),
);

Ok(Self { venv_name })
}

pub(crate) fn set_venv_name(&mut self, maybe_name: Option<PathBuf>) {
if let Some(name) = maybe_name {
self.venv_name = name;
}
}
}

fn read_file(path: &Path) -> String {
if let Ok(file) = File::open(path) {
let mut reader = BufReader::new(file);

let mut contents = String::new();
reader.read_to_string(&mut contents).unwrap_or(0);
contents
} else {
String::new()
}
}