Treat missing config file as empty

dblsaiko
Jan 4, 2024, 5:14 PM
2TWREKSRQN3QBIIJQ6Q4T3RVBTKSYA5N6W4YBRMRWD22WWS3DGQAC

Dependencies

  • [2] 6BEKMR3L Fixing a conflict
  • [3] FVQYZQFL Create dialoguer themes based on global config
  • [4] TFPETWTV Add config options for patch message templates
  • [5] L4JXJHWX pijul/*: reorganize imports and remove extern crate
  • [6] CCLLB7OI Upgrading to Sanakirja 0.15 + version bump
  • [7] A3RM526Y Integrating identity malleability
  • [8] SXEYMYF7 Fixing the bad changes in history (unfortunately, by rebooting).
  • [9] I24UEJQL Various post-fire fixes
  • [10] ZSFJT4SF Allow remotes to have a different push and pull address
  • [11] IUGP6ZGB Add support for ~/.config/pijul even on macos
  • [12] EEBKW7VT Keys and identities

Change contents

  • replacement in pijul/src/config.rs at line 2
    [4.89898][4.1665:1685](),[4.1685][2.0:0](),[2.0][4.0:24](),[4.89898][4.0:24]()
    use std::io::Write;
    use std::path::PathBuf;
    [4.89898]
    [4.89898]
    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
    [4.89899][4.11448:11466]()
    use anyhow::bail;
    [4.89899]
    [3.0]
    use anyhow::anyhow;
  • replacement in pijul/src/config.rs at line 12
    [4.1021][4.89899:89940](),[4.89899][4.89899:89940]()
    #[derive(Debug, Serialize, Deserialize)]
    [4.1021]
    [4.89940]
    #[derive(Debug, Default, Serialize, Deserialize)]
  • edit in pijul/src/config.rs at line 87
    [4.543]
    [4.90048]
    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` dir
    if 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")
    }
    [4.90062]
    [4.90964]
    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` dir
    let 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)))