This commit is contained in:
Dr. Matthias Ratajczak
2022-12-02 17:23:50 +01:00
parent 2efa25e14e
commit de1a8b5f9f
4 changed files with 40 additions and 8 deletions
+3 -4
View File
@@ -1,12 +1,11 @@
use std::collections::HashSet; use std::collections::HashSet;
use std::path::PathBuf;
use std::process::exit; use std::process::exit;
use crate::action; use crate::action;
use crate::cmd::run_install_command; use crate::cmd::run_install_command;
use crate::db::{get_all_installed_packages, get_explicitly_installed_packages}; use crate::db::{get_all_installed_packages, get_explicitly_installed_packages};
use crate::group::{Group, GROUPS_DIR}; use crate::Group;
use crate::package::Package; use crate::Package;
use clap::ArgMatches; use clap::ArgMatches;
@@ -78,7 +77,7 @@ impl Pacdef {
.get_many::<String>("group") .get_many::<String>("group")
.unwrap() .unwrap()
.map(|file| { .map(|file| {
let mut buf = PathBuf::from(GROUPS_DIR); let mut buf = crate::path::get_pacdef_group_dir().unwrap();
buf.push(file); buf.push(file);
buf buf
}) })
+1 -4
View File
@@ -2,12 +2,9 @@ use std::collections::HashSet;
use std::fs::File; use std::fs::File;
use std::hash::Hash; use std::hash::Hash;
use std::io::{BufRead, BufReader}; use std::io::{BufRead, BufReader};
use std::path::PathBuf;
use crate::Package; use crate::Package;
pub const GROUPS_DIR: &str = "/home/ratajc72/.config/pacdef/groups";
#[derive(Debug)] #[derive(Debug)]
pub struct Group { pub struct Group {
pub name: String, pub name: String,
@@ -17,7 +14,7 @@ pub struct Group {
impl Group { impl Group {
pub fn load_from_dir() -> HashSet<Self> { pub fn load_from_dir() -> HashSet<Self> {
let mut result = HashSet::new(); let mut result = HashSet::new();
let path = PathBuf::from(GROUPS_DIR); let path = crate::path::get_pacdef_group_dir().unwrap();
for entry in path.read_dir().unwrap() { for entry in path.read_dir().unwrap() {
let file = entry.unwrap(); let file = entry.unwrap();
let name = file.file_name(); let name = file.file_name();
+1
View File
@@ -5,6 +5,7 @@ mod core;
pub mod db; pub mod db;
mod group; mod group;
mod package; mod package;
mod path;
mod ui; mod ui;
pub use crate::core::Pacdef; pub use crate::core::Pacdef;
+35
View File
@@ -0,0 +1,35 @@
use std::{env, path::PathBuf};
use anyhow::{Context, Result};
pub(crate) fn get_pacdef_group_dir() -> Result<PathBuf> {
let mut result = get_pacdef_base_dir().context("getting pacdef base dir")?;
result.push("groups");
Ok(result)
}
pub(crate) fn get_pacdef_config_file() -> Result<PathBuf> {
let mut result = get_pacdef_base_dir().context("getting pacdef base dir")?;
result.push("pacdef.conf");
Ok(result)
}
fn get_pacdef_base_dir() -> Result<PathBuf> {
let mut dir = get_xdg_config_home().context("getting XDG_CONFIG_HOME")?;
dir.push("pacdef");
Ok(dir)
}
fn get_xdg_config_home() -> Result<PathBuf> {
if let Ok(config) = env::var("XDG_CONFIG_HOME") {
Ok(config.into())
} else {
let mut config = get_home_dir().context("falling back to $HOME/.config")?;
config.push(".config");
Ok(config)
}
}
fn get_home_dir() -> Result<PathBuf> {
Ok(env::var("HOME").context("getting $HOME variable")?.into())
}