forked from me-box/databox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabox-pin
executable file
·103 lines (81 loc) · 2.03 KB
/
databox-pin
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
#!/bin/bash
fail() {
echo -e "[ERROR] ${1}"
exit 1
}
assert() {
if [ "$1" != "$2" ]
then
fail "$3" "$1"
fi
}
usage() {
echo $#
echo "Please invoke this script with the relative path to the databox component you would like to pin."
echo "Usage: databox-pin [OPTION...] [COMPONENT DIR]"
echo "Flags:"
echo "-d Delete the pin"
echo "-h This help message"
exit 1
}
if [[ $# < 1 ]] || [[ "$1" == "-h" ]] # Must have more than 1 args.
then
usage
fi
update_file() {
FILE=$1
COMP=$2
LINE=$3
LEFT=$(grep -v "^${COMP}" $FILE)
echo "$LEFT" > $FILE
echo -e $LINE >> $FILE
}
check_y() {
if [ $1 != "y" ]
then
echo "OK - no changes made"
exit 0
fi
}
add_entry() {
cd $COMPONENT_DIR >/dev/null 2>&1
assert $? 0 "directory (${COMPONENT_DIR}) not found"
git status >/dev/null 2>&1
assert $? 0 "directory (${COMPONENT_DIR}) is not a git repo."
ORIGIN=$(git remote get-url --push origin)
#convert ssh to https pins
ORIGIN=${ORIGIN//git@/https:\/\/}
ORIGIN=${ORIGIN//com:/com\/}
BRANCH=$(git rev-parse --abbrev-ref HEAD)
cd ..
echo "Are you sure you want to pin ${COMPONENT_NAME} to ${ORIGIN} ${BRANCH}? [y/n]"
read -n1 ANS
check_y $ANS
update_file $DATABOX_CONFIG_FILE $COMPONENT_NAME "$COMPONENT_NAME $ORIGIN $BRANCH"
echo "Done pined ${COMPONENT_NAME} to ${ORIGIN} ${BRANCH}"
}
del_entry() {
echo "Are you sure you want to unpin ${COMPONENT_NAME}? [y/n]"
read -n1 ANS
check_y $ANS
ORIGIN="https://github.com/me-box/${COMPONENT_NAME}"
BRANCH="master"
update_file $DATABOX_CONFIG_FILE $COMPONENT_NAME "$COMPONENT_NAME $ORIGIN $BRANCH"
echo "Done unpined ${COMPONENT_NAME}"
}
DATABOX_CONFIG_FILE=databox-components
if [[ "$1" == "-d" ]]
then
shift
DATABOX_DIR=${pwd}
COMPONENT_DIR=${1}
COMPONENT_NAME=${1//.\//}
COMPONENT_NAME=${COMPONENT_NAME%/}
del_entry
else
DATABOX_DIR=${pwd}
COMPONENT_DIR=${1}
COMPONENT_NAME=${1//.\//}
COMPONENT_NAME=${COMPONENT_NAME%/}
add_entry
fi