Skip to content

Commit

Permalink
feat: read credentials.ini from XDG_CONFIG_DIR (or os equivalents) in…
Browse files Browse the repository at this point in the history
… addition to bin directory
  • Loading branch information
RobBrazier committed Aug 11, 2024
1 parent ea1b09b commit b46f997
Show file tree
Hide file tree
Showing 3 changed files with 151 additions and 7 deletions.
121 changes: 121 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ configparser = { version = "3.1.0", features = ["indexmap"] }
serde = { version = "1.0.204", features = ["derive"] }
chrono = "0.4.38"
webbrowser = "1.0.1"
dirs = "5.0.1"

[profile.release]
strip = "debuginfo"
36 changes: 29 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use chrono::{DateTime, FixedOffset, SecondsFormat, Utc};
use configparser::ini::Ini;
use serde::Deserialize;
use std::{env, io, time::Duration};
use std::{env, io, path::PathBuf, time::Duration};
use ureq::AgentBuilder;

#[derive(Deserialize)]
Expand Down Expand Up @@ -141,12 +141,34 @@ impl Env {
}
}

fn find_config_file() -> Option<PathBuf> {
let xdg_config_path = dirs::config_local_dir().unwrap().join("discrakt");
let mut exe_path = env::current_exe().unwrap();
exe_path.pop();

let locations = vec![xdg_config_path, exe_path];

for location in &locations {
let config_file = location.join("credentials.ini");
if config_file.exists() {
return Some(config_file);
}
}
eprintln!(
"Could not find credentials.ini in {:?}",
locations
.iter()
.map(|loc| loc.to_str().to_owned().unwrap())
.collect::<Vec<_>>()
);
None
}

pub fn load_config() -> Env {
let mut config = Ini::new();
let mut path = env::current_exe().unwrap();
path.pop();
path.push("credentials.ini");
let config_file = find_config_file();

let path = config_file.expect("Could not find credentials.ini");
config.load(path).expect("Failed to load credentials.ini");

Env {
Expand All @@ -173,9 +195,9 @@ pub fn load_config() -> Env {

fn set_oauth_tokens(json_response: &TraktAccessToken) {
let mut config = Ini::new_cs();
let mut path = env::current_exe().unwrap();
path.pop();
path.push("credentials.ini");
let config_file = find_config_file();

let path = config_file.expect("Could not find credentials.ini");

config
.load(path.clone())
Expand Down

0 comments on commit b46f997

Please sign in to comment.