Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use XDG conventions on macOS too
Browse files Browse the repository at this point in the history
utkarshgupta137 committed Apr 22, 2023

Unverified

This user has not yet uploaded their public signing key.
1 parent 94d56c0 commit 72c08a1
Showing 5 changed files with 51 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
@@ -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.6"
log = "0.4"
reqwest = { version = "0.11.3", features = ["blocking"], default-features = false }
serde = "1.0.21"
37 changes: 23 additions & 14 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -6,12 +6,14 @@ use std::{
};

use anyhow::{bail, ensure, Context, Result};
use app_dirs::{get_app_root, AppDataType};
use etcetera::app_strategy::{AppStrategy, AppStrategyArgs};
use etcetera::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
@@ -291,6 +293,15 @@ pub struct Config {
}

impl Config {
/// Get the appdirs for the current platform.
fn get_appdirs() -> Result<impl AppStrategy, HomeDirError> {
etcetera::app_strategy::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
@@ -319,10 +330,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 {
@@ -337,15 +348,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,
@@ -415,9 +425,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.
5 changes: 0 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -32,7 +32,6 @@ compile_error!(

use std::{env, process};

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

@@ -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,
9 changes: 7 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -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,
@@ -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",
}

0 comments on commit 72c08a1

Please sign in to comment.