-
Notifications
You must be signed in to change notification settings - Fork 0
/
git-url-short.py
132 lines (129 loc) · 4.38 KB
/
git-url-short.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import sys
from colorama import Fore, Style
from urllib.request import urlopen
import urllib.request
from git import Repo
import os
def fast_scandir(dirname):
subfolders= [f.path for f in os.scandir(dirname) if f.is_dir()]
for dirname in list(subfolders):
subfolders.extend(fast_scandir(dirname))
return subfolders
args = sys.argv
repo_url = "[email protected]:sailingteam4/sailingteam4.github.io.git"
if len(args) == 3:
if args[1] == "-d":
display_name = args[2]
if (os.path.exists("/tmp/git-short-url/")):
print(Fore.RED + "Repo already exists" + Style.RESET_ALL)
entry = input("Do you want to remove the repo? (y/n): ")
if entry == "y":
os.system("rm -rf /tmp/git-short-url/")
else:
exit()
if (' ' in display_name):
print(Fore.RED + "Display name format invalid" + Style.RESET_ALL)
exit()
try:
repo = Repo.clone_from(repo_url, "/tmp/git-short-url/")
except:
print(Fore.RED + "Error cloning repo" + Style.RESET_ALL)
exit()
if (os.path.exists("/tmp/git-short-url/u/" + display_name) == False):
print(Fore.RED + "Display name not found" + Style.RESET_ALL)
exit()
try:
os.system("rm -rf /tmp/git-short-url/u/" + display_name)
except:
print(Fore.RED + "Error deleting file" + Style.RESET_ALL)
exit()
try:
repo.index.remove(["u/" + display_name + "/index.html"])
repo.index.commit("Link shortner delete " + display_name)
repo.remotes.origin.push()
except:
print(Fore.RED + "Error pushing to repo" + Style.RESET_ALL)
exit()
os.system("rm -rf /tmp/git-short-url/")
print(Fore.GREEN + "URL deleted successfully" + Style.RESET_ALL)
exit()
else:
url = args[1]
display_name = args[2]
try:
urlopen(url)
except:
print(Fore.RED + "Invalid URL" + Style.RESET_ALL)
exit()
if (' ' in display_name):
print(Fore.RED + "Display name format invalid" + Style.RESET_ALL)
exit()
print("Shortening URL: "+ Fore.GREEN + url + Style.RESET_ALL +" with display name: " + Fore.GREEN + display_name + Style.RESET_ALL)
if (os.path.exists("/tmp/git-short-url/")):
print(Fore.RED + "Repo already exists" + Style.RESET_ALL)
entry = input("Do you want to remove the repo? (y/n): ")
if entry == "y":
os.system("rm -rf /tmp/git-short-url/")
else:
exit()
try:
repo = Repo.clone_from(repo_url, "/tmp/git-short-url/")
except:
print(Fore.RED + "Error cloning repo" + Style.RESET_ALL)
exit()
try:
os.makedirs("/tmp/git-short-url/u/" + display_name)
except:
print(Fore.RED + "Error creating directory" + Style.RESET_ALL)
exit()
try:
with open("/tmp/git-short-url/u/" + display_name + "/index.html", "w") as file:
template = urllib.request.urlopen('https://raw.githubusercontent.com/sailingteam4/sailingteam4.github.io/main/u/template').read().decode('utf-8')
template = template.replace("url_to_go", url)
template = template.replace("Displayname", display_name)
file.write(template)
except:
print(Fore.RED + "Error writing to file" + Style.RESET_ALL)
exit()
try:
repo.index.add(["u/" + display_name + "/index.html"])
repo.index.commit("Link shortner " + display_name)
repo.remotes.origin.push()
except:
print(Fore.RED + "Error pushing to repo" + Style.RESET_ALL)
exit()
os.system("rm -rf /tmp/git-short-url/")
print(Fore.GREEN + "URL shortened successfully !" + Style.RESET_ALL)
exit()
if len(args) == 2:
if args[1] == "-d":
print("Usage: urlshort -d <display_name> to delete a short url")
exit()
elif (args[1] == "-h" or args[1] == "--help"):
print("Usage: urlshort <url_to_short> <display_name>\nOr urlshort -d <display_name> to delete a short url")
exit()
elif (args[1] == "-l"):
if (os.path.exists("/tmp/git-short-url/")):
print(Fore.RED + "Repo already exists" + Style.RESET_ALL)
entry = input("Do you want to remove the repo? (y/n): ")
if entry == "y":
os.system("rm -rf /tmp/git-short-url/")
else:
exit()
try:
repo = Repo.clone_from(repo_url, "/tmp/git-short-url/")
except:
print(Fore.RED + "Error cloning repo" + Style.RESET_ALL)
exit()
try:
listfile = fast_scandir("/tmp/git-short-url/u/")
except:
print(Fore.RED + "Error listing files" + Style.RESET_ALL)
exit()
print("Shortened URLs display name:")
for i in listfile:
print(i.split("/")[4])
os.system("rm -rf /tmp/git-short-url/")
exit()
else:
print("Usage: urlshort <url_to_short> <display_name>\nOr urlshort -d <display_name> to delete a short url")