refactor: read client_secret.json from disk instead of env vars

This commit is contained in:
Ruben Rosario
2026-06-21 10:23:25 +01:00
parent 9da086b7be
commit ae9910bcbc
2 changed files with 46 additions and 22 deletions
+40 -7
View File
@@ -4,6 +4,7 @@ mod infrastructure;
mod ui;
use std::io;
use std::path::PathBuf;
use std::sync::Arc;
use crossterm::event::{self, Event};
@@ -19,9 +20,33 @@ use crate::infrastructure::api::ApiClient;
use crate::infrastructure::db::Db;
use crate::ui::{draw, AppView, NetworkStatus};
fn find_secret_file() -> Option<PathBuf> {
if let Ok(path) = std::env::var("GOOGLE_CLIENT_SECRET_FILE") {
let p = PathBuf::from(&path);
if p.exists() {
return Some(p);
}
}
let config_path = dirs::config_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join("task_app")
.join("client_secret.json");
if config_path.exists() {
return Some(config_path);
}
let local_path = PathBuf::from("client_secret.json");
if local_path.exists() {
return Some(local_path);
}
None
}
fn main() -> io::Result<()> {
let db_path = dirs::data_dir()
.unwrap_or_else(|| std::path::PathBuf::from("."))
.unwrap_or_else(|| PathBuf::from("."))
.join("task_app")
.join("tasks.db");
@@ -29,6 +54,17 @@ fn main() -> io::Result<()> {
let db = Arc::new(Db::new(db_path.to_str().unwrap()).expect("Failed to open database"));
let secret_path = find_secret_file().unwrap_or_else(|| {
eprintln!(
"ERROR: Google client secret file not found.\n\
Place client_secret.json in one of:\n\
- Set GOOGLE_CLIENT_SECRET_FILE env var\n\
- ~/.config/task_app/client_secret.json\n\
- ./client_secret.json (current directory)"
);
std::process::exit(1);
});
enable_raw_mode()?;
let mut stdout = io::stdout();
stdout.execute(EnterAlternateScreen)?;
@@ -39,12 +75,9 @@ fn main() -> io::Result<()> {
tokio::runtime::Runtime::new()
.unwrap()
.block_on(async {
ApiClient::new(
std::env::var("GOOGLE_CLIENT_ID").unwrap_or_default(),
std::env::var("GOOGLE_CLIENT_SECRET").unwrap_or_default(),
)
.await
.expect("Failed to create ApiClient")
ApiClient::new(&secret_path)
.await
.expect("Failed to create ApiClient")
}),
);