-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi-refresh
More file actions
executable file
·108 lines (84 loc) · 2.36 KB
/
api-refresh
File metadata and controls
executable file
·108 lines (84 loc) · 2.36 KB
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
#!/bin/bash
source app_location.cfg
function show_help() {
cat << EOF
Usage: api-refresh [OPTION...]
-d Directory to be used (optional)
- if not passed current directory will be used.
- must be a existing git directory
- pass "." to use current directory
-b Branch to be used (optional)
- if not passed current branch will be used.
- if passed branch does not exist current branch will be used.
-? Prints this help
EOF
}
function get_branch() {
BRANCH_TO_REFRESH="$1"
if [[ -z "$BRANCH_TO_REFRESH" ]]; then
GIT_BRANCH=$(git branch | grep "*")
BRANCH=${GIT_BRANCH:2}
else
BRANCH="$BRANCH_TO_REFRESH"
if [[ -z $(git branch | grep "$BRANCH") ]]; then
GIT_BRANCH=$(git branch | grep "*")
CURRENT_BRANCH=${GIT_BRANCH:2}
echo "$BRANCH does not exist. Branch set to $CURRENT_BRANCH"
BRANCH="$CURRENT_BRANCH"
fi
fi
}
function refresh() {
API_DIR="$1"
BRANCH_TO_REFRESH="$2"
if [[ -d "$API_DIR" ]]; then
cd "$API_DIR"
echo "Working in $API_DIR ..."
BRANCH=''
ERROR_MESSAGE=''
get_branch $BRANCH_TO_REFRESH
echo "Fetching ..."
git fetch --all --prune
CHECKOUT_MESSAGE=$(git checkout "$BRANCH" 2>&1 > /dev/null)
if [[ $? == 1 ]]; then
echo "An error occurred when tried to checkout $BRANCH:"
echo "$CHECKOUT_MESSAGE"
exit 1
fi
echo "Pulling $BRANCH ..."
git pull &> /dev/null
echo -n "Installing dependencies "
while true; do echo -n "."; sleep 1; done &
trap 'kill $!' SIGTERM SIGKILL
composer install --no-interaction &> /dev/null
kill $!
echo -e "\nDone."
exit 0
else
echo "$API_DIR is not a directory."
exit 1
fi
}
DIR_TO_REFRESH=""
BRANCH_TO_REFRESH=""
while getopts "d:b:h?" opt; do
case "$opt" in
d) DIR_TO_REFRESH=$OPTARG
;;
b) BRANCH_TO_REFRESH=$OPTARG
;;
h|\?)
show_help
exit 0
;;
esac
done
shift $((OPTIND -1))
if [[ -z "$DIR_TO_REFRESH" ]]; then
refresh "$PWD" $BRANCH_TO_REFRESH
elif [[ "$DIR_TO_REFRESH" == "." ]]; then
refresh "$PWD" $BRANCH_TO_REFRESH
else
refresh $DIR_TO_REFRESH $BRANCH_TO_REFRESH
fi
exit 0