Add `abra cp` command to push & pull files

This commit is contained in:
3wc 2020-09-14 19:16:34 +02:00
parent f0a17bfd87
commit 4658e3484c
1 changed files with 32 additions and 2 deletions

34
abra
View File

@ -52,11 +52,12 @@ sub_help() {
echo "Usage: $PROGRAM_NAME [-a STACK_NAME] <subcommand> [options]"
echo ""
echo "Subcommands:"
echo " cp SRC_PATH SERVICE:DEST_PATH copy files to a container"
echo " deploy [COMPOSE_FILE] let 'em rip"
echo " logs SERVICE [ARGS] tail logs from a deployed service"
echo " run SERVICE CMD run a command in the specified service's container"
echo " run_args SERVICE ARGS CMD run, passing extra args to docker exec"
echo " secret_generate SECRET VERSION [CMD] generate a secret, store it in pass & as a Docker secret"
echo " deploy [COMPOSE_FILE] let 'em rip"
echo " logs SERVICE [ARGS] tail logs from a deployed service"
echo " ... (custom commands)"
echo ""
echo "Make sure \$STACK_NAME is set using direnv or -a"
@ -159,6 +160,35 @@ sub_logs (){
docker service logs "${STACK_NAME}_${SERVICE}" $LOGS_ARGS
}
sub_cp() {
SOURCE=$1
DEST=$2
SERVICE=$(echo "$SOURCE" | grep -o '^[^:]\+:' || echo "$DEST" | grep -o '^[^:]\+:')
SERVICE=$(echo $SERVICE | tr -d ':')
if [ -z $SERVICE ]; then
echo "$(tput setaf 1)ERROR: Can't find SERVICE in either SRC or DEST$(tput sgr0)"
echo ""
echo "Usage: $PROGRAM_NAME cp SERVICE:SRC_PATH DEST_PATH"
echo " $PROGRAM_NAME cp SRC_PATH SERVICE:DEST_PATH"
exit
fi
CONTAINER=$(docker container ls --format "table {{.ID}},{{.Names}}" \
| grep "${STACK_NAME}_${SERVICE}" | cut -d',' -f1)
if [ -z $CONTAINER ]; then
echo "$(tput setaf 1)ERROR: Can't find a ${STACK_NAME}_${SERVICE}$(tput sgr0)"
exit
fi
CP_ARGS=$(echo "$SOURCE $DEST" | sed "s/$SERVICE/$CONTAINER/")
# shellcheck disable=SC2086
docker cp $CP_ARGS
}
subcommand=$1
case $subcommand in
"" | "-h" | "--help")