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

feat: read credentials.ini from XDG_CONFIG_HOME (or os equivalents) in addition to bin directory #103

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
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"
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ Plex Rich Presence alternatives:
*P.S.* Discord needs to be running on the machine Discrakt is running on.
*P.P.S.* Place the `credentials.ini` file in the same directory as the executable.

*P.P.P.S.* If you want to store the configuration in a common location, the `credentials.ini` can also be stored in:

|Operating System|Location|Example|
|--------|-----|-------|
|Linux|`$XDG_CONFIG_HOME`/discrakt or `$HOME`/.config/discrakt|/home/alice/.config/discrakt/credentials.ini|
|macOS|`$HOME`/Library/Application Support/discrakt|/Users/Alice/Library/Application Support/discrakt/credentials.ini|
|Windows|`%LOCALAPPDATA%`\discrakt|C:\Users\Alice\AppData\Local\discrakt\credentials.ini|
afonsojramos marked this conversation as resolved.
Show resolved Hide resolved

## Running executables

Running the executables is as easy as clicking the provided executables in the latest [release](https://github.com/afonsojramos/discrakt/releases). That's it!
Expand Down
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];
afonsojramos marked this conversation as resolved.
Show resolved Hide resolved

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");
afonsojramos marked this conversation as resolved.
Show resolved Hide resolved

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