-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnonix.py
executable file
·77 lines (61 loc) · 1.84 KB
/
nonix.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
#!/usr/bin/env python
import os
import sys
from dataclasses import dataclass
@dataclass
class LinkItem:
name: str
source: str
des: str
todo: list[LinkItem] = []
roaming = ["nushell"]
config = ["nvim"]
def get_des_path(name: str) -> str | None:
match sys.platform:
case "linux":
return os.path.join(os.path.expanduser("~/.config"), name)
case "win32":
local_dir = os.path.join(os.environ["USERPROFILE"], "AppData", "Local")
roaming_dir = os.path.join(os.environ["USERPROFILE"], "AppData", "Roaming")
roaming_list = ["nushell"]
local_list = ["nvim"]
if name in roaming_list:
return os.path.join(roaming_dir, name)
elif name in local_list:
return os.path.join(local_dir, name)
else:
return None
case _:
raise OSError("your os is not currently supported")
def init():
for dir in os.listdir("config"):
s = os.path.abspath(os.path.join("config", dir))
d = get_des_path(dir)
if d is None:
continue
todo.append(LinkItem(name=dir, source=s, des=d))
def link():
try:
for item in todo:
print(f"symlink {item.source} to {item.des}")
os.symlink(item.source, item.des)
except OSError as e:
print(f"create symlink error {e}")
def clean():
for item in todo:
folder = item.des
if os.path.exists(folder):
print(f"deleting {folder}")
os.remove(folder)
if __name__ == "__main__":
# populate todo list
init()
if len(sys.argv) == 1:
link()
elif len(sys.argv) == 2:
if sys.argv[1] == "clean":
clean()
else:
print(f"{sys.argv[1]} not supported")
else:
print("too many arguments")