1
0
mirror of https://github.com/httpie/cli.git synced 2025-08-10 22:42:05 +02:00

Support (part of) the XDG Base Directory Specification (#920)

On Unix-like systems, the configuration file now lives in
$XDG_CONFIG_HOME/httpie/ by default, not ~/.httpie/ (the behaviour on
Windows is unchanged). The previous location is still checked, in order
to support existing installations.

Searching $XDG_CONFIG_DIRS is still not supported.

Fixes #145; supersedes #436.
This commit is contained in:
Ash Holland
2020-05-21 14:50:00 +01:00
committed by GitHub
parent e11a2d1346
commit 5af0874ed3
3 changed files with 68 additions and 8 deletions

View File

@@ -8,11 +8,28 @@ from httpie import __version__
from httpie.compat import is_windows
DEFAULT_CONFIG_DIR = Path(os.environ.get(
'HTTPIE_CONFIG_DIR',
os.path.expanduser('~/.httpie') if not is_windows else
os.path.expandvars(r'%APPDATA%\\httpie')
))
def get_default_config_dir() -> Path:
"""Return the path to the httpie configuration directory.
This directory isn't guaranteed to exist, and nor are any of its
ancestors.
"""
env_config_dir = os.environ.get('HTTPIE_CONFIG_DIR')
if env_config_dir:
return Path(env_config_dir)
if is_windows:
return Path(os.path.expandvars(r'%APPDATA%\httpie'))
legacy_config_dir = os.path.expanduser('~/.httpie')
if os.path.exists(legacy_config_dir):
return Path(legacy_config_dir)
xdg_config_dir = os.environ.get('XDG_CONFIG_HOME')
if not xdg_config_dir or not os.path.isabs(xdg_config_dir):
xdg_config_dir = os.path.expanduser('~/.config')
httpie_config_dir = os.path.join(xdg_config_dir, 'httpie')
return Path(httpie_config_dir)
DEFAULT_CONFIG_DIR = get_default_config_dir()
class ConfigFileError(Exception):