diff --git a/src/client/config.rs b/src/client/config.rs index 213f5b5..84192cf 100644 --- a/src/client/config.rs +++ b/src/client/config.rs @@ -240,9 +240,26 @@ impl ClientConfig { } } +// impl From for ClientConfig { +// fn from(config: GlimConfig) -> Self { +// Self::new(config.gitlab_url, config.gitlab_token).with_search_filter(config.search_filter) +// } +// } +// impl From for ClientConfig { fn from(config: GlimConfig) -> Self { - Self::new(config.gitlab_url, config.gitlab_token).with_search_filter(config.search_filter) + let token = if let Some(token_file) = &config.gitlab_token_file { + std::fs::read_to_string(token_file) + .map(|s| s.trim().to_string()) + .unwrap_or_else(|e| { + tracing::warn!("Failed to read token file {:?}: {}", token_file, e); + config.gitlab_token.to_string() + }) + } else { + config.gitlab_token.to_string() + }; + + Self::new(config.gitlab_url, token).with_search_filter(config.search_filter) } } @@ -354,6 +371,7 @@ mod tests { let glim_config = GlimConfig { gitlab_url: "https://gitlab.example.com".into(), gitlab_token: "test-token".into(), + gitlab_token_file: None, search_filter: Some("test".into()), log_level: Some("Off".into()), animations: true, diff --git a/src/glim_app.rs b/src/glim_app.rs index 5847f0b..cfe78fc 100644 --- a/src/glim_app.rs +++ b/src/glim_app.rs @@ -42,6 +42,8 @@ pub struct GlimConfig { pub gitlab_url: CompactString, /// The Personal Access Token to authenticate with GitLab pub gitlab_token: CompactString, + #[serde(default)] + pub gitlab_token_file: Option, /// Filter applied to the projects list pub search_filter: Option, /// Logging level: Off, Error, Warn, Info, Debug, Trace @@ -56,6 +58,7 @@ impl Default for GlimConfig { Self { gitlab_url: "https://".into(), gitlab_token: "".into(), + gitlab_token_file: None, search_filter: None, log_level: Some("Error".into()), animations: true, diff --git a/src/ui/popup/config_popup.rs b/src/ui/popup/config_popup.rs index 34892bc..aee1af5 100644 --- a/src/ui/popup/config_popup.rs +++ b/src/ui/popup/config_popup.rs @@ -1,4 +1,4 @@ -use std::vec; +use std::{path::PathBuf, vec}; use compact_str::{CompactString, ToCompactString}; use ratatui::{ @@ -164,9 +164,14 @@ impl ConfigPopupState { .unwrap_or(&"") .trim() .to_compact_string(); - let search_filter_value = values.get(2).unwrap_or(&"").trim(); - let log_level_value = values.get(3).unwrap_or(&"Off").trim(); - let animations_value = values.get(4).unwrap_or(&"true").trim(); + let gitlab_token_file = values + .get(2) + .map(|s| s.trim()) + .filter(|s| !s.is_empty()) + .map(|s| PathBuf::from(s)); + let search_filter_value = values.get(3).unwrap_or(&"").trim(); + let log_level_value = values.get(4).unwrap_or(&"Off").trim(); + let animations_value = values.get(5).unwrap_or(&"true").trim(); let search_filter = if search_filter_value.is_empty() { None @@ -185,6 +190,7 @@ impl ConfigPopupState { GlimConfig { gitlab_url, gitlab_token, + gitlab_token_file, search_filter, log_level, animations,