Skip to content

Commit

Permalink
Add default configuration file from OS specific location
Browse files Browse the repository at this point in the history
  • Loading branch information
triarius committed Aug 31, 2024
1 parent 6dba94d commit 52204a1
Show file tree
Hide file tree
Showing 3 changed files with 164 additions and 13 deletions.
158 changes: 148 additions & 10 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ edition = "2021"
build = "build.rs"

[dependencies]
clap = { version = "4.5.13", features = ["derive", "env"] }
clap = { version = "4.5.13", features = ["derive", "env", "string"] }
clap-serde-derive = "0.2.1"
color-eyre = "0.6.3"
directories = "5.0.1"
env_logger = "0.11.5"
log = "0.4.22"
nom = "7.1.3"
Expand Down
16 changes: 14 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use std::{
#[command(version)]
struct Args {
/// The debug level.
#[arg(short, long, action = clap::ArgAction::Count)]
#[arg(short, long, env = "ELEPHANTINE_DEBUG", action = clap::ArgAction::Count)]
debug: u8,

/// Path to the configuration file.
#[arg(long, value_name = "FILE", default_value = "config.toml")]
#[arg(long, env = "ELEPHANTINE_CONFIG_FILE", value_name = "FILE", default_value = default_config_file())]
config_file: PathBuf,

/// The configuration options.
Expand All @@ -37,3 +37,15 @@ fn main() -> Result<()> {
let mut output = stdout();
Listener::new(config).listen(input, &mut output)
}

fn default_config_file() -> String {
directories::ProjectDirs::from("org", "elephantine", "elephantine").map_or_else(
|| "elephantine.toml".to_string(),
|dirs| {
dirs.config_dir()
.join("elephantine.toml")
.to_string_lossy()
.to_string()
},
)
}

0 comments on commit 52204a1

Please sign in to comment.