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 c63b03d
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 148 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
36 changes: 22 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,15 @@ 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(),
})
}

/// 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 +329,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: appdirs.cache_dir(),
source: PathSource::OsConvention,
}
} else {
Expand All @@ -337,15 +347,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: appdirs.data_dir(),
source: PathSource::OsConvention,
})
.ok()
} else {
None
}
});
let directories = DirectoriesConfig {
cache_dir,
Expand Down Expand Up @@ -415,9 +424,8 @@ 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((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
9 changes: 7 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ impl LineType {
/// The reason why a certain path (e.g. config path or cache dir) was chosen.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum PathSource {
/// OS convention (e.g. XDG on Linux)
/// OS convention (e.g. XDG on Unix)
OsConvention,
/// Env variable (TEALDEER_*)
EnvVar,
Expand All @@ -201,7 +201,12 @@ impl fmt::Display for PathSource {
f,
"{}",
match self {
Self::OsConvention => "OS convention",
Self::OsConvention =>
if cfg!(windows) {
"Windows convention"
} else {
"XDG convention"
},
Self::EnvVar => "env variable",
Self::ConfigFile => "config file",
}
Expand Down

0 comments on commit c63b03d

Please sign in to comment.