This commit is contained in:
Christoph Stahl 2022-10-28 02:07:38 +02:00
parent 786ac39033
commit 75325f3baa

View file

@ -15,13 +15,14 @@ import sys
basedir = os.path.expanduser("~/.dotfiles")
def getpaths(path):
absolutpath = os.path.abspath(os.path.expanduser(path))
dotfilepath = basedir + absolutpath[len(os.path.expanduser("~")):]
dotfilepath = basedir + absolutpath[len(os.path.expanduser("~")) :]
if absolutpath.startswith(basedir):
dotfilepath = absolutpath
absolutpath = os.path.expanduser("~" + absolutpath[len(basedir):])
absolutpath = os.path.expanduser("~" + absolutpath[len(basedir) :])
return absolutpath, dotfilepath
@ -30,11 +31,14 @@ def link(path, force):
absolutpath, dotfilepath = getpaths(path)
if not absolutpath.startswith(os.path.expanduser("~")):
print(f"Could not add {path} to repo, only files in your home directory are supported", file=sys.stderr)
print(
f"Could not add {path} to repo, only files in your home directory are supported",
file=sys.stderr,
)
sys.exit(2)
if absolutpath == os.path.expanduser("~"):
print(f"Cannot add home directory to dotfiles", file=sys.stderr)
print("Cannot add home directory to dotfiles", file=sys.stderr)
sys.exit(3)
if absolutpath.startswith(basedir):
@ -55,6 +59,7 @@ def link(path, force):
os.system(f"git -C {basedir} add {dotfilepath}")
os.symlink(dotfilepath, absolutpath)
def unlink(path):
absolutpath, dotfilepath = getpaths(path)
@ -89,12 +94,14 @@ def apply(path, force):
shutil.rmtree(absolutpath)
os.makedirs(os.path.dirname(absolutpath), exist_ok=True)
print(f"{dotfilepath} -> {absolutpath}")
os.link(dotfilepath, absolutpath)
def sync():
os.system(f"git -C {basedir} pull")
os.system(f"git -C {basedir} commit -am \"dotfiles\"")
os.system(f'git -C {basedir} commit -am "dotfiles"')
os.system(f"git -C {basedir} push")
@ -112,11 +119,11 @@ def main():
symlink_parser = subparsers.add_parser("apply")
sync_parser = subparsers.add_parser("sync")
add_parser.add_argument('--force', '-f', action="store_true")
add_parser.add_argument('path')
unlink_parser.add_argument('path')
symlink_parser.add_argument('--force', '-f', action="store_true")
symlink_parser.add_argument('path')
add_parser.add_argument("--force", "-f", action="store_true")
add_parser.add_argument("path")
unlink_parser.add_argument("path")
symlink_parser.add_argument("--force", "-f", action="store_true")
symlink_parser.add_argument("path")
args = parser.parse_args()
@ -130,5 +137,6 @@ def main():
case "sync":
sync()
if __name__ == "__main__":
main()