Skip to content

Commit

Permalink
Configurize cache path
Browse files Browse the repository at this point in the history
  • Loading branch information
coastalwhite committed Apr 6, 2024
1 parent 6a7d6ca commit 2a60abb
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 15 deletions.
4 changes: 4 additions & 0 deletions extra/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ main_log_path = "/var/log/lemurs.log"
# Window Manager for Xorg, the Compositor for Wayland and the Shell for TTY.
client_log_path = "/var/log/lemurs.client.log"

# At which point to point the cache. If you want to disable the cache globally
# you can use `/dev/null`.
cache_path = "/var/cache/lemurs";

# Disable all logging. This is overwritten by the `--no-log` flag.
do_log = true

Expand Down
1 change: 1 addition & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ toml_config_struct! { Config, PartialConfig, RoughConfig,

main_log_path => String,
client_log_path => String,
cache_path => String,

do_log => bool,

Expand Down
24 changes: 13 additions & 11 deletions src/info_caching.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use log::{info, warn};
use std::fs::{read_to_string, write};

pub const CACHE_PATH: &str = "/var/cache/lemurs";
use crate::config::Config;

const USERNAME_LENGTH_LIMIT: usize = 32;

// Saved in the /var/cache/lemurs file as
Expand Down Expand Up @@ -48,13 +49,12 @@ impl CachedInfo {
}
}

pub fn get_cached_information() -> CachedInfo {
info!(
"Attempting to get a cached information from '{}'",
CACHE_PATH
);
pub fn get_cached_information(config: &Config) -> CachedInfo {
let cache_path = &config.cache_path;

match read_to_string(CACHE_PATH) {
info!("Attempting to get a cached information from '{cache_path}'",);

match read_to_string(cache_path) {
Ok(cached) => {
// Remove any line feeds
let cached = cached.trim().to_string();
Expand Down Expand Up @@ -102,8 +102,10 @@ pub fn get_cached_information() -> CachedInfo {
}
}

pub fn set_cache(environment: Option<&str>, username: Option<&str>) {
info!("Attempting to set cache");
pub fn set_cache(environment: Option<&str>, username: Option<&str>, config: &Config) {
let cache_path = &config.cache_path;

info!("Attempting to set cache: {cache_path}");

let username = if let Some(username) = username {
// Username length check
Expand All @@ -129,9 +131,9 @@ pub fn set_cache(environment: Option<&str>, username: Option<&str>) {
username.unwrap_or_default()
);

match write(CACHE_PATH, cache_file_content) {
match write(cache_path, cache_file_content) {
Err(err) => {
warn!("Failed to set username to cache file. Reason: '{}'", err);
warn!("Failed to set username to cache file. Reason: '{err}'");
}
_ => {
info!("Successfully set username in cache file");
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,14 @@ fn main() -> Result<(), Box<dyn Error>> {
}
}
Commands::Cache => {
let cached_info = info_caching::get_cached_information();
let cached_info = info_caching::get_cached_information(&config);

let environment = cached_info.environment().unwrap_or("No cached value");
let username = cached_info.username().unwrap_or("No cached value");

println!(
"Information currently cached within '{}'\n",
info_caching::CACHE_PATH
config.cache_path
);

println!("environment: '{environment}'");
Expand Down
4 changes: 2 additions & 2 deletions src/ui/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,14 +257,14 @@ impl LoginForm {
.then_some(self.widgets.get_username());

info!("Setting cached information");
set_cache(selected_env.as_deref(), username.as_deref());
set_cache(selected_env.as_deref(), username.as_deref(), &self.config);
}

fn load_cache(&self) {
let env_remember = self.config.environment_switcher.remember;
let username_remember = self.config.username_field.remember;

let cached = get_cached_information();
let cached = get_cached_information(&self.config);

if username_remember {
if let Some(username) = cached.username() {
Expand Down

0 comments on commit 2a60abb

Please sign in to comment.