-
Notifications
You must be signed in to change notification settings - Fork 0
/
GitHubRepoList.sh
executable file
·85 lines (76 loc) · 2.21 KB
/
GitHubRepoList.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
#!/usr/bin/env bash
#
# Create a CSV report of all repositories
#
# PreReqs:
# github cli, jq, sort installed
# GH_TOKEN set in env or login via gh cli
#
# TODO(kernelsam): Read token from lastpass
#
############################################################
# help
############################################################
help()
{
# Display Help
echo
echo "Syntax: GitHubRepoList.sh [-h|t]"
echo "options:"
echo "f Fields to search. Comma separated list. Ex. \"name,description,updatedAt"\"
echo "h Print this Help."
echo "l Maximum number of repositories to list"
echo "o github org"
echo
}
############################################################
# read args
############################################################
while [ -n "$1" ]; do
case "$1" in
--fields|-f)
shift
fields="${1:-name}"
;;
--help|-h)
shift
echo "You entered number as: $1"
;;
--limit|-l)
shift
limit="${1:-500}"
;;
--org|-o)
shift
org=$1
;;
*)
help
exit 1
;;
esac
shift
done
if [ ! -n "$org" ]; then
echo "[ERROR] Org was not provided"
exit 1
fi
############################################################
# main
# list gh repos to file and sort
############################################################
# update fields to be passed to jq in the format
# .<value1>,.<value2>,...,.<valuen>
if grep -q "," <<< "${fields}"; then
jqfields=$(echo "${fields}" | awk -F "," '{ for(i=1; i<NF; i++) printf ".%s,", $i }')
jqfields=$jqfields$(echo ".${fields}" | awk -F, '{ print "."$NF }')
else
# single field should have no trailing comma
jqfields=".${fields}"
fi
# list the repos to a file
echo "[INFO] gh repo list ${org} -L ${limit} --json ${fields} --jq .[]| [${jqfields}] | tr -d '[]' > repolist.csv"
gh repo list "${org}" -L "${limit}" --json "${fields}" --jq " .[]| [${jqfields}]" | tr -d '[]' > repolist.csv
# sort the file in place and add a header
sort -k 1 -o repolist.csv{,}
echo -e "${fields}\n$(cat repolist.csv)" > repolist.csv