captaincore/app/key/add
2019-09-11 20:23:38 -04:00

44 lines
No EOL
1,017 B
Bash

#!/usr/bin/env bash
#
# Add a SSH Key to CaptainCore CLI.
#
# `captaincore keys add <ssh-key>`
#
# --id=<id>
#
# Load configuration
root_path="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"; root_path=${root_path%app*}
source ${root_path}lib/arguments
run_command() {
read -r -d '' php_code << heredoc
\$arguments = <<<PHPHEREDOC
$1
PHPHEREDOC;
echo base64_decode( \$arguments );
heredoc
ssh_key=$( php -r "$php_code" )
mkdir -p "$path_keys/${captain_id}/"
echo -n "$ssh_key" > "$path_keys/${captain_id}/${id}"
chmod 600 "$path_keys/${captain_id}/${id}"
# Fetch and return fingerprint of newly added key
ssh-keygen -E md5 -lf "$path_keys/${captain_id}/${id}" | perl -n -e '/.+?:([^ ]+)/&& print $1'
}
# See if any sites are specifed
if [ ${#arguments[*]} -eq 1 ]; then
# Runs on specifed sites
run_command ${arguments[*]}
fi
# Error if no sites specifed
if [ ${#arguments[*]} -eq 0 ]; then
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Please specify a SSH key."
fi