add config (WIP)
This commit is contained in:
@@ -8,6 +8,7 @@ use crate::backend::backend_trait::*;
|
||||
use crate::{impl_backend_constants, Group, Package};
|
||||
|
||||
pub(crate) struct Pacman {
|
||||
pub(crate) binary: String,
|
||||
pub(crate) packages: HashSet<Package>,
|
||||
}
|
||||
|
||||
@@ -69,6 +70,7 @@ fn get_db_handle() -> Result<Alpm> {
|
||||
impl Pacman {
|
||||
pub(crate) fn new() -> Self {
|
||||
Self {
|
||||
binary: BINARY.to_string(),
|
||||
packages: HashSet::new(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
use std::fs::{read_to_string, File};
|
||||
use std::io::{ErrorKind, Write};
|
||||
use std::path::PathBuf;
|
||||
|
||||
use anyhow::{bail, Context, Result};
|
||||
use serde_derive::{Deserialize, Serialize};
|
||||
|
||||
use crate::path::get_pacdef_base_dir;
|
||||
|
||||
const CONFIG_FILE_NAME: &str = "pacdef.yaml";
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Config {
|
||||
pub aur_helper: String,
|
||||
pub warn_not_symlinks: bool,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load() -> Result<Self> {
|
||||
let file = get_config_file()?;
|
||||
let from_file = read_to_string(&file);
|
||||
|
||||
if let Err(e) = from_file {
|
||||
if e.kind() == ErrorKind::NotFound {
|
||||
println!("creating default config under {file:?}");
|
||||
return Self::use_default_and_save_to(file);
|
||||
} else {
|
||||
bail!("unexpected error occured: {e:?}");
|
||||
};
|
||||
}
|
||||
|
||||
let content = from_file.unwrap();
|
||||
|
||||
serde_yaml::from_str(&content).context("parsing yaml config")
|
||||
}
|
||||
|
||||
fn use_default_and_save_to(file: PathBuf) -> Result<Self> {
|
||||
let result = Self::default();
|
||||
|
||||
let content = serde_yaml::to_string(&result).context("converting Config to yaml")?;
|
||||
let mut output = File::create(file).context("creating default config file")?;
|
||||
write!(output, "{content}").context("writing default config")?;
|
||||
|
||||
Ok(result)
|
||||
}
|
||||
}
|
||||
|
||||
fn get_config_file() -> Result<PathBuf> {
|
||||
let mut file = get_pacdef_base_dir().context("getting pacdef base dir for config file")?;
|
||||
file.push(CONFIG_FILE_NAME);
|
||||
Ok(file)
|
||||
}
|
||||
|
||||
impl Default for Config {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
aur_helper: "paru".into(),
|
||||
warn_not_symlinks: true,
|
||||
}
|
||||
}
|
||||
}
|
||||
+8
-2
@@ -14,18 +14,24 @@ use crate::env::get_single_var;
|
||||
use crate::path::get_pacdef_group_dir;
|
||||
use crate::search;
|
||||
use crate::ui::get_user_confirmation;
|
||||
use crate::Config;
|
||||
use crate::Group;
|
||||
|
||||
pub struct Pacdef {
|
||||
args: ArgMatches,
|
||||
config: Config,
|
||||
groups: HashSet<Group>,
|
||||
}
|
||||
|
||||
// TODO review
|
||||
impl Pacdef {
|
||||
#[must_use]
|
||||
pub fn new(args: ArgMatches, groups: HashSet<Group>) -> Self {
|
||||
Self { args, groups }
|
||||
pub fn new(args: ArgMatches, config: Config, groups: HashSet<Group>) -> Self {
|
||||
Self {
|
||||
args,
|
||||
config,
|
||||
groups,
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::unit_arg)]
|
||||
|
||||
@@ -8,6 +8,8 @@ use anyhow::{Context, Result};
|
||||
|
||||
use super::Section;
|
||||
|
||||
use crate::Config;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Group {
|
||||
pub(crate) name: String,
|
||||
@@ -15,7 +17,7 @@ pub struct Group {
|
||||
}
|
||||
|
||||
impl Group {
|
||||
pub fn load_from_dir() -> Result<HashSet<Self>> {
|
||||
pub fn load(config: &Config) -> Result<HashSet<Self>> {
|
||||
let mut result = HashSet::new();
|
||||
|
||||
let path = crate::path::get_pacdef_group_dir().context("getting pacdef group dir")?;
|
||||
@@ -23,8 +25,13 @@ impl Group {
|
||||
let file = entry.context("getting group file")?;
|
||||
let path = file.path();
|
||||
|
||||
if config.warn_not_symlinks && !path.is_symlink() {
|
||||
println!("WARNING: group file {path:?} is not a symlink");
|
||||
}
|
||||
|
||||
let group =
|
||||
Group::try_from(&path).with_context(|| format!("reading group file {path:?}"))?;
|
||||
|
||||
result.insert(group);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ mod action;
|
||||
pub mod args;
|
||||
mod backend;
|
||||
mod cmd;
|
||||
mod config;
|
||||
mod core;
|
||||
mod env;
|
||||
mod grouping;
|
||||
@@ -9,6 +10,7 @@ mod path;
|
||||
mod search;
|
||||
mod ui;
|
||||
|
||||
pub use crate::config::Config;
|
||||
pub use crate::core::Pacdef;
|
||||
pub use crate::grouping::Group;
|
||||
pub(crate) use crate::grouping::Package;
|
||||
|
||||
+4
-3
@@ -1,10 +1,11 @@
|
||||
use anyhow::{Context, Result};
|
||||
|
||||
use pacdef::{args, Group, Pacdef};
|
||||
use pacdef::{args, Config, Group, Pacdef};
|
||||
|
||||
fn main() -> Result<()> {
|
||||
let args = args::get();
|
||||
let groups = Group::load_from_dir()?;
|
||||
let pacdef = Pacdef::new(args, groups);
|
||||
let config = Config::load().context("loading config file")?;
|
||||
let groups = Group::load(&config).context("loading groups")?;
|
||||
let pacdef = Pacdef::new(args, config, groups);
|
||||
pacdef.run_action_from_arg().context("running action")
|
||||
}
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@ pub(crate) fn get_pacdef_group_dir() -> Result<PathBuf> {
|
||||
Ok(result)
|
||||
}
|
||||
|
||||
fn get_pacdef_base_dir() -> Result<PathBuf> {
|
||||
pub(crate) fn get_pacdef_base_dir() -> Result<PathBuf> {
|
||||
let mut dir = get_xdg_config_home().context("getting XDG_CONFIG_HOME")?;
|
||||
dir.push("pacdef");
|
||||
Ok(dir)
|
||||
|
||||
Reference in New Issue
Block a user