-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdfm.py
108 lines (84 loc) · 2.72 KB
/
dfm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import argparse
import os
import sys
from typing import Tuple
from dataclasses import dataclass
@dataclass
class FileConfig:
relative_path: str
FILES = {
"alacritty.toml": FileConfig(".config"),
"inputrc": FileConfig(""),
"init.lua": FileConfig(".config/nvim"),
"tmux.conf": FileConfig(""),
"zshrc": FileConfig(""),
}
def get_dotfiles_dir() -> str:
return os.path.dirname(os.path.realpath(__file__))
def calculate_destination_dir(
dest_dir: str, file: str, file_config: FileConfig
) -> Tuple[str, str]:
destination_dir = os.path.join(dest_dir, file_config.relative_path)
filename = file
if file_config.relative_path == "":
filename = "." + filename
return (destination_dir, filename)
def install(args):
for file, file_config in FILES.items():
(destination_dir, filename) = calculate_destination_dir(
args.dest_dir, file, file_config
)
try:
os.makedirs(destination_dir)
except FileExistsError:
pass
source = os.path.join(get_dotfiles_dir(), file)
destination = os.path.join(destination_dir, filename)
print(f"Symlink: {source} -> {destination}")
try:
os.symlink(source, destination)
except FileExistsError:
pass
def uninstall(args):
for file, file_config in FILES.items():
(destination_dir, filename) = calculate_destination_dir(
args.dest_dir, file, file_config
)
destination = os.path.join(destination_dir, filename)
print(f"Removing symlink: {destination}")
try:
os.remove(destination)
except FileNotFoundError:
pass
def main():
parser = argparse.ArgumentParser(description="dotfile management tool")
subparsers = parser.add_subparsers()
# Install
install_command_parser = subparsers.add_parser("install")
install_command_parser.add_argument(
"--dest-dir",
type=str,
nargs=1,
default=os.path.expanduser("~"),
required=False,
help="Destination directory to install dotfiles to (Defaults to homedir)",
)
install_command_parser.set_defaults(func=install)
# Uninstall
uninstall_command_parser = subparsers.add_parser("uninstall")
uninstall_command_parser.add_argument(
"--dest-dir",
type=str,
nargs=1,
default=os.path.expanduser("~"),
required=False,
help="Destination directory to remove dotfiles from (Defaults to homedir)",
)
uninstall_command_parser.set_defaults(func=uninstall)
args = parser.parse_args()
if hasattr(args, "func"):
args.func(args)
else:
parser.print_help()
if __name__ == "__main__":
main()