You've already forked httpie-cli
mirror of
https://github.com/httpie/cli.git
synced 2025-08-10 22:42:05 +02:00
Cleanup & refactor XDG_CONFIG_HOME support
This commit is contained in:
@@ -8,25 +8,51 @@ from httpie import __version__
|
||||
from httpie.compat import is_windows
|
||||
|
||||
|
||||
ENV_XDG_CONFIG_HOME = 'XDG_CONFIG_HOME'
|
||||
ENV_HTTPIE_CONFIG_DIR = 'HTTPIE_CONFIG_DIR'
|
||||
DEFAULT_CONFIG_DIRNAME = 'httpie'
|
||||
DEFAULT_RELATIVE_XDG_CONFIG_HOME = Path('.config')
|
||||
DEFAULT_RELATIVE_LEGACY_CONFIG_DIR = Path('.httpie')
|
||||
DEFAULT_WINDOWS_CONFIG_DIR = Path(
|
||||
os.path.expandvars('%APPDATA%')) / DEFAULT_CONFIG_DIRNAME
|
||||
|
||||
|
||||
def get_default_config_dir() -> Path:
|
||||
"""Return the path to the httpie configuration directory.
|
||||
"""
|
||||
Return the path to the httpie configuration directory.
|
||||
|
||||
This directory isn't guaranteed to exist, and nor are any of its
|
||||
ancestors.
|
||||
ancestors (only the legacy ~/.httpie, if returned, is guaranteed to exist).
|
||||
|
||||
XDG Base Directory Specification support:
|
||||
|
||||
<https://wiki.archlinux.org/index.php/XDG_Base_Directory>
|
||||
|
||||
$XDG_CONFIG_HOME is supported; $XDG_CONFIG_DIRS is not
|
||||
|
||||
"""
|
||||
env_config_dir = os.environ.get('HTTPIE_CONFIG_DIR')
|
||||
# 1. explicitly set through env
|
||||
env_config_dir = os.environ.get(ENV_HTTPIE_CONFIG_DIR)
|
||||
if env_config_dir:
|
||||
return Path(env_config_dir)
|
||||
|
||||
# 2. Windows
|
||||
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)
|
||||
return DEFAULT_WINDOWS_CONFIG_DIR
|
||||
|
||||
home_dir = Path.home()
|
||||
|
||||
# 3. legacy ~/.httpie
|
||||
legacy_config_dir = home_dir / DEFAULT_RELATIVE_LEGACY_CONFIG_DIR
|
||||
if legacy_config_dir.exists():
|
||||
return legacy_config_dir
|
||||
|
||||
# 4. XDG
|
||||
xdg_config_home_dir = os.environ.get(
|
||||
ENV_XDG_CONFIG_HOME, # 4.1. explicit
|
||||
home_dir / DEFAULT_RELATIVE_XDG_CONFIG_HOME # 4.2. default
|
||||
)
|
||||
return Path(xdg_config_home_dir) / DEFAULT_CONFIG_DIRNAME
|
||||
|
||||
|
||||
DEFAULT_CONFIG_DIR = get_default_config_dir()
|
||||
|
Reference in New Issue
Block a user