aboutsummarylogtreecommitdiffstats
path: root/fetcher.sh
blob: cdd03fb01e01b2bc6e770cdd8782fc602327e2ba (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/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 -u

#================================== Logging ==================================
script_name="$(basename "${BASH_SOURCE[0]}")"
configfile="$HOME/.config/fetcher.conf"

# 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="info") >&3)
# Use tee to redirect fd 2 to logfile.err and to stderr
exec 2> >(tee >(systemd-cat --identifier="$script_name" --priority="notice") >&4)

#================================= Functions =================================
function help_menu() {
    echo "$script_name Options:"
    echo -h, --help     Show this information
    echo -f=, --file=   Use another input file for fetcher
    echo
    echo "Create your fetcher config file $configfile like this:"
    echo "$HOME/workspace/repo pull --ff-only"
    echo "$HOME/workspace/repo2 pull origin master:master"
    echo "$HOME/workspace/repo2 push"
    echo
}

#============================== Parsing Options ==============================

for i in "$@"
do
    case $i in
        -f=*|--file=*)
            configfile="${i#*=}"
            ;;
        -h|--help)
            help_menu
            exit
            ;;
        *)
            # unknown option
            ;;
    esac
done

if [ ! -f "$configfile" ]; then
    echo "Configuration file \"$configfile\" not found." >&2
    echo
    help_menu
    exit
fi

#================================ Parse lines ================================
readarray lines < "$configfile"

if [ ${#lines[@]} -eq 0 ]; then
    echo "Configuration file \"$configfile\" empty." >&2
    echo
    help_menu
    exit
fi

for i in "${lines[@]}"; do
    IFS=' ' read -r -a line <<< "$i"
    path="${line[0]}"
    args=$(IFS=' '; echo "${line[@]:1}")

    cmd="git -C $path $args"
    echo "Running: $cmd"
    eval "$cmd" || true
    echo
done