[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:
parent
f64df23eda
commit
7f2ab12b1e
1 changed files with 32 additions and 0 deletions
32
Python/eryntools/src/eryntools/dotfiles/__init__.py
Normal file
32
Python/eryntools/src/eryntools/dotfiles/__init__.py
Normal 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'))
|
Loading…
Add table
Add a link
Reference in a new issue