Skip to content

Commit

Permalink
🔧 use XDG_CONFIG_HOME as a default to place cache files
Browse files Browse the repository at this point in the history
  • Loading branch information
friedow committed Feb 13, 2024
1 parent a6fa3e4 commit 8d9a41b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions client/src/plugin/brave/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,12 @@ 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::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")?;
Expand Down
2 changes: 1 addition & 1 deletion client/src/plugin/git_repositories.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Plugin for GitRepositoriesPlugin {
self.entries.clear();

let git_repository_paths: Vec<String> =
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(""));

Expand Down
46 changes: 21 additions & 25 deletions client/src/plugin/utils.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use anyhow::Context;
use anyhow::{anyhow, Context};
use iced::futures::StreamExt;

pub fn spawn<PluginType: Plugin + std::marker::Send + 'static>(
Expand Down Expand Up @@ -167,33 +167,29 @@ pub fn search(entries: Vec<crate::model::Entry>, query: &String) -> Vec<crate::m
.collect::<Vec<crate::model::Entry>>()
}

// TODO: this function should return a result and propagate errors
pub fn read_index_file<T>(file_name: &str) -> T
pub fn config_directory() -> anyhow::Result<String> {
let home_directory = std::env::var("HOME")?;
let config_in_home = format!("{home_directory}/.config");
let config_directory = std::env::var("XDG_CONFIG_HOME").unwrap_or(config_in_home);
Ok(format!("{config_directory}/centerpiece"))
}

pub fn cache_directory() -> anyhow::Result<String> {
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"))
}

pub fn read_index_file<T>(file_name: &str) -> anyhow::Result<T>
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 = 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<T, _> = serde_json::from_reader(reader);
Expand All @@ -204,5 +200,5 @@ where
);
panic!();
}
git_repository_paths_result.unwrap()
Ok(git_repository_paths_result.unwrap())
}
13 changes: 4 additions & 9 deletions client/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,14 @@ pub struct Settings {

impl Settings {
pub fn new() -> Result<Self, ConfigError> {
let home_directory = std::env::var("HOME").map_err(|_| {
ConfigError::Message("Could read HOME environment variable".to_string())
let config_directory = crate::plugin::utils::config_directory().map_err(|_| {
config::ConfigError::Message("Unable to find config directory.".to_string())
})?;
let config_in_home = format!("{home_directory}/.config");
let config_directory = std::env::var("XDG_CONFIG_HOME").unwrap_or(config_in_home);
let centerpice_config_file_name = format!("{config_directory}/centerpiece/config");
let config_file = format!("{config_directory}/config");

Config::builder()
.add_source(config::File::new("config", config::FileFormat::Yaml))
.add_source(
config::File::new(&centerpice_config_file_name, config::FileFormat::Yaml)
.required(false),
)
.add_source(config::File::new(&config_file, config::FileFormat::Yaml).required(false))
.build()?
.try_deserialize()
}
Expand Down
1 change: 1 addition & 0 deletions services/index-git-repositories/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
23 changes: 15 additions & 8 deletions services/index-git-repositories/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,34 @@ fn main() {
write_index_file(git_repository_paths);
}

pub fn cache_directory() -> anyhow::Result<String> {
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(&centerpice_cache_directory) {
log::error!(
error = log::as_error!(error);
"Error while creating cache directory",
);
panic!();
}

let index_file_path = cache_directory_path.join("git-repositories-index.json");
let index_file_path =
std::path::Path::new(&centerpice_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 {
Expand Down

0 comments on commit 8d9a41b

Please sign in to comment.