Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🔧 Configuration File #61

Merged
merged 7 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
258 changes: 252 additions & 6 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
15 changes: 5 additions & 10 deletions client/src/plugin/brave/history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
58 changes: 28 additions & 30 deletions client/src/plugin/git_repositories.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use crate::plugin::utils::Plugin;
use crate::{plugin::utils::Plugin, settings::Settings};
use anyhow::Context;

pub struct GitRepositoriesPlugin {
entries: Vec<crate::model::Entry>,
settings: Settings,
}

impl Plugin for GitRepositoriesPlugin {
fn id() -> &'static str {
"git-repositories"
"git_repositories"
}

fn priority() -> u32 {
Expand All @@ -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
Expand All @@ -32,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 All @@ -59,32 +68,21 @@ impl Plugin for GitRepositoriesPlugin {
entry: crate::model::Entry,
plugin_channel_out: &mut iced::futures::channel::mpsc::Sender<crate::Message>,
) -> 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<String> = 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)
Expand Down
52 changes: 28 additions & 24 deletions client/src/plugin/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,33 +167,37 @@ 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");
Ok(std::env::var("XDG_CONFIG_HOME").unwrap_or(config_in_home))
}

pub fn centerpiece_config_directory() -> anyhow::Result<String> {
let config_directory = config_directory()?;
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");
Ok(std::env::var("XDG_CACHE_HOME").unwrap_or(cache_in_home))
}

pub fn centerpiece_cache_directory() -> anyhow::Result<String> {
let cache_directory = cache_directory()?;
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 = 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<T, _> = serde_json::from_reader(reader);
Expand All @@ -204,5 +208,5 @@ where
);
panic!();
}
git_repository_paths_result.unwrap()
Ok(git_repository_paths_result.unwrap())
}
36 changes: 36 additions & 0 deletions client/src/settings.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use config::{Config, ConfigError};
use serde_derive::Deserialize;

#[derive(Debug, Deserialize)]
#[allow(unused)]
pub struct GitRepositoriesSettings {
friedow marked this conversation as resolved.
Show resolved Hide resolved
pub commands: Vec<Vec<String>>,
}

#[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<Self, ConfigError> {
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()
}
}
6 changes: 6 additions & 0 deletions config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
plugin:
git_repositories:
commands:
- ["alacritty", "--command", "nvim", "$GIT_DIRECTORY"]
- ["alacritty", "--command", "lazygit", "--path", "$GIT_DIRECTORY"]
- ["alacritty", "--working-directory", "$GIT_DIRECTORY"]
47 changes: 30 additions & 17 deletions home-manager-module.nix
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 = {
Expand All @@ -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";
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
Loading