generated from 2KAbhishek/bare-minimum
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathghpm.sh
executable file
·122 lines (98 loc) · 2.98 KB
/
ghpm.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
#!/usr/bin/env bash
echo -e "\u001b[32;1m ghpm - GitHub Project Manager \u001b[0m"
echo
project_dir=$1
if [ -z "$project_dir" ] || [[ "." == "$project_dir" ]] || [ ! -d "$project_dir" ]; then
project_dir=$PWD
fi
cd "$project_dir" || exit
function clone_self_repos {
gh repo list --json sshUrl -q ".[].sshUrl" -L 500 | xargs -L1 gh repo clone
}
function get_username {
if [ -z "$username" ]; then
echo -e " \u001b[37;1m\u001b[4m Enter GitHub Username:\u001b[0m"
echo -en "\u001b[32;1m ==> \u001b[0m"
read -r username
fi
}
function get_page_count {
page_count=0
public_repos=$(curl -s "https://api.github.com/users/$username" | jq -r ."public_repos")
echo -e "\u001b[32;1m\u001b[4m\nPublic Repository Count: $public_repos\u001b[0m"
((page_count = public_repos / 100 + 1))
}
function clone_public_repos {
get_username
get_page_count
echo -e "\u001b[7m\n Cloning public repos of $username@github \u001b[0m"
for ((i = 1; i <= page_count; i++)); do
curl -s "https://api.github.com/users/$username/repos?page=$i&per_page=100" |
jq -r ".[].html_url" | grep -i "$username" | xargs -L1 git clone
done
}
function success {
echo -e "\n\033[32m Complete! \033[0m\n"
}
function repo_operation {
for i in $(find . -maxdepth 2 -name ".git" | cut -c 3-); do
cd "$i" || return
cd ..
repo=$(pwd | awk -F/ '{print $NF}')
case "$1" in
"ssh-remote")
echo -e "\u001b[7m\n Setting ssh remote for $repo \u001b[0m"
get_username
git remote set-url origin git@github.com:"${username}/${repo}.git"
;;
"custom")
echo -e "\u001b[7m\n Running $2 in $repo \u001b[0m"
$2
;;
esac
cd "$project_dir" || return
done
}
while true; do
echo -e " \u001b[37;1m\u001b[4m Select an option:\u001b[0m"
echo -e " \u001b[34;1m (1) Clone own repos (requires GitHub CLI) \u001b[0m"
echo -e " \u001b[34;1m (2) Clone public repos of others \u001b[0m"
echo -e " \u001b[34;1m (3) Run command in all repos \u001b[0m"
echo -e " \u001b[34;1m (4) Set SSH remote \u001b[0m"
echo -e " \u001b[31;1m (0) Exit \u001b[0m"
echo -en "\u001b[32;1m ==> \u001b[0m"
read -r option
case $option in
"1")
clone_self_repos
success
read -r
;;
"2")
clone_public_repos
success
read -r
;;
"3")
echo -e " \u001b[37;1m\u001b[4m Enter command to run (e.g: git pull) \u001b[0m"
echo -en "\u001b[32;1m ==> \u001b[0m"
read -r custom_command
repo_operation "custom" "$custom_command"
success
read -r
;;
"4")
repo_operation "ssh-remote"
success
read -r
;;
"0")
echo -e "\u001b[32;1m Bye! \u001b[0m"
exit 0
;;
*)
echo -e "\u001b[31;1m Invalid option entered! \u001b[0m"
exit 1
;;
esac
done