Skip to content

Commit

Permalink
treehouses sshkey github adduser multiple users (fixes #2233) (#2231)
Browse files Browse the repository at this point in the history
Co-authored-by: dogi <[email protected]>
  • Loading branch information
JLKwong and dogi authored Jun 5, 2021
1 parent 8593f87 commit 31d53d3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
23 changes: 16 additions & 7 deletions modules/sshkey.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
function sshkey () {
local keys githubusername auth_files teams team_id members
checkroot
checkargn $# 5
if [ "$1" == "add" ]; then
checkargn $# 4
shift
temp_file=$(mktemp)
echo "$@" >> $temp_file
Expand All @@ -26,13 +26,15 @@ function sshkey () {
log_and_exit1 "ERROR: invalid public key"
fi
elif [ "$1" == "list" ]; then
checkargn $# 1
echo "==== root keys ===="
cat /root/.ssh/authorized_keys
if [ "$(detectrpi)" != "nonrpi" ]; then
echo "==== pi keys ===="
cat /home/pi/.ssh/authorized_keys
fi
elif [ "$1" == "delete" ]; then
checkargn $# 2
if [ -z "$2" ]; then
echo "Error: missing argument"
log_and_exit1 "Usage: $BASENAME sshkey delete \"<key>\""
Expand All @@ -53,6 +55,7 @@ function sshkey () {
fi
fi
elif [ "$1" == "deleteall" ]; then
checkargn $# 1
rm /root/.ssh/authorized_keys
if [ "$(detectrpi)" != "nonrpi" ]; then
rm /home/pi/.ssh/authorized_keys
Expand All @@ -68,11 +71,16 @@ function sshkey () {
echo "Error: missing argument"
log_and_exit1 "Usage: $BASENAME sshkey adduser <username>"
fi
keys=$(curl -s "https://github.com/$3.keys")
if [ ! -z "$keys" ]; then
keys=$(sed 's#$# '$3'#' <<< $keys)
sshkey add "$keys"
fi
shift; shift
for user in "$@"; do
echo " Attempting to add the following user: $user"
keys=$(curl -s "https://github.com/$user.keys")
if [ ! -z "$keys" ]; then
keys=$(sed 's#$# '$user'#' <<< $keys)
sshkey add "$keys"
fi
echo " Successfully added user: $user"
done
elif [ "$2" == "deleteuser" ]; then
if [ -z "$3" ]; then
echo "Error: missing argument"
Expand All @@ -84,7 +92,7 @@ function sshkey () {
if [ -f "$file" ]; then
if grep -q " $githubusername$" $file; then
sed -i "/ $githubusername$/d" $file
echo "$githubusername's key(s) deleted from $file"
echo "$githubusername's key(s) deleted from $file"
else
echo "$githubusername does not exist"
fi
Expand All @@ -93,6 +101,7 @@ function sshkey () {
fi
done
elif [ "$2" == "addteam" ]; then
checkargn $# 5
if [ -z "$3" ] || [ -z "$4" ] || [ -z "$5" ]; then
echo "Error: missing arguments"
log_and_exit1 "Usage: $BASENAME sshkey github addteam <organization> <team_name> <access_token>"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@treehouses/cli",
"version": "1.25.59",
"version": "1.25.60",
"remote": "4000",
"description": "Thin command-line interface for Raspberry Pi low level configuration.",
"main": "cli.sh",
Expand Down

0 comments on commit 31d53d3

Please sign in to comment.