aboutsummarylogtreecommitdiffstats
path: root/fetcher.sh
blob: a334906de1351d79662106bf7c109ee5cadf0c45 (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
        addcommitpush)
            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 '=============================================================================='