Skip to content

Commit

Permalink
feat!: use XDG conventions on macOS too
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshgupta137 committed May 3, 2023
1 parent 0738da6 commit c7ff0af
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 100 deletions.
53 changes: 21 additions & 32 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ serde = { version = "1.0.160", features = [ "derive" ] }
serde_json = "1.0.96"
comfy-table = "6.1.4"
strum = { version = "0.24.1", features = [ "derive" ] }
dirs = "5.0.0"
etcetera = "0.8.0"
toml = "0.7.3"
indexmap = "1.9.3"
maxminddb = "0.23.0"
Expand Down
17 changes: 3 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -594,23 +594,12 @@ Trippy can be configured with via command line arguments or an optional configur
item is specified in both the configuration file and via a command line argument then the latter will take precedence.

The configuration file location may be provided to trippy via the `-c` (`--config-file`) argument. If not provided,
Trippy will attempt to locate a `trippy.toml` or `.trippy.toml` configuration file in one of the following platform
specific locations:
Trippy will attempt to locate a `trippy.toml` or `.trippy.toml` configuration file in one of the following locations:

- The current directory
- The user home directory
- The user config direction

For example, on Linux Trippy will attempt to locate the following config files (in order):

- `./trippy.toml`
- `./.trippy.toml`
- `$HOME/trippy.toml`
- `$HOME/.trippy.toml`
- `$HOME/.config/trippy.toml`
- `$HOME/.config/.trippy.toml`

See [here](https://github.com/dirs-dev/dirs-rs) for platform specific directory information.
- the XDG config directory (Unix only): `$XDG_CONFIG_HOME` or `~/.config`
- the Windows data directory (Windows only): `%APPDATA%`

An annotated [template configuration file](trippy-config-sample.toml) is available.

Expand Down
67 changes: 29 additions & 38 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1272,10 +1272,11 @@ pub mod config_file {
MultipathStrategyConfig, Protocol, TuiColor, TuiKeyBinding,
};
use anyhow::Context;
use etcetera::BaseStrategy;
use serde::Deserialize;
use std::fs::File;
use std::io::read_to_string;
use std::path::{Path, PathBuf};
use std::path::Path;

const DEFAULT_CONFIG_FILE: &str = "trippy.toml";
const DEFAULT_HIDDEN_CONFIG_FILE: &str = ".trippy.toml";
Expand All @@ -1285,40 +1286,27 @@ pub mod config_file {
/// Returns the parsed `Some(ConfigFile)` if the config file exists, `None` otherwise.
///
/// Trippy will attempt to locate a `trippy.toml` or `.trippy.toml`
/// config file in one of the following platform specific locations:
/// config file in one of the following locations:
/// - the current directory
/// - the user home directory
/// - the user config direction
///
/// For example, on Linux the Trippy will attempt to locate the following
/// files (in order):
/// - `./trippy.toml`
/// - `./.trippy.toml`
/// - `$HOME/trippy.toml`
/// - `$HOME/.trippy.toml`
/// - `$HOME/.config/trippy.toml`
/// - `$HOME/.config/.trippy.toml`
///
/// See [here](https://github.com/dirs-dev/dirs-rs) for platform specific directory
/// information.
/// - the XDG config directory (Unix only): `$XDG_CONFIG_HOME` or `~/.config`
/// - the Windows data directory (Windows only): `%APPDATA%`
///
/// Note that only the first config file found is used, no attempt is
/// made to merge the values from multiple files.
pub fn read_default_config_file() -> anyhow::Result<Option<ConfigFile>> {
if let Some(file) = read_file(|| Some(PathBuf::new()), DEFAULT_CONFIG_FILE)? {
Ok(Some(file))
} else if let Some(file) = read_file(|| Some(PathBuf::new()), DEFAULT_HIDDEN_CONFIG_FILE)? {
Ok(Some(file))
} else if let Some(file) = read_file(dirs::home_dir, DEFAULT_CONFIG_FILE)? {
Ok(Some(file))
} else if let Some(file) = read_file(dirs::home_dir, DEFAULT_HIDDEN_CONFIG_FILE)? {
Ok(Some(file))
} else if let Some(file) = read_file(dirs::config_dir, DEFAULT_CONFIG_FILE)? {
Ok(Some(file))
} else if let Some(file) = read_file(dirs::config_dir, DEFAULT_HIDDEN_CONFIG_FILE)? {
use etcetera::base_strategy as base;
if let Some(file) = read_files("")? {
Ok(Some(file))
} else {
Ok(None)
let basedirs = base::choose_base_strategy()?;
if let Some(file) = read_files(basedirs.home_dir())? {
Ok(Some(file))
} else if let Some(file) = read_files(basedirs.config_dir())? {
Ok(Some(file))
} else {
Ok(None)
}
}
}

Expand All @@ -1329,17 +1317,20 @@ pub mod config_file {
Ok(toml::from_str(&read_to_string(file)?)?)
}

fn read_file<F: FnOnce() -> Option<PathBuf>>(
dir: F,
file: &str,
) -> anyhow::Result<Option<ConfigFile>> {
if let Some(mut path) = dir() {
path.push(file);
if path.exists() {
Ok(Some(read_config_file(path)?))
} else {
Ok(None)
}
fn read_files<P: AsRef<Path>>(dir: P) -> anyhow::Result<Option<ConfigFile>> {
if let Some(file) = read_file(dir.as_ref(), DEFAULT_CONFIG_FILE)? {
Ok(Some(file))
} else if let Some(file) = read_file(dir.as_ref(), DEFAULT_HIDDEN_CONFIG_FILE)? {
Ok(Some(file))
} else {
Ok(None)
}
}

fn read_file<P: AsRef<Path>>(dir: P, file: &str) -> anyhow::Result<Option<ConfigFile>> {
let path = dir.as_ref().join(file);
if path.exists() {
Ok(Some(read_config_file(path)?))
} else {
Ok(None)
}
Expand Down
19 changes: 4 additions & 15 deletions trippy-config-sample.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,11 @@
# Copy this template config file to your platform specific config dir.
#
# Trippy will attempt to locate a `trippy.toml` or `.trippy.toml` config file
# in one of the following platform specific locations:
# in one of the following locations:
# the current directory
# the user home directory
# the user config directory
#
# For example, on Linux the Trippy will attempt to locate the following
# files (in order):
# `./trippy.toml`
# `./.trippy.toml`
# `$HOME/trippy.toml`
# `$HOME/.trippy.toml`
# `$HOME/.config/trippy.toml`
# `$HOME/.config/.trippy.toml`
#
# See https://github.com/dirs-dev/dirs-rs for platform specific directory
# information.
# the XDG config directory (Unix only): `$XDG_CONFIG_HOME` or `~/.config`
# the Windows data directory (Windows only): `%APPDATA%`
#
# You may override the config file name and location by passing the `-c`
# (`--config-file`) command line argument.
Expand Down Expand Up @@ -299,4 +288,4 @@ clear-dns-cache = "ctrl+k"
clear-selection = "esc"
toggle-as-info = "z"
toggle-hop-details = "d"
quit = "q"
quit = "q"

0 comments on commit c7ff0af

Please # to comment.