From 4658e3484cde42d7408cc58a29209ca06e03330b Mon Sep 17 00:00:00 2001 From: 3wc <3wc.git@doesthisthing.work> Date: Mon, 14 Sep 2020 19:16:34 +0200 Subject: [PATCH] Add `abra cp` command to push & pull files --- abra | 34 ++++++++++++++++++++++++++++++++-- 1 file changed, 32 insertions(+), 2 deletions(-) diff --git a/abra b/abra index 706ee04..250193d 100755 --- a/abra +++ b/abra @@ -52,11 +52,12 @@ sub_help() { echo "Usage: $PROGRAM_NAME [-a STACK_NAME] [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")