Skip to content

Commit

Permalink
Use XDG conventions on macOS too
Browse files Browse the repository at this point in the history
  • Loading branch information
utkarshgupta137 committed Apr 22, 2023
1 parent 94d56c0 commit 715af6f
Show file tree
Hide file tree
Showing 9 changed files with 65 additions and 153 deletions.
146 changes: 20 additions & 126 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 @@ -21,10 +21,10 @@ path = "src/main.rs"

[dependencies]
anyhow = "1"
app_dirs = { version = "2", package = "app_dirs2" }
atty = "0.2"
clap = { version = "3", features = ["std", "derive", "suggestions", "color"], default-features = false }
env_logger = { version = "0.10", optional = true }
etcetera = "0.7"
log = "0.4"
reqwest = { version = "0.11.3", features = ["blocking"], default-features = false }
serde = "1.0.21"
Expand Down
4 changes: 2 additions & 2 deletions docs/src/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ format](https://toml.io/) called `config.toml`.
## Configfile Path

The configuration file path follows OS conventions (e.g.
`$XDG_CONFIG_HOME/tealdeer/config.toml` on Linux). The paths can be queried
`$XDG_CONFIG_HOME/tealdeer/config.toml` on Unix). The paths can be queried
with the following command:

$ tldr --show-paths
Expand All @@ -15,7 +15,7 @@ Creating the config file can be done manually or with the help of `tldr`:

$ tldr --seed-config

On Linux, this will usually be `~/.config/tealdeer/config.toml`.
On Unix, this will usually be `~/.config/tealdeer/config.toml`.

## Config Example

Expand Down
2 changes: 1 addition & 1 deletion docs/src/config_directories.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ exist, it will be created.
cache_dir = "/home/myuser/.tealdeer-cache/"

If no `cache_dir` is specified, tealdeer will fall back to a location that
follows OS conventions. On Linux, it will usually be at `~/.cache/tealdeer/`.
follows OS conventions. On Unix, it will usually be at `~/.cache/tealdeer/`.
Use `tldr --show-paths` to show the path that is being used.

## `custom_pages_dir`
Expand Down
2 changes: 1 addition & 1 deletion docs/src/usage_custom_pages.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tealdeer allows creating new custom pages, overriding existing pages as well as
extending existing pages.

The directory, where these custom pages and patches can be placed, follows OS
conventions. On Linux for instance, the default location is
conventions. On Unix for instance, the default location is
`~/.local/share/tealdeer/pages/`. To print the path used on your system, simply
run `tldr --show-paths`.

Expand Down
46 changes: 32 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ use std::{
};

use anyhow::{bail, ensure, Context, Result};
use app_dirs::{get_app_root, AppDataType};
use etcetera::{AppStrategy, AppStrategyArgs, HomeDirError};
use log::debug;
use serde_derive::{Deserialize, Serialize};
use yansi::{Color, Style};

use crate::types::PathSource;
use crate::NAME;

pub const CONFIG_FILE_NAME: &str = "config.toml";
pub const MAX_CACHE_AGE: Duration = Duration::from_secs(2_592_000); // 30 days
Expand Down Expand Up @@ -291,6 +292,22 @@ pub struct Config {
}

impl Config {
/// Get the appdirs for the current platform.
fn get_appdirs() -> Result<impl AppStrategy, HomeDirError> {
etcetera::choose_app_strategy(AppStrategyArgs {
top_level_domain: "org".into(),
author: NAME.into(),
app_name: NAME.into(),
})
}

pub fn pop_if_windows(mut path: PathBuf) -> PathBuf {
if cfg!(windows) {
path.pop();
}
path
}

/// Convert a `RawConfig` to a high-level `Config`.
///
/// For this, some values need to be converted to other types and some
Expand Down Expand Up @@ -319,10 +336,10 @@ impl Config {
path: config_value,
source: PathSource::ConfigFile,
}
} else if let Ok(default_dir) = get_app_root(AppDataType::UserCache, &crate::APP_INFO) {
} else if let Ok(appdirs) = Self::get_appdirs() {
// Otherwise, fall back to the default user cache directory.
PathWithSource {
path: default_dir,
path: Self::pop_if_windows(appdirs.cache_dir()),
source: PathSource::OsConvention,
}
} else {
Expand All @@ -337,15 +354,14 @@ impl Config {
source: PathSource::ConfigFile,
})
.or_else(|| {
get_app_root(AppDataType::UserData, &crate::APP_INFO)
.map(|path| {
// Note: The `join("")` call ensures that there's a trailing slash
PathWithSource {
path: path.join("pages").join(""),
source: PathSource::OsConvention,
}
if let Ok(appdirs) = Self::get_appdirs() {
Some(PathWithSource {
path: Self::pop_if_windows(appdirs.cache_dir()).join("pages"),
source: PathSource::OsConvention,
})
.ok()
} else {
None
}
});
let directories = DirectoriesConfig {
cache_dir,
Expand Down Expand Up @@ -415,9 +431,11 @@ pub fn get_config_dir() -> Result<(PathBuf, PathSource)> {
};

// Otherwise, fall back to the user config directory.
let dirs = get_app_root(AppDataType::UserConfig, &crate::APP_INFO)
.context("Failed to determine the user config directory")?;
Ok((dirs, PathSource::OsConvention))
let appdirs = Config::get_appdirs().context("Failed to determine the user home directory")?;
Ok((
Config::pop_if_windows(appdirs.config_dir()),
PathSource::OsConvention,
))
}

/// Return the path to the config file.
Expand Down
5 changes: 0 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ compile_error!(

use std::{env, process};

use app_dirs::AppInfo;
use atty::Stream;
use clap::Parser;

Expand All @@ -57,10 +56,6 @@ use crate::{
};

const NAME: &str = "tealdeer";
const APP_INFO: AppInfo = AppInfo {
name: NAME,
author: NAME,
};
const ARCHIVE_URL: &str = "https://tldr.sh/assets/tldr.zip";

/// The cache should be updated if it was explicitly requested,
Expand Down

0 comments on commit 715af6f

Please sign in to comment.