start reworking config loading (for transition) WIP
This commit is contained in:
+18
-3
@@ -17,7 +17,7 @@ Main program for `pacdef`. All internal logic happens in [`pacdef_core`].
|
|||||||
|
|
||||||
use std::process::{ExitCode, Termination};
|
use std::process::{ExitCode, Termination};
|
||||||
|
|
||||||
use anyhow::{Context, Result};
|
use anyhow::{bail, Context, Result};
|
||||||
|
|
||||||
use pacdef_core::{get_args, get_config_path, get_group_dir, Config, Group, Pacdef};
|
use pacdef_core::{get_args, get_config_path, get_group_dir, Config, Group, Pacdef};
|
||||||
|
|
||||||
@@ -45,8 +45,7 @@ fn main_inner() -> Result<()> {
|
|||||||
let args = get_args();
|
let args = get_args();
|
||||||
|
|
||||||
let config_file = get_config_path().context("getting config file")?;
|
let config_file = get_config_path().context("getting config file")?;
|
||||||
let config = Config::load(&config_file)
|
let config = load_config(config_file)?;
|
||||||
.with_context(|| format!("loading config file {}", config_file.to_string_lossy()))?;
|
|
||||||
|
|
||||||
let group_dir = get_group_dir().context("resolving group dir")?;
|
let group_dir = get_group_dir().context("resolving group dir")?;
|
||||||
let groups = Group::load(&group_dir, config.warn_not_symlinks)
|
let groups = Group::load(&group_dir, config.warn_not_symlinks)
|
||||||
@@ -55,3 +54,19 @@ fn main_inner() -> Result<()> {
|
|||||||
let pacdef = Pacdef::new(args, config, groups);
|
let pacdef = Pacdef::new(args, config, groups);
|
||||||
pacdef.run_action_from_arg().context("running action")
|
pacdef.run_action_from_arg().context("running action")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn load_config(config_file: std::path::PathBuf) -> Result<Config> {
|
||||||
|
let config = match Config::load(&config_file)
|
||||||
|
.with_context(|| format!("loading config file {}", config_file.to_string_lossy()))
|
||||||
|
{
|
||||||
|
Ok(config) => config,
|
||||||
|
Err(e) => {
|
||||||
|
let e = e.root_cause().downcast_ref::<pacdef_core::Error>();
|
||||||
|
match e {
|
||||||
|
Some(pacdef_core::Error::ConfigFileNotFound) => Config::default(),
|
||||||
|
_ => bail!("huh"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
Ok(config)
|
||||||
|
}
|
||||||
|
|||||||
@@ -29,11 +29,7 @@ impl Config {
|
|||||||
Ok(content) => content,
|
Ok(content) => content,
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
if e.kind() == ErrorKind::NotFound {
|
if e.kind() == ErrorKind::NotFound {
|
||||||
println!(
|
bail!(crate::Error::ConfigFileNotFound)
|
||||||
"creating default config under {}",
|
|
||||||
config_file.to_string_lossy()
|
|
||||||
);
|
|
||||||
return Self::use_default_and_save_to(config_file);
|
|
||||||
}
|
}
|
||||||
bail!("unexpected error occured: {e:?}");
|
bail!("unexpected error occured: {e:?}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,12 +7,15 @@ use std::fmt::Display;
|
|||||||
pub enum Error {
|
pub enum Error {
|
||||||
/// Package search yields no results.
|
/// Package search yields no results.
|
||||||
NoPackagesFound,
|
NoPackagesFound,
|
||||||
|
/// Config file not found.
|
||||||
|
ConfigFileNotFound,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Display for Error {
|
impl Display for Error {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
Self::NoPackagesFound => f.write_str("no packages matching query"),
|
Self::NoPackagesFound => f.write_str("no packages matching query"),
|
||||||
|
Self::ConfigFileNotFound => f.write_str("config file not found"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user