-
Notifications
You must be signed in to change notification settings - Fork 1
/
clone
executable file
·107 lines (86 loc) · 2.72 KB
/
clone
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
#!/usr/bin/env python3
"""
Usage: clone [-h] [-v] [-d] [-p PROTOCOL] repo_name [dest]
Clone GitHub repos really fast!
positional arguments:
repo_name Name of repository to clone.
dest Destination to clone repository.
options:
-h, --help show this help message and exit
-v, --version show program's version number and exit
-d, --dry-run Don't actually clone, just echo the remote url of the repo.
-p PROTOCOL, --protocol PROTOCOL
Use a different protocol to clone. Options: git, http, ssh
"""
import argparse
import subprocess
import sys
from pathlib import Path
from typing import Optional
DOTFILES_DIR = Path(Path.home(), ".dotfiles", "zfunc")
sys.path.insert(0, str(DOTFILES_DIR))
from gitUtils import get_git_username
VERSION = "0.1.0"
USERNAME = get_git_username()
def clone(url: str, dest: Optional[Path] = None) -> None:
"""Clone a git repository by calling git as a subprocess."""
if dest is None:
subprocess.run(["git", "clone", url])
else:
subprocess.run(["git", "clone", url, dest])
def git_url(stub: str) -> str:
return f"git://github.com/{stub}"
def http_url(stub: str) -> str:
return f"https://github.com/{stub}"
def ssh_url(stub: str) -> str:
return f"git@github.com:{stub}"
if __name__ == "__main__":
protocols = {"git": git_url, "http": http_url, "ssh": ssh_url}
parser = argparse.ArgumentParser(
prog="clone",
description="Clone GitHub repos really fast!",
)
parser.add_argument(
"-v",
"--version",
action="version",
version=f"%(prog)s {VERSION}",
)
parser.add_argument(
"-d",
"--dry-run",
action="store_true",
help="Don't actually clone, just echo the remote url of the repo.",
)
parser.add_argument(
"-p",
"--protocol",
action="store",
default="ssh",
help="Use a different protocol to clone. Options: git, http, ssh",
)
parser.add_argument(
"repo_name",
action="store",
help="Name of repository to clone.",
)
parser.add_argument(
"dest",
action="store",
type=Path,
nargs="?",
help="Destination to clone repository.",
)
args = vars(parser.parse_args())
if args["protocol"].lower() not in protocols.keys():
raise ValueError(
f"Protocol '{args['protocol']}' not supported, try one of these {', '.join(protocols.keys())}."
)
name = args["repo_name"]
if "/" not in name:
name = f"{USERNAME}/{name}"
url = protocols[args["protocol"]](name)
if args["dry_run"]:
print(url)
else:
clone(url, args["dest"])