From 75325f3baaf07656ff133c917339e56325cd6e30 Mon Sep 17 00:00:00 2001 From: Christoph Stahl Date: Fri, 28 Oct 2022 02:07:38 +0200 Subject: [PATCH] dotfiles --- .local/bin/dotfiles | 30 +++++++++++++++++++----------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/.local/bin/dotfiles b/.local/bin/dotfiles index eb12845..7ee29eb 100755 --- a/.local/bin/dotfiles +++ b/.local/bin/dotfiles @@ -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()