-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgh-clone.sh
executable file
·106 lines (94 loc) · 3.14 KB
/
gh-clone.sh
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
#!/bin/bash
visibility="all"
user=""
limit=100
into=""
while [[ "$#" -gt 0 ]]; do
case $1 in
--visibility)
visibility="$2"
shift 2
;;
--user)
user="$2"
shift 2
;;
--limit)
limit="$2"
shift 2
;;
--into)
into="$2"
shift 2
;;
*)
printf "\e[31mUnknown option: $1\e[0m\n"
exit 1
;;
esac
done
if [[ -z "$user" ]]; then
printf "\e[31mError: The --user option is required.\e[0m\n"
exit 1
fi
if ! command -v gh &> /dev/null; then
printf "\e[31mError: 'gh' (GitHub CLI) is not installed. Please install it first.\e[0m\n"
exit 1
fi
if ! command -v git &> /dev/null; then
printf "\e[31mError: 'git' is not installed. Please install it first.\e[0m\n"
exit 1
fi
if [[ ! "$limit" =~ ^[1-9][0-9]*$ ]]; then
printf "\e[31mError: --limit must be 1 or greater.\e[0m\n"
exit 1
fi
if [[ "$visibility" == "private" || "$visibility" == "all" ]]; then
if ! gh auth status | grep -q "Logged in to .* account $user "; then
printf "\e[31mError: You are not logged in as the specified user: $user. Set --visibility to 'public' or use 'gh auth login'.\e[0m\n"
exit 1
fi
fi
if [[ "$visibility" == "private" || "$visibility" == "public" ]]; then
if [[ "$(gh repo list "$user" --limit 1 --visibility "$visibility" 2>/dev/null | wc -l)" -eq 0 ]]; then
printf "\e[31mError: No $visibility repositories found for '$user'.\e[0m\n"
exit 1
fi
else
if [[ "$(gh repo list "$user" --limit 1 2>/dev/null | wc -l)" -eq 0 ]]; then
printf "\e[31mError: No repositories found for '$user'.\e[0m\n"
exit 1
fi
fi
if [[ "$visibility" != "all" && "$visibility" != "public" && "$visibility" != "private" ]]; then
printf "\e[31mError: Invalid visibility option. Use 'all', 'public', or 'private'.\e[0m\n"
exit 1
fi
if [[ -z "$into" ]]; then
into="$user"
fi
if [[ -d "$into" && ! -w "$into" ]] || ! mkdir -p "$into"; then
printf "\e[31mError: Unable to create or access the specified directory '$into'.\e[0m\n"
exit 1
fi
if [[ "$visibility" == "public" || "$visibility" == "private" ]]; then
while read -r repo _; do
repo_name=$(basename "$repo")
if [[ -d "$into/$repo_name" ]]; then
printf "\e[33m$repo_name already exists in $(realpath "$into"). Skipping...\e[0m\n"
else
gh repo clone "$repo" "$into/$repo_name" > /dev/null 2>&1
printf "\e[32mCloned $repo_name into $(realpath "$into/$repo_name")\e[0m\n"
fi
done < <(gh repo list "$user" --visibility "$visibility" --limit "$limit")
else
while read -r repo _; do
repo_name=$(basename "$repo")
if [[ -d "$into/$repo_name" ]]; then
printf "\e[33m$repo_name already exists in $(realpath "$into"). Skipping...\e[0m\n"
else
gh repo clone "$repo" "$into/$repo_name" > /dev/null 2>&1
printf "\e[32mCloned $repo_name into $(realpath "$into/$repo_name")\e[0m\n"
fi
done < <(gh repo list "$user" --limit "$limit")
fi