diff --git a/Cargo.lock b/Cargo.lock index b4b67c3..f6b2477 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -33,6 +33,17 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f26201604c87b1e01bd3d98f8d5d9a8fcbb815e8cedb41ffccbeb4bf593a35fe" +[[package]] +name = "ahash" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a824f2aa7e75a0c98c5a504fceb80649e9c35265d44525b5f94de4771a395cd" +dependencies = [ + "getrandom", + "once_cell", + "version_check", +] + [[package]] name = "ahash" version = "0.8.7" @@ -374,6 +385,12 @@ dependencies = [ "rustc-demangle", ] +[[package]] +name = "base64" +version = "0.13.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e1b586273c5702936fe7b7d6896644d8be71e6314cfe09d3167c95f712589e8" + [[package]] name = "base64" version = "0.21.7" @@ -430,6 +447,15 @@ version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0d8c1fef690941d3e7788d328517591fecc684c084084702d6ff1641e993699a" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "block-sys" version = "0.1.0-beta.1" @@ -535,12 +561,14 @@ dependencies = [ "battery", "chrono", "clap", + "config", "dbus", "freedesktop-desktop-entry", "iced", "log", "networkmanager", "serde", + "serde_derive", "serde_json", "simple_logger", "sqlite", @@ -732,6 +760,25 @@ dependencies = [ "crossbeam-utils", ] +[[package]] +name = "config" +version = "0.13.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23738e11972c7643e4ec947840fc463b6a571afcd3e735bdfce7d03c7a784aca" +dependencies = [ + "async-trait", + "json5", + "lazy_static", + "nom", + "pathdiff", + "ron", + "rust-ini", + "serde", + "serde_json", + "toml", + "yaml-rust", +] + [[package]] name = "core-foundation" version = "0.7.0" @@ -808,6 +855,15 @@ dependencies = [ "unicode-segmentation", ] +[[package]] +name = "cpufeatures" +version = "0.2.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "53fe5e26ff1b7aef8bca9c6080520cfb8d9333c7568e1829cef191a9723e5504" +dependencies = [ + "libc", +] + [[package]] name = "crc32fast" version = "1.3.2" @@ -848,6 +904,16 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a81dae078cea95a014a339291cec439d2f232ebe854a9d672b796c6afafa9b7" +[[package]] +name = "crypto-common" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bfb12502f3fc46cca1bb51ac28df9d618d813cdc3d2f25b9fe775a34af26bb3" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "d3d12" version = "0.6.0" @@ -885,6 +951,16 @@ dependencies = [ "powerfmt", ] +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "dirs" version = "3.0.2" @@ -929,6 +1005,12 @@ dependencies = [ "libloading 0.8.1", ] +[[package]] +name = "dlv-list" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0688c2a7f92e427f44895cd63841bff7b29f8d7a1648b9e7e07a4a365b2e1257" + [[package]] name = "downcast-rs" version = "1.2.0" @@ -1237,6 +1319,16 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "gethostname" version = "0.2.3" @@ -1426,6 +1518,9 @@ name = "hashbrown" version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +dependencies = [ + "ahash 0.7.7", +] [[package]] name = "hashbrown" @@ -1433,7 +1528,7 @@ version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" dependencies = [ - "ahash", + "ahash 0.8.7", "allocator-api2", ] @@ -1685,6 +1780,7 @@ checksum = "029d73f573d8e8d63e6d5020011d3255b28c3ba85d6cf870a07184ed23de9284" name = "index-git-repositories" version = "0.8.0" dependencies = [ + "anyhow", "log", "rust_search", "serde_json", @@ -1770,6 +1866,17 @@ dependencies = [ "wasm-bindgen", ] +[[package]] +name = "json5" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96b0db21af676c1ce64250b5f40f3ce2cf27e4e47cb91ed91eb6fe9350b430c1" +dependencies = [ + "pest", + "pest_derive", + "serde", +] + [[package]] name = "khronos-egl" version = "4.1.0" @@ -1874,6 +1981,12 @@ dependencies = [ "redox_syscall 0.4.1", ] +[[package]] +name = "linked-hash-map" +version = "0.5.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f" + [[package]] name = "linux-raw-sys" version = "0.3.8" @@ -2002,6 +2115,12 @@ dependencies = [ "objc", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -2147,6 +2266,16 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "ntapi" version = "0.4.1" @@ -2326,6 +2455,16 @@ dependencies = [ "libredox 0.0.2", ] +[[package]] +name = "ordered-multimap" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ccd746e37177e1711c20dd619a1620f34f5c8b569c53590a72dedd5344d8924a" +dependencies = [ + "dlv-list", + "hashbrown 0.12.3", +] + [[package]] name = "owned_ttf_parser" version = "0.20.0" @@ -2412,12 +2551,63 @@ dependencies = [ "windows-targets 0.48.5", ] +[[package]] +name = "pathdiff" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8835116a5c179084a830efb3adc117ab007512b535bc1a21c991d3b32a6b44dd" + [[package]] name = "percent-encoding" version = "2.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "e3148f5046208a5d56bcfc03053e3ca6334e51da8dfb19b6cdc8b306fae3283e" +[[package]] +name = "pest" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1f200d8d83c44a45b21764d1916299752ca035d15ecd46faca3e9a2a2bf6ad06" +dependencies = [ + "memchr", + "thiserror", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcd6ab1236bbdb3a49027e920e693192ebfe8913f6d60e294de57463a493cfde" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a31940305ffc96863a735bef7c7994a00b325a7138fdbc5bda0f1a0476d3275" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn 2.0.48", +] + +[[package]] +name = "pest_meta" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7ff62f5259e53b78d1af898941cdcdccfae7385cf7d793a6e55de5d05bb4b7d" +dependencies = [ + "once_cell", + "pest", + "sha2", +] + [[package]] name = "phf" version = "0.11.2" @@ -2766,6 +2956,17 @@ dependencies = [ "bytemuck", ] +[[package]] +name = "ron" +version = "0.7.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "88073939a61e5b7680558e6be56b419e208420c2adb92be54921fa6b72283f1a" +dependencies = [ + "base64 0.13.1", + "bitflags 1.3.2", + "serde", +] + [[package]] name = "roxmltree" version = "0.18.1" @@ -2775,6 +2976,16 @@ dependencies = [ "xmlparser", ] +[[package]] +name = "rust-ini" +version = "0.18.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6d5f2436026b4f6e79dc829837d467cc7e9a55ee40e750d716713540715a2df" +dependencies = [ + "cfg-if", + "ordered-multimap", +] + [[package]] name = "rust_search" version = "2.1.0" @@ -2902,18 +3113,18 @@ dependencies = [ [[package]] name = "serde" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "63261df402c67811e9ac6def069e4786148c4563f4b50fd4bf30aa370d626b02" +checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32" dependencies = [ "serde_derive", ] [[package]] name = "serde_derive" -version = "1.0.195" +version = "1.0.196" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" +checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67" dependencies = [ "proc-macro2", "quote", @@ -2940,6 +3151,17 @@ dependencies = [ "serde", ] +[[package]] +name = "sha2" +version = "0.10.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "simd-adler32" version = "0.3.7" @@ -3435,6 +3657,15 @@ version = "0.1.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1f3ccbac311fea05f86f61904b462b55fb3df8837a366dfc601a0161d0532f20" +[[package]] +name = "toml" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f4f7f0dd8d50a853a531c426359045b1998f04219d88799810762cd4ad314234" +dependencies = [ + "serde", +] + [[package]] name = "toml_datetime" version = "0.6.5" @@ -3503,6 +3734,12 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "ucd-trie" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ed646292ffc8188ef8ea4d1e0e0150fb15a5c2e12ad9b8fc191ae7a8a7f3c4b9" + [[package]] name = "unicode-bidi" version = "0.3.14" @@ -3585,7 +3822,7 @@ version = "0.35.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "14d09ddfb0d93bf84824c09336d32e42f80961a9d1680832eb24fdf249ce11e6" dependencies = [ - "base64", + "base64 0.21.7", "log", "pico-args", "usvg-parser", @@ -4434,6 +4671,15 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ec7a2a501ed189703dba8b08142f057e887dfc4b2cc4db2d343ac6376ba3e0b9" +[[package]] +name = "yaml-rust" +version = "0.4.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85" +dependencies = [ + "linked-hash-map", +] + [[package]] name = "yazi" version = "0.1.6" diff --git a/client/Cargo.toml b/client/Cargo.toml index 3262c97..2dbb093 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -44,3 +44,5 @@ sqlite = "0.32.0" # wifi networkmanager = "0.4.1" dbus = "0.9.7" +config = "0.13.4" +serde_derive = "1.0.196" diff --git a/client/src/main.rs b/client/src/main.rs index 4ae4901..66cef48 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -5,6 +5,7 @@ mod cli; mod component; mod model; mod plugin; +mod settings; pub fn main() -> iced::Result { let _args = crate::cli::CliArgs::parse(); diff --git a/client/src/plugin/brave/history.rs b/client/src/plugin/brave/history.rs index e5fc03b..6f99b4a 100644 --- a/client/src/plugin/brave/history.rs +++ b/client/src/plugin/brave/history.rs @@ -29,22 +29,17 @@ impl Plugin for HistoryPlugin { fn update_entries(&mut self) -> anyhow::Result<()> { self.entries.clear(); - let home_directory = - std::env::var("HOME").context("Could not read HOME environment variable")?; + let config_directory = crate::plugin::utils::config_directory()?; + let history_file_path = + format!("{config_directory}/BraveSoftware/Brave-Browser/Default/History"); - let cache_directory_path = std::path::Path::new(&home_directory).join(".cache/centerpiece"); - std::fs::create_dir_all(&cache_directory_path) - .context("Error while creating cache directory")?; - - let history_file_path = std::path::Path::new(&home_directory) - .join(".config/BraveSoftware/Brave-Browser/Default/History"); - let history_cache_file_path = cache_directory_path.join("brave-history.sqlite"); + let cache_directory = crate::plugin::utils::centerpiece_cache_directory()?; + let history_cache_file_path = format!("{cache_directory}/brave-history.sqlite"); std::fs::copy(history_file_path, &history_cache_file_path) .context("Error while creating cache directory")?; let connection = sqlite::open(history_cache_file_path).unwrap(); - let query = "SELECT title, url FROM urls ORDER BY visit_count DESC, last_visit_time DESC"; connection.execute(query).unwrap(); let url_rows = connection diff --git a/client/src/plugin/git_repositories.rs b/client/src/plugin/git_repositories.rs index 192a6bf..a2521fc 100644 --- a/client/src/plugin/git_repositories.rs +++ b/client/src/plugin/git_repositories.rs @@ -1,13 +1,14 @@ -use crate::plugin::utils::Plugin; +use crate::{plugin::utils::Plugin, settings::Settings}; use anyhow::Context; pub struct GitRepositoriesPlugin { entries: Vec, + settings: Settings, } impl Plugin for GitRepositoriesPlugin { fn id() -> &'static str { - "git-repositories" + "git_repositories" } fn priority() -> u32 { @@ -23,7 +24,15 @@ impl Plugin for GitRepositoriesPlugin { } fn new() -> Self { - Self { entries: vec![] } + let settings_result = Settings::new(); + if let Err(error) = settings_result { + log::error!(target: Self::id(), "{:?}", error); + panic!(); + } + Self { + entries: vec![], + settings: settings_result.unwrap(), + } } // This lint seems to be a false positive @@ -32,7 +41,7 @@ impl Plugin for GitRepositoriesPlugin { self.entries.clear(); let git_repository_paths: Vec = - crate::plugin::utils::read_index_file("git-repositories-index.json"); + crate::plugin::utils::read_index_file("git-repositories-index.json")?; let home = std::env::var("HOME").unwrap_or(String::from("")); @@ -59,32 +68,21 @@ impl Plugin for GitRepositoriesPlugin { entry: crate::model::Entry, plugin_channel_out: &mut iced::futures::channel::mpsc::Sender, ) -> anyhow::Result<()> { - std::process::Command::new("alacritty") - .arg("--working-directory") - .arg(&entry.id) - .spawn() - .context(format!( - "Failed to launch terminal while activating entry with id '{}'.", - entry.id - ))?; - - std::process::Command::new("sublime_text") - .arg("--new-window") - .arg(&entry.id) - .spawn() - .context(format!( - "Failed to launch editor while activating entry with id '{}'.", - entry.id - ))?; - - std::process::Command::new("sublime_merge") - .arg("--new-window") - .arg(&entry.id) - .spawn() - .context(format!( - "Failed to launch git ui while activating entry with id '{}'.", - entry.id - ))?; + for command in self.settings.plugin.git_repositories.commands.clone() { + let parsed_command: Vec = command + .into_iter() + .map(|command_part| { + if command_part == "$GIT_DIRECTORY" { + entry.id.clone() + } else { + command_part + } + }) + .collect(); + std::process::Command::new(&parsed_command[0]) + .args(&parsed_command[1..]) + .spawn()?; + } plugin_channel_out .try_send(crate::Message::Exit) diff --git a/client/src/plugin/utils.rs b/client/src/plugin/utils.rs index 196ee2a..9e7d7d4 100644 --- a/client/src/plugin/utils.rs +++ b/client/src/plugin/utils.rs @@ -167,33 +167,37 @@ pub fn search(entries: Vec, query: &String) -> Vec>() } -// TODO: this function should return a result and propagate errors -pub fn read_index_file(file_name: &str) -> T +pub fn config_directory() -> anyhow::Result { + let home_directory = std::env::var("HOME")?; + let config_in_home = format!("{home_directory}/.config"); + Ok(std::env::var("XDG_CONFIG_HOME").unwrap_or(config_in_home)) +} + +pub fn centerpiece_config_directory() -> anyhow::Result { + let config_directory = config_directory()?; + Ok(format!("{config_directory}/centerpiece")) +} + +pub fn cache_directory() -> anyhow::Result { + let home_directory = std::env::var("HOME")?; + let cache_in_home = format!("{home_directory}/.cache"); + Ok(std::env::var("XDG_CACHE_HOME").unwrap_or(cache_in_home)) +} + +pub fn centerpiece_cache_directory() -> anyhow::Result { + let cache_directory = cache_directory()?; + Ok(format!("{cache_directory}/centerpiece")) +} + +pub fn read_index_file(file_name: &str) -> anyhow::Result where T: serde::de::DeserializeOwned, { - let home_directory_result = std::env::var("HOME"); - if let Err(error) = home_directory_result { - log::error!( - error = log::as_error!(error); - "Could not read HOME environment variable", - ); - panic!(); - } - let home_directory = home_directory_result.unwrap(); + let cache_directory = centerpiece_cache_directory()?; + let index_file_path = format!("{cache_directory}/{file_name}"); - let index_file_path = std::path::Path::new(&home_directory) - .join(".cache/centerpiece") - .join(file_name); - let index_file_result = std::fs::File::open(index_file_path); - if let Err(error) = index_file_result { - log::error!( - error = log::as_error!(error); - "Error while opening index file", - ); - panic!(); - } - let index_file = index_file_result.unwrap(); + let index_file = + std::fs::File::open(index_file_path).context("Error while opening index file")?; let reader = std::io::BufReader::new(index_file); let git_repository_paths_result: Result = serde_json::from_reader(reader); @@ -204,5 +208,5 @@ where ); panic!(); } - git_repository_paths_result.unwrap() + Ok(git_repository_paths_result.unwrap()) } diff --git a/client/src/settings.rs b/client/src/settings.rs new file mode 100644 index 0000000..309c6f1 --- /dev/null +++ b/client/src/settings.rs @@ -0,0 +1,36 @@ +use config::{Config, ConfigError}; +use serde_derive::Deserialize; + +#[derive(Debug, Deserialize)] +#[allow(unused)] +pub struct GitRepositoriesSettings { + pub commands: Vec>, +} + +#[derive(Debug, Deserialize)] +#[allow(unused)] +pub struct PluginSettings { + pub git_repositories: GitRepositoriesSettings, +} + +#[derive(Debug, Deserialize)] +#[allow(unused)] +pub struct Settings { + pub plugin: PluginSettings, +} + +impl Settings { + pub fn new() -> Result { + let config_directory = + crate::plugin::utils::centerpiece_config_directory().map_err(|_| { + config::ConfigError::Message("Unable to find config directory.".to_string()) + })?; + let config_file = format!("{config_directory}/config"); + + Config::builder() + .add_source(config::File::new("config", config::FileFormat::Yaml)) + .add_source(config::File::new(&config_file, config::FileFormat::Yaml).required(false)) + .build()? + .try_deserialize() + } +} diff --git a/config.yml b/config.yml new file mode 100644 index 0000000..d090cc9 --- /dev/null +++ b/config.yml @@ -0,0 +1,6 @@ +plugin: + git_repositories: + commands: + - ["alacritty", "--command", "nvim", "$GIT_DIRECTORY"] + - ["alacritty", "--command", "lazygit", "--path", "$GIT_DIRECTORY"] + - ["alacritty", "--working-directory", "$GIT_DIRECTORY"] diff --git a/home-manager-module.nix b/home-manager-module.nix index bd09a8f..48ff43e 100644 --- a/home-manager-module.nix +++ b/home-manager-module.nix @@ -1,17 +1,29 @@ { index-git-repositories, centerpiece }: -{ - lib, - pkgs, - config, - ... -}: +{ lib, pkgs, config, ... }: let cfg = config.programs.centerpiece; git-index-name = "index-git-repositories"; -in -{ +in { options.programs.centerpiece = { enable = lib.mkEnableOption (lib.mdDoc "Centerpiece"); + + config.plugin.git_repositories = { + commands = lib.mkOption { + default = [ + [ "alacritty" "--command" "nvim" "$GIT_DIRECTORY" ] + [ "alacritty" "--working-directory" "$GIT_DIRECTORY" ] + ]; + type = lib.types.listOf lib.types.listOf lib.types.str; + description = lib.mdDoc + "The commands to launch when an entry is selected. Use the $GIT_DIRECTORY variable to pass in the selected directory."; + example = [ + [ "code" "--new-window" "$GIT_DIRECTORY" ] + [ "alacritty" "--command" "lazygit" "--path" "$GIT_DIRECTORY" ] + [ "alacritty" "--working-directory" "$GIT_DIRECTORY" ] + ]; + }; + }; + services.index-git-repositories = { enable = lib.mkEnableOption (lib.mdDoc "enable timer"); interval = lib.mkOption { @@ -31,6 +43,10 @@ in config = lib.mkMerge [ (lib.mkIf cfg.enable { home.packages = [ centerpiece ]; }) + (lib.mkIf cfg.config { + home.file.".config/centerpiece/config.yml" = builtins.toYAML cfg.config; + }) + (lib.mkIf cfg.services.index-git-repositories.enable { systemd.user = { services = { @@ -41,21 +57,18 @@ in }; Service = { - ExecStart = "${pkgs.writeShellScript "${git-index-name}-service-ExecStart" '' - exec ${lib.getExe index-git-repositories} - ''}"; + ExecStart = "${pkgs.writeShellScript + "${git-index-name}-service-ExecStart" '' + exec ${lib.getExe index-git-repositories} + ''}"; Type = "oneshot"; }; }; }; timers = { index-git-repositories-timer = { - Unit = { - Description = "Activate the git repository indexer"; - }; - Install = { - WantedBy = [ "timers.target" ]; - }; + Unit = { Description = "Activate the git repository indexer"; }; + Install = { WantedBy = [ "timers.target" ]; }; Timer = { OnUnitActiveSec = cfg.services.index-git-repositories.interval; OnBootSec = "0min"; diff --git a/services/index-git-repositories/Cargo.toml b/services/index-git-repositories/Cargo.toml index 328827b..f119da6 100644 --- a/services/index-git-repositories/Cargo.toml +++ b/services/index-git-repositories/Cargo.toml @@ -9,3 +9,4 @@ simple_logger = { version = "4.3.3", features = ["colors", "threads", "timestamp serde_json = "1.0.108" rust_search = "2.0.0" +anyhow = "1.0.79" diff --git a/services/index-git-repositories/src/main.rs b/services/index-git-repositories/src/main.rs index 64878eb..bb9d2c5 100644 --- a/services/index-git-repositories/src/main.rs +++ b/services/index-git-repositories/src/main.rs @@ -37,19 +37,25 @@ fn main() { write_index_file(git_repository_paths); } +pub fn cache_directory() -> anyhow::Result { + let home_directory = std::env::var("HOME")?; + let cache_in_home = format!("{home_directory}/.cache"); + let cache_directory = std::env::var("XDG_CACHE_HOME").unwrap_or(cache_in_home); + Ok(format!("{cache_directory}/centerpiece")) +} + fn write_index_file(git_repository_paths: Vec<&str>) { - let home_directory_result = std::env::var("HOME"); - if let Err(error) = home_directory_result { + let cache_directory_result = cache_directory(); + if let Err(error) = cache_directory_result { log::error!( - error = log::as_error!(error); - "Could read HOME environment variable", + error = log::error!("{:?}", error); + "Could not determine cache directory.", ); panic!(); } - let home_directory = home_directory_result.unwrap(); + let centerpice_cache_directory = cache_directory_result.unwrap(); - let cache_directory_path = std::path::Path::new(&home_directory).join(".cache/centerpiece"); - if let Err(error) = std::fs::create_dir_all(&cache_directory_path) { + if let Err(error) = std::fs::create_dir_all(¢erpice_cache_directory) { log::error!( error = log::as_error!(error); "Error while creating cache directory", @@ -57,7 +63,8 @@ fn write_index_file(git_repository_paths: Vec<&str>) { panic!(); } - let index_file_path = cache_directory_path.join("git-repositories-index.json"); + let index_file_path = + std::path::Path::new(¢erpice_cache_directory).join("git-repositories-index.json"); let index_file_result = std::fs::File::create(index_file_path); if let Err(error) = index_file_result {