aboutsummarylogtreecommitdiffstats
path: root/fetcher.sh
diff options
context:
space:
mode:
authorManoel Brunnen2016-09-06 00:07:05 +0200
committerManoel Brunnen2016-09-06 00:07:05 +0200
commitf8c7e6f8a5c5ed674a74747efbb522053f583987 (patch)
treeee07810b2d65a5d65d14cf7be5699035c5f17965 /fetcher.sh
downloadaur-f8c7e6f8a5c5ed674a74747efbb522053f583987.tar.gz
Initial Commit
Diffstat (limited to 'fetcher.sh')
-rwxr-xr-xfetcher.sh134
1 files changed, 134 insertions, 0 deletions
diff --git a/fetcher.sh b/fetcher.sh
new file mode 100755
index 000000000000..25626dac53e8
--- /dev/null
+++ b/fetcher.sh
@@ -0,0 +1,134 @@
+#!/bin/bash
+#===============================================================================
+# AUTHOR: Manoel Brunnen, manoel.brunnen@gmail.com
+# CREATED: 21.07.2016
+# LICENSE: MIT
+# FILE: fetcher.sh
+# USAGE: ./fetcher.sh
+#
+# DESCRIPTION: update some git repos
+#
+# No warranty! For nothing. Use it at your own risk.
+#===============================================================================
+set -e
+set -o nounset # Treat unset variables as an error
+
+#================================== Logging ==================================
+script_name=`basename "${BASH_SOURCE[0]}"`
+
+# Backup stdout(&1) and stderr(&2) to fd 3 and fd 4
+exec 3>&1 4>&2
+# Restore stdout and stderr
+trap 'exec 2>&4 1>&3' 0 1 2 3
+# Use tee to redirect fd 1 to logfile.out and to stdout
+exec 1> >(tee >(systemd-cat --identifier="$script_name" --priority="debug") >&3)
+# Use tee to redirect fd 2 to logfile.err and to stderr
+exec 2> >(tee >(systemd-cat --identifier="$script_name" --priority="warning") >&4)
+
+#================================= Functions =================================
+function config_usage() {
+ echo "Create your fetcher config file $configfile like this:"
+ echo $HOME/workspace/repo pull
+ echo $HOME/.vim pull
+ echo $HOME/workspace/project1 pull origin master:master
+ echo $HOME/workspace/project2 alias
+}
+
+#============================== Parsing Options ==============================
+dryrun=false
+configfile="$HOME/.config/fetcher.conf"
+
+for i in "$@"
+do
+case $i in
+ -n|--dryrun)
+ dryrun=true
+ ;;
+ -f=*|--file=*)
+ configfile="${i#*=}"
+ ;;
+ -h|--help)
+ echo $script_name' Options:'
+ echo -h, --help Show this information
+ echo -n, --dryrun Do a dry run
+ echo -f=, --file= Use another input file for fetcher
+ echo
+ config_usage
+ exit
+ ;;
+ *)
+ # unknown option
+ ;;
+esac
+done
+
+if [ ! -f $configfile ]; then
+ echo "Configuration file \"$configfile\" not found." >&2
+ config_usage
+ exit
+fi
+
+#================================ Parse lines ================================
+readarray -t lines < $configfile
+
+if [ ${#lines[@]} -eq 0 ]; then
+ config_usage
+ exit
+fi
+
+for ((i=0; i < ${#lines[@]}; i++)); do
+ line="${lines[$i]}"
+
+ IFS=' ' read -r -a line <<< "$line"
+ eval path=${line[0]}
+ eval gitcmd=${line[1]}
+ remote=''
+ branch=''
+
+ if [ ${#line[@]} -gt 2 ]; then
+ eval remote=${line[2]}
+ fi
+
+ if [ ${#line[@]} -gt 3 ]; then
+ eval branch=${line[3]}
+ fi
+
+ case $gitcmd in
+ push)
+ if $dryrun; then
+ cmd="git -C $path add -n -A"
+ cmd="$cmd && (git -C $path commit -n -v || true)"
+ cmd="$cmd && git -C $path push -n $remote $branch"
+ else
+ cmd="git -C $path add -A"
+ cmd="$cmd && (git -C $path commit -v || true)"
+ cmd="$cmd && git -C $path push $remote $branch"
+ fi
+ ;;
+ pull)
+ if $dryrun; then
+ cmd="git -C $path pull -n $remote $branch"
+ else
+ cmd="git -C $path pull $remote $branch"
+ fi
+ ;;
+ *)
+ if $dryrun; then
+ cmd="echo 'git -C $path $gitcmd $remote $branch'"
+ else
+ cmd="git -C $path $gitcmd $remote $branch"
+ fi
+ ;;
+ esac
+
+ echo '=============================================================================='
+ echo "$script_name runs:"
+ echo "$cmd"
+ eval "$cmd"
+ echo
+done
+
+echo '=============================================================================='
+echo "fetcher processed successfully $configfile."
+echo '=============================================================================='
+