use std::env;
use std::fs;
use std::path::PathBuf;
pub async fn run_config(debug: bool) -> Result<(), String> {
let current_dir: PathBuf = env::current_dir().map_err(|e| format!("Failed to get current directory: {}", e))?;
let xbp_json_path_dotfolder: PathBuf = current_dir.join(".xbp/xbp.json");
let xbp_json_path_root: PathBuf = current_dir.join("xbp.json");
if debug {
println!("[DEBUG] Current dir: {}", current_dir.display());
println!("[DEBUG] Checking for: {}", xbp_json_path_dotfolder.display());
println!("[DEBUG] Checking for: {}", xbp_json_path_root.display());
}
let (found_path, found_location) = if xbp_json_path_dotfolder.exists() {
(Some(xbp_json_path_dotfolder), Some(".xbp/xbp.json"))
} else if xbp_json_path_root.exists() {
(Some(xbp_json_path_root), Some("xbp.json"))
} else {
(None, None)
};
if let (Some(path), Some(location)) = (found_path, found_location) {
println!("Found xbp.json at: {}", path.display());
match fs::read_to_string(&path) {
Ok(contents) => {
if debug {
println!("[DEBUG] xbp.json contents: {}", contents);
}
if let Ok(json_data) = serde_json::from_str::<serde_json::Value>(&contents) {
for (key, value) in json_data.as_object().unwrap() {
let value_str: String = value.to_string().replace("\"", "");
println!("{:<15} | {}", key, value_str);
}
} else {
eprintln!(
"\x1b[91mFailed to parse {} contents as JSON.\x1b[0m",
location
);
}
}
Err(e) => {
eprintln!("\x1b[91mFailed to read {}: {}\x1b[0m", location, e);
}
}
} else {
println!("\x1b[91mNo .xbp/xbp.json or xbp.json found in the current directory.\x1b[0m");
}
Ok(())
}