[eryntools] Add a dotfiles package to eryntools

Implement a utility for finding the dotfiles repo. It assumes ~/.dotfiles unless an override is given.
This commit is contained in:
Eryn Wells 2024-03-05 11:01:05 -08:00
parent f64df23eda
commit 7f2ab12b1e

View file

@ -0,0 +1,32 @@
# Eryn Wells <eryn@erynwells.me>
from os.path import expanduser, expandvars, isdir, join
from typing import Optional
def find_repository(path: Optional[str] = None) -> Optional[str]:
'''Find the dotfiles repo.'''
_DEFAULT_CANDIDATE_PATHS = [
'~/.dotfiles',
]
candidate_paths: list[str] = []
if path:
candidate_paths.append(path)
candidate_paths.extend(_DEFAULT_CANDIDATE_PATHS)
for path in candidate_paths:
path = expanduser(path)
path = expandvars(path)
if not path_is_git_repository(path):
continue
return path
return None
def path_is_git_repository(path: str) -> bool:
return bool(path) and isdir(path) and isdir(join(path, '.git'))