Treat missing config file as empty
Dependencies
- [2]
6BEKMR3LFixing a conflict - [3]
FVQYZQFLCreate dialoguer themes based on global config - [4]
TFPETWTVAdd config options for patch message templates - [5]
L4JXJHWXpijul/*: reorganize imports and remove extern crate - [6]
CCLLB7OIUpgrading to Sanakirja 0.15 + version bump - [7]
A3RM526YIntegrating identity malleability - [8]
SXEYMYF7Fixing the bad changes in history (unfortunately, by rebooting). - [9]
I24UEJQLVarious post-fire fixes - [10]
ZSFJT4SFAllow remotes to have a different push and pull address - [11]
IUGP6ZGBAdd support for ~/.config/pijul even on macos - [12]
EEBKW7VTKeys and identities
Change contents
- replacement in pijul/src/config.rs at line 2
use std::io::Write;use std::path::PathBuf;use std::fs::File;use std::io;use std::io::{Read, Write};use std::path::{Path, PathBuf}; - replacement in pijul/src/config.rs at line 7
use anyhow::bail;use anyhow::anyhow; - replacement in pijul/src/config.rs at line 12
#[derive(Debug, Serialize, Deserialize)]#[derive(Debug, Default, Serialize, Deserialize)] - edit in pijul/src/config.rs at line 87
fn try_load_file(path: impl Into<PathBuf> + AsRef<Path>) -> Option<(io::Result<File>, PathBuf)> {let pp = path.as_ref();match File::open(pp) {Ok(v) => Some((Ok(v), path.into())),Err(e) if e.kind() == io::ErrorKind::NotFound => None,Err(e) => Some((Err(e), path.into())),}} - replacement in pijul/src/config.rs at line 97[4.90062]→[4.12751:12811](∅→∅),[4.12811]→[4.544:597](∅→∅),[4.90115]→[4.544:597](∅→∅),[4.597]→[4.90206:90243](∅→∅),[4.90206]→[4.90206:90243](∅→∅),[4.90243]→[4.12812:12925](∅→∅),[4.12925]→[4.60:357](∅→∅),[4.60]→[4.60:357](∅→∅),[4.357]→[4.12926:13018](∅→∅),[4.13018]→[4.401:594](∅→∅),[4.401]→[4.401:594](∅→∅),[4.594]→[4.90370:90437](∅→∅),[4.90370]→[4.90370:90437](∅→∅),[4.90437]→[4.598:651](∅→∅),[4.651]→[4.13019:13111](∅→∅),[4.639]→[4.90532:90561](∅→∅),[4.13111]→[4.90532:90561](∅→∅),[4.90532]→[4.90532:90561](∅→∅),[4.90561]→[4.640:678](∅→∅),[4.678]→[4.90607:90629](∅→∅),[4.90607]→[4.90607:90629](∅→∅),[4.90629]→[4.679:700](∅→∅),[4.700]→[4.90662:90697](∅→∅),[4.90662]→[4.90662:90697](∅→∅),[4.90697]→[4.11467:11517](∅→∅),[4.11517]→[4.13112:13336](∅→∅),[4.13336]→[4.11539:11646](∅→∅),[4.11539]→[4.11539:11646](∅→∅),[4.794]→[4.90881:90898](∅→∅),[4.11646]→[4.90881:90898](∅→∅),[4.90881]→[4.90881:90898](∅→∅),[4.90898]→[4.11647:11702](∅→∅),[4.844]→[4.90954:90964](∅→∅),[4.11702]→[4.90954:90964](∅→∅),[4.90954]→[4.90954:90964](∅→∅)
pub fn load() -> Result<(Global, u64), anyhow::Error> {if let Some(mut dir) = global_config_dir() {dir.push("config.toml");let (s, meta) = std::fs::read(&dir).and_then(|x| Ok((x, std::fs::metadata(&dir)?))).or_else(|e| {// Read from `$HOME/.config/pijul` dirif let Some(mut dir) = dirs_next::home_dir() {dir.push(".config");dir.push(CONFIG_DIR);dir.push("config.toml");std::fs::read(&dir).and_then(|x| Ok((x, std::fs::metadata(&dir)?)))} else {Err(e.into())}}).or_else(|e| {// Read from `$HOME/.pijulconfig`if let Some(mut dir) = dirs_next::home_dir() {dir.push(GLOBAL_CONFIG_DIR);std::fs::read(&dir).and_then(|x| Ok((x, std::fs::metadata(&dir)?)))} else {Err(e.into())}})?;debug!("s = {:?}", s);if let Ok(t) = toml::from_slice(&s) {let ts = meta.modified()?.duration_since(std::time::SystemTime::UNIX_EPOCH).unwrap().as_secs();Ok((t, ts))} else {bail!("Could not read configuration file at {:?}", dir)}} else {bail!("Global configuration file missing")}pub fn load() -> Result<(Global, Option<u64>), anyhow::Error> {let res = None.or_else(|| {let mut path = global_config_dir()?;path.push("config.toml");try_load_file(path)}).or_else(|| {// Read from `$HOME/.config/pijul` dirlet mut path = dirs_next::home_dir()?;path.push(".config");path.push(CONFIG_DIR);path.push("config.toml");try_load_file(path)}).or_else(|| {// Read from `$HOME/.pijulconfig`let mut path = dirs_next::home_dir()?;path.push(GLOBAL_CONFIG_DIR);try_load_file(path)});let Some((file, path)) = res else {return Ok((Global::default(), None));};let mut file = file.map_err(|e| {anyhow!("Could not open configuration file at {}", path.display()).context(e)})?;let mut buf = String::new();file.read_to_string(&mut buf).map_err(|e| {anyhow!("Could not read configuration file at {}", path.display()).context(e)})?;debug!("buf = {:?}", buf);let global: Global = toml::from_str(&buf).map_err(|e| {anyhow!("Could not parse configuration file at {}", path.display()).context(e)})?;let metadata = file.metadata()?;let file_age = metadata.modified()?.duration_since(std::time::SystemTime::UNIX_EPOCH)?.as_secs();Ok((global, Some(file_age)))