-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add base for settings, and relocate venv-name default to it.
- Loading branch information
Showing
5 changed files
with
73 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} | ||
} |