-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadd-keys.sh
More file actions
executable file
·94 lines (93 loc) · 1.95 KB
/
add-keys.sh
File metadata and controls
executable file
·94 lines (93 loc) · 1.95 KB
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
#!/bin/sh
usage() {
printf "Usage: ./add-keys.sh [-v] USERNAME\n"
exit 1
}
SUDO=""
PM=""
PMSHORT=""
SILENT=true
TODEVNULL="> /dev/null"
# Get options
while getopts v opt; do
case $opt in
v)
SILENT=false
TODEVNULL=""
;;
\?)
usage
esac
done
USER=${@:$OPTIND:1}
if [ -z $USER ]; then
printf "Username missing\n"
usage
fi
# Check for root
if [ $(whoami) != root ]; then
SUDO="sudo"
fi
# Check for package manager
if command -v pacman > /dev/null; then
if ! $SILENT; then
printf "Using pacman\n"
fi
PM="$SUDO pacman -S"
PMSHORT="pacman"
elif command -v brew > /dev/null; then
if ! $SILENT; then
printf "Using brew\n"
fi
PM="brew install"
PMSHORT="brew"
elif command -v apt-get > /dev/null; then
if ! $SILENT; then
printf "Using apt\n"
fi
PM="$SUDO apt-get install"
PMSHORT="apt"
elif command -v yum > /dev/null; then
if ! $SILENT; then
printf "Using yum\n"
fi
PM="$SUDO yum install"
PMSHORT="yum"
else
if ! $SILENT; then
printf "No package manager found; please install the following packages yourself:\ncurl\njq\nbase64\n"
fi
fi
# Check for curl
if ! command -v curl > /dev/null; then
if ! $SILENT; then
printf "No curl found, trying to install\n"
fi
$PM curl $TODEVNULL
fi
# Get keys from GitHub
KEYS=$(curl -s -L https://api.github.com/users/$USER/keys)
# Check for jq
if ! command -v jq > /dev/null; then
if ! $SILENT; then
printf "No jq found, trying to install\n"
fi
$PM jq $TODEVNULL
fi
# Check for base64
if ! command -v base64 > /dev/null; then
if ! $SILENT; then
printf "No base64 found, trying to install\n"
fi
if $PMSHORT == "pacman" || $PMSHORT == "yum"; then
$PM libb64 $TODEVNULL
elif $PMSHORT == "brew"; then
$PM base64 $TODEVNULL
elif $PMSHORT == "apt"; then
$PM libb64-0d $TODEVNULL
fi
fi
# Transform JSON to appendable keys
for ROW in $(printf "$KEYS" | jq -r ".[] | @base64"); do
printf "$ROW" | base64 --decode | jq -r ".key"
done