Per collection/folder auth

This commit is contained in:
Torbjörn Molin committed 2026-10-19 12:38:38 +02:00
1 parent d5e7b484fc
commit 47ee07d9ff
16 files changed
+1569 -58

No files matched your search

+34 -4
View File
@@ -1,9 +1,11 @@
//! A collection is a folder of `.http` files. Scanning builds the sidebar
//! tree (folder → file → requests) and parses every file.
//! tree (folder → file → requests), parses every file and reads the folder
//! settings ([`crate::folder`]).
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use crate::folder::FolderSettings;
use crate::httpfile::HttpFile;
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -39,6 +41,10 @@ pub struct Collection {
pub tree: Node,
/// Parsed files by absolute path.
pub files: BTreeMap<PathBuf, HttpFile>,
/// Folder settings by folder path (only folders that have a file).
pub folders: BTreeMap<PathBuf, FolderSettings>,
/// Folder settings files that could not be read.
pub folder_errors: Vec<String>,
}
pub fn is_http_file(path: &Path) -> bool {
@@ -52,7 +58,8 @@ fn skip_dir(name: &str) -> bool {
/// Scans `root` recursively. Folders without `.http` files are left out.
pub fn scan(root: &Path) -> std::io::Result<Collection> {
let mut files = BTreeMap::new();
let tree = scan_dir(root, &mut files)?.unwrap_or_else(|| Node::Folder {
let mut folders = Folders::default();
let tree = scan_dir(root, &mut files, &mut folders)?.unwrap_or_else(|| Node::Folder {
name: dir_name(root),
path: root.to_path_buf(),
children: Vec::new(),
@@ -61,16 +68,35 @@ pub fn scan(root: &Path) -> std::io::Result<Collection> {
root: root.to_path_buf(),
tree,
files,
folders: folders.settings,
folder_errors: folders.errors,
})
}
#[derive(Default)]
struct Folders {
settings: BTreeMap<PathBuf, FolderSettings>,
errors: Vec<String>,
}
fn dir_name(p: &Path) -> String {
p.file_name()
.map(|n| n.to_string_lossy().into_owned())
.unwrap_or_else(|| p.display().to_string())
}
fn scan_dir(dir: &Path, files: &mut BTreeMap<PathBuf, HttpFile>) -> std::io::Result<Option<Node>> {
fn scan_dir(
dir: &Path,
files: &mut BTreeMap<PathBuf, HttpFile>,
settings: &mut Folders,
) -> std::io::Result<Option<Node>> {
match FolderSettings::load(dir) {
Ok(Some(s)) => {
settings.settings.insert(dir.to_path_buf(), s);
}
Ok(None) => {}
Err(e) => settings.errors.push(e.to_string()),
}
let mut entries: Vec<_> = std::fs::read_dir(dir)?.filter_map(Result::ok).collect();
entries.sort_by_key(|e| e.file_name().to_string_lossy().to_lowercase());
let mut folders = Vec::new();
@@ -83,7 +109,7 @@ fn scan_dir(dir: &Path, files: &mut BTreeMap<PathBuf, HttpFile>) -> std::io::Res
if skip_dir(&name) {
continue;
}
if let Some(n) = scan_dir(&path, files)? {
if let Some(n) = scan_dir(&path, files, settings)? {
folders.push(n);
}
} else if is_http_file(&path) {
@@ -122,6 +148,8 @@ mod tests {
std::fs::create_dir(root.join(".git")).unwrap();
std::fs::write(root.join(".git/x.http"), "GET /").unwrap();
std::fs::create_dir(root.join("empty")).unwrap();
std::fs::create_dir_all(root.join("orders/.reqlite")).unwrap();
std::fs::write(root.join("orders/.reqlite/folder.json"), r#"{"auth": "Bearer x"}"#).unwrap();
let c = scan(root).unwrap();
let Node::Folder { children, .. } = &c.tree else {
@@ -135,5 +163,7 @@ mod tests {
assert_eq!(orders.len(), 1);
assert_eq!(c.files.len(), 2);
assert_eq!(c.files[&root.join("users.http")].request_count(), 2);
assert_eq!(c.folders.keys().collect::<Vec<_>>(), vec![&root.join("orders")]);
assert!(c.folder_errors.is_empty());
}
}