Skip to content

Commit

Permalink
feat: support custom config location
Browse files Browse the repository at this point in the history
- test: don't use real fixture file
  • Loading branch information
azzamsa committed Jun 17, 2024
1 parent 33458bb commit 8a04292
Show file tree
Hide file tree
Showing 6 changed files with 181 additions and 25 deletions.
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,6 @@ jobs:
- uses: taiki-e/install-action@just
- uses: taiki-e/install-action@nextest

- name: Prepare config
run: |
mkdir -p tests/.config/bilal
cp examples/config.toml tests/.config/bilal
- name: Run tests
run: just test

Expand Down
127 changes: 127 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 @@ -36,6 +36,7 @@ toml = { version = "0.8", features = ["parse"] }

[dev-dependencies]
assert_cmd = "2.0"
assert_fs = "1.1.1"
predicates = "3.1"

[package.metadata.binstall]
Expand Down
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ bilal current ... current salah time
bilal current --json .... with JSON format
```

Create a file named `bilal.toml` in `~/.config/bilal/`. If you are on `Windows`, put it under `\AppData\Bilal\`. Then add your configuration:
To configure Bilal, first create a file named `bilal.toml` in `~/.config/bilal/` on Unix-like systems. On Windows, place it under `\AppData\Bilal\`. If you prefer a custom location, simply set the `BILAL_CONFIG` environment variable.

Next, add your configuration details to the `bilal.toml` file:

```toml
latitude = -6.18233995
Expand All @@ -56,7 +58,7 @@ You can use Bilal with `i3status-rust` to show salah time in your status.

![i3status-rust-bilal-urgent](docs/i3status-rust-urgent.png)

`i3status-rurts` configuration Example:
`i3status-rust` configuration Example:

```bash
[[block]]
Expand Down Expand Up @@ -124,6 +126,6 @@ mu'azzin, chosen by Muhammad PBUH himself.

## License

Copyright (c) 2020-2023 azzamsa
Copyright (c) 2020-2024 azzamsa

Bilal is distributed under the terms of [GPL V3 License](LICENSE).
26 changes: 15 additions & 11 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,21 @@ fn parse(content: &str) -> Result<Config, Error> {

/// Return configuration path
fn path() -> Result<PathBuf, Error> {
let path = if cfg!(windows) {
Path::new(&std::env::var("APPDATA")?)
.join("Bilal")
.join("config.toml")
} else {
Path::new(&std::env::var("HOME")?)
.join(".config")
.join("bilal")
.join("config.toml")
};
Ok(path)
match &std::env::var("BILAL_CONFIG") {
Err(_) => {
if cfg!(windows) {
Ok(Path::new(&std::env::var("APPDATA")?)
.join("Bilal")
.join("config.toml"))
} else {
Ok(Path::new(&std::env::var("HOME")?)
.join(".config")
.join("bilal")
.join("config.toml"))
}
}
Ok(path) => Ok(PathBuf::from(path)),
}
}

fn deserialize_time_format<'de, D>(deserializer: D) -> Result<TimeFormat, D::Error>
Expand Down
39 changes: 33 additions & 6 deletions tests/integration.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{env, error::Error, process::Command};

use assert_cmd::{crate_name, prelude::*};
use assert_fs::{prelude::*, TempDir};
use predicates::prelude::*;

#[test]
Expand All @@ -15,8 +16,11 @@ fn help() -> Result<(), Box<dyn Error>> {

#[test]
fn all() -> Result<(), Box<dyn Error>> {
env::set_var("HOME", "./tests");
env::set_var("APPDATA", "./tests");
let temp_dir = setup_config()?;
env::set_var(
"BILAL_CONFIG",
format!("{}/config.toml", temp_dir.path().display()),
);

let mut cmd = Command::cargo_bin(crate_name!())?;
cmd.arg("all");
Expand All @@ -28,8 +32,11 @@ fn all() -> Result<(), Box<dyn Error>> {

#[test]
fn current() -> Result<(), Box<dyn Error>> {
env::set_var("HOME", "./tests");
env::set_var("APPDATA", "./tests");
let temp_dir = setup_config()?;
env::set_var(
"BILAL_CONFIG",
format!("{}/config.toml", temp_dir.path().display()),
);

let mut cmd = Command::cargo_bin(crate_name!())?;
cmd.arg("current").arg("--json");
Expand All @@ -42,8 +49,11 @@ fn current() -> Result<(), Box<dyn Error>> {

#[test]
fn next() -> Result<(), Box<dyn Error>> {
env::set_var("HOME", "./tests");
env::set_var("APPDATA", "./tests");
let temp_dir = setup_config()?;
env::set_var(
"BILAL_CONFIG",
format!("{}/config.toml", temp_dir.path().display()),
);

let mut cmd = Command::cargo_bin(crate_name!())?;
cmd.arg("next").arg("--json");
Expand All @@ -53,3 +63,20 @@ fn next() -> Result<(), Box<dyn Error>> {
.stdout(predicate::str::contains("\u{25b6}"));
Ok(())
}

fn setup_config() -> Result<TempDir, Box<dyn Error>> {
let temp_dir = assert_fs::TempDir::new()?;
let config = temp_dir.child("config.toml");
config.write_str(&config_base())?;
Ok(temp_dir)
}

fn config_base() -> String {
let content = r#"
latitude = -6.18233995
longitude = 106.84287154
madhab = "Shafi"
method = "Egyptian"
"#;
content.to_string()
}

0 comments on commit 8a04292

Please sign in to comment.