Skip to content
Merged
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
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions crates/jp_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ assert_matches = { workspace = true }
camino-tempfile = { workspace = true }
indoc = { workspace = true }
pretty_assertions = { workspace = true, features = ["std"] }
serial_test = { workspace = true }
test-log = { workspace = true }

[lints]
Expand Down
20 changes: 15 additions & 5 deletions crates/jp_cli/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,21 @@ impl From<crate::error::Error> for Error {
("available", available.join(", ")),
]
.into(),
MissingConfigFile(path) => [
("message", "Missing config file".into()),
("path", path.to_string()),
]
.into(),
MissingConfigFile { path, searched } => {
let mut meta = vec![
("message", "Missing config file".into()),
("path", path.to_string()),
];
if !searched.is_empty() {
let dirs = searched
.iter()
.map(|p| format!(" - {p}"))
.collect::<Vec<_>>()
.join("\n");
meta.push(("searched", format!("Searched in:\n{dirs}")));
}
meta
}
};

Self::from(metadata)
Expand Down
7 changes: 5 additions & 2 deletions crates/jp_cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ pub(crate) enum Error {
KeyValue(#[from] jp_config::assignment::KvAssignmentError),

/// Missing config file.
#[error("Config file not found: {0}")]
MissingConfigFile(Utf8PathBuf),
#[error("Config file not found: {path}")]
MissingConfigFile {
path: Utf8PathBuf,
searched: Vec<Utf8PathBuf>,
},

#[error("CLI Config error: {0}")]
CliConfig(String),
Expand Down
110 changes: 68 additions & 42 deletions crates/jp_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,8 @@ fn load_cli_cfg_args(
overrides: &[KeyValueOrPath],
workspace: Option<&Workspace>,
) -> Result<PartialAppConfig> {
let home = std::env::home_dir().and_then(|p| Utf8PathBuf::from_path_buf(p).ok());

for field in overrides {
match field {
KeyValueOrPath::Path(path) if path.exists() => {
Expand All @@ -493,43 +495,76 @@ fn load_cli_cfg_args(
}
}
KeyValueOrPath::Path(path) => {
// Get the list of `config_load_paths`
// Build search roots in precedence order (lowest first).
//
// We do this on every iteration of `overrides`, to allow
// additional load paths to be added using `--cfg`.
let config_load_paths = workspace.iter().flat_map(|w| {
partial.config_load_paths.iter().flatten().filter_map(|p| {
Utf8PathBuf::try_from(p.to_path(w.root()))
.inspect_err(|e| {
tracing::error!(
path = p.to_string(),
error = e.to_string(),
"Not a valid UTF-8 path"
);
})
.ok()
})
});

let mut found = false;
for load_path in config_load_paths {
debug!(
path = path.as_str(),
load_path = load_path.as_str(),
"Trying to load partial from config load path"
);
// 1. User-global: $XDG_CONFIG_HOME/jp/config/
// 2. Workspace: <workspace_root>/
// 3. User-workspace: $XDG_DATA_HOME/jp/workspace/<id>/config/
let mut roots: Vec<Utf8PathBuf> = Vec::new();

if let Some(global_dir) = user_global_config_path(home.as_deref()) {
roots.push(global_dir.join("config"));
}
if let Some(w) = workspace {
roots.push(w.root().to_owned());
}
if let Some(user_ws_dir) = workspace.and_then(Workspace::user_storage_path) {
roots.push(user_ws_dir.join("config"));
}

let mut matches: Vec<PartialAppConfig> = Vec::new();
let mut searched: Vec<Utf8PathBuf> = Vec::new();

// Search each root independently. Within a single root, the
// first `config_load_paths` entry that produces a match wins.
// Across roots, all matches are collected for merging.
for root in &roots {
let load_paths: Vec<Utf8PathBuf> = partial
.config_load_paths
.iter()
.flatten()
.filter_map(|p| {
Utf8PathBuf::try_from(p.to_path(root))
.inspect_err(|e| {
tracing::error!(
path = p.to_string(),
error = e.to_string(),
"Not a valid UTF-8 path"
);
})
.ok()
})
.collect();

for load_path in &load_paths {
searched.push(load_path.clone());

if let Some(path) = find_file_in_load_path(path, &load_path) {
if let Some(p) = load_partial_at_path(path)? {
partial = load_partial(partial, p)?;
debug!(
path = path.as_str(),
load_path = load_path.as_str(),
root = root.as_str(),
"Trying to load partial from config load path"
);

if let Some(file) = find_file_in_load_path(path, load_path) {
if let Some(p) = load_partial_at_path(file)? {
matches.push(p);
}

break; // first match within this root
}
found = true;
break;
}
}

if !found {
return Err(Error::MissingConfigFile(path.clone()));
if matches.is_empty() {
return Err(Error::MissingConfigFile {
path: path.clone(),
searched,
});
}

for p in matches {
partial = load_partial(partial, p)?;
}
}
KeyValueOrPath::KeyValue(kv) => partial
Expand Down Expand Up @@ -846,14 +881,5 @@ fn run_dhat() -> dhat::Profiler {
}

#[cfg(test)]
mod tests {
use clap::CommandFactory;
use test_log::test;

use super::*;

#[test]
fn test_cli() {
Cli::command().debug_assert();
}
}
#[path = "lib_tests.rs"]
mod lib_tests;
Loading
Loading