feat: add local profiles inspection before global
This commit is contained in:
1 parent
ef53278466
commit
8539e0308d
1 file changed
+13
-8
+13
-8
@@ -1,9 +1,19 @@
|
||||
//! Profile: `<config>/profiles/NAME.toml`. One profile = one identity (own HOME/auth), budget and gate.
|
||||
//! Profile: `./.hx/profiles/NAME.toml` (repo-local, checked first), else `<config>/profiles/NAME.toml`. One profile = one identity (own HOME/auth), budget and gate.
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use serde::Deserialize;
|
||||
use std::path::PathBuf;
|
||||
|
||||
/// `./.hx/profiles/NAME.toml` when it exists (repo-local wins), else `<config>/profiles/NAME.toml`.
|
||||
fn profile_path(name: &str) -> PathBuf {
|
||||
let file = format!("{name}.toml");
|
||||
let local = PathBuf::from(".hx/profiles").join(&file);
|
||||
if local.is_file() {
|
||||
return local;
|
||||
}
|
||||
crate::config_dir().join("profiles").join(file)
|
||||
}
|
||||
|
||||
/// [`Profile::add_allow`] on a file.
|
||||
fn add_allow_at(path: &std::path::Path, entry: &str) -> Result<bool> {
|
||||
crate::policy::check_entry(entry)?;
|
||||
@@ -172,9 +182,7 @@ impl Profile {
|
||||
if name.is_empty() || name.contains(['/', '.']) {
|
||||
bail!("bad profile name {name:?}");
|
||||
}
|
||||
let path = crate::config_dir()
|
||||
.join("profiles")
|
||||
.join(format!("{name}.toml"));
|
||||
let path = profile_path(name);
|
||||
let s = match std::fs::read_to_string(&path) {
|
||||
// only the implicit profile may be missing; a typo in any other name must still fail
|
||||
Err(e) if name == "default" && e.kind() == std::io::ErrorKind::NotFound => {
|
||||
@@ -192,10 +200,7 @@ impl Profile {
|
||||
if name.is_empty() || name.contains(['/', '.']) {
|
||||
bail!("bad profile name {name:?}");
|
||||
}
|
||||
let path = crate::config_dir()
|
||||
.join("profiles")
|
||||
.join(format!("{name}.toml"));
|
||||
add_allow_at(&path, entry)
|
||||
add_allow_at(&profile_path(name), entry)
|
||||
}
|
||||
|
||||
pub fn account<'a>(&'a self, profile: &'a str) -> &'a str {
|
||||
|
||||
Reference in new issue
Block a user