#!/bin/zsh RETVAL=0 # CONF defaultLocalPath='/path/to/local/dir/' defaultRemotePath='/path/to/remote/dir/' defaultHost='devac' NONE='\033[0m' # none GREY='\033[2m' # grey BOLD='\033[1m' # bold GREEN='\033[0;32m' # green YELLOW='\033[0;33m' # yellow RED='\033[0;31m' # red BLUE='\033[0;34m' # blue CYAN='\033[0;36m' # cyan MAGENTA='\033[0;35m' # magenta BLACK='\033[0;30m' # black WHITE='\033[0;37m' # white BBLUE='\033[1;94m' # blue bold POSITIONAL=() clear echo -e "${NONE}${BBLUE}🔎 Lancement du script $0 ${NONE}${GREY}" echo -e "${NONE}${BLUE}-------------------------------${NONE}${GREY}" Usage() { echo -e "${BOLD}NAME${NONE}" echo " scpsync - Synchronize local folder to a remote host" echo " " echo -e "${BOLD}SYNOPSIS${NONE}" echo " sh $0 [OPTION]..." echo " " echo -e "${BOLD}DESCRIPTION${NONE}" echo " -h, --help" echo " " echo " print this help" echo " " echo " -d, --dest" echo " " echo " remote directory absolut path" echo " " echo " -s, --source" echo " " echo " source directory absolut path" echo " " echo " -r, --remote-host" echo " remote ssh host" echo " " echo -e "${RED} 🐞${NONE} Usage: $0 -s /path/to/local/dir/ -d /path/to/remote/dir/ -r sshhost" } while [[ $# -gt 0 ]] do key="$1" case $key in -h|--help) Usage exit 0 ;; -d|--dest) dirRemotePath=$2 shift # past argument shift # past value ;; -s|--source) dirLocalPath=$2 shift shift ;; -r|--remote-host) host=$2 shift shift ;; *) # unknown option POSITIONAL+=("$1") # save it in an array for later shift ;; esac done set -- "${POSITIONAL[@]}" if test -z "$dirLocalPath"; then dirLocalPath=$defaultLocalPath echo -e "${YELLOW}⚠ Local path:${NONE} ${dirLocalPath}${YELLOW} (default)${NONE}" else echo -e "${GREEN}⚙️ Local path:${NONE} ${dirLocalPath}" fi if test -z "$dirRemotePath"; then dirRemotePath=$defaultRemotePath echo -e "${YELLOW}⚠ Remote path:${NONE} ${dirRemotePath}${YELLOW} (default)${NONE}" else echo -e "${GREEN}⚙️ Remote path:${NONE} ${dirRemotePath}" fi if [ -z "$host" ]; then host=$defaultHost echo -e "${YELLOW}⚠ Remote host:${NONE} ${host}${YELLOW} (default)${NONE}" else echo -e "${GREEN}⚙️ Remote host:${NONE} ${host}" fi if [ $RETVAL -eq 0 ]; then trap exit INT function exit() { echo -e "" echo -e "${NONE}${CYAN}♻️ Réinitialisation du répo distant...${NONE}${GREY}" echo -e "${NONE}\$> ${MAGENTA}ssh ${host} \"cd ${dirRemotePath}; git reset --hard\"${NONE}" ssh $host "cd $dirRemotePath; git reset --hard" echo -e "\n${NONE}${CYAN}🗑️ Suppression du cache${NONE}${GREY}" echo -e "${NONE}\$> ${MAGENTA}ssh ${host} \"cd ${dirRemotePath}; composer front-cache-clear\"${NONE}" ssh $host "cd $dirRemotePath; composer front-cache-clear" echo -e "${NONE}${GREEN}✔️ Ciao !${NONE}" } syncfile () { fileLocalPath=$1 fileRemotePath=${fileLocalPath/$dirLocalPath/$dirRemotePath} echo -e "\n${NONE}${CYAN}🔄 Synchronisation du fichier ${YELLOW}${fileLocalPath/$dirLocalPath/}${NONE}" echo -e "${NONE}\$> ${MAGENTA}scp ${fileLocalPath} ${host}:${fileRemotePath}${NONE}" scp ${fileLocalPath} $host:$fileRemotePath } echo -e "\n${NONE}${BBLUE}⏳ En attente de modification${NONE}${GREY}" echo -e "${NONE}${BLUE}-----------------------------${NONE}${GREY}" num=0 while read fileLocalPath; do if [ "$fileLocalPath" = "NoOp" ]; then num=0 echo -e "\n${NONE}${CYAN}🗑️ Suppression du cache${NONE}${GREY}" echo -e "${NONE}\$> ${MAGENTA}ssh ${host} \"cd ${dirRemotePath}; composer front-cache-clear\"${NONE}\n" ssh $host "cd $dirRemotePath; composer front-cache-clear" echo -e "\n${NONE}${BBLUE}⏳ En attente de modification${NONE}${GREY}" echo -e "${NONE}${BLUE}-----------------------------${NONE}${GREY}" else num=$((num+1)) #echo -e "${NONE}${num} - ${fileLocalPath}" syncfile $fileLocalPath $num & fi done < <(fswatch --batch-marker -0 --exclude '.git/' $dirLocalPath | xargs -0 -n 1 -I {}) else echo -e "${RED}⚠ ${NONE} Le script a rencontré une erreur" echo "-----------------------------" echo -e "${RED} 💀 Exit code:${NONE} $RETVAL" exit $RETVAL fi