forked from efrecon/docker-mosquitto
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bridge-topic.sh
executable file
·79 lines (62 loc) · 1.88 KB
/
bridge-topic.sh
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
#!/bin/sh
###
### Set topic mapping for Mosquitto Bridge configuration
###
### This is done by copying the contents of the topics file
### and inserting them in the generated bridge.conf file
### Accepted Arguments
### $1 → The topic list file
### $2 → The configuration file to update
source ./logger.sh
### Exit codes
TOPIC_NOT_SET_CODE=41
BRIDGE_CONF_NOT_SET_CODE=42
BRIDGE_CONF_FILE_NOT_FOUND_CODE=43
TOPIC_FILE_NOT_FOUND_CODE=44
TOPIC_EMPTY_CODE=45
###
### Accepted Arguments
### $1 → Error Message
### $2 → The exit code
###
function handleError() {
error "${1}"
exit $2
}
###
### Accepted Arguments
### $1 → Last item in the topics file
### $2 → The topics file
### $3 → The target configuration file
###
function setTopics() {
### Add remove everything between the '#topic' and the next config section
sed -i -e '/^#topic/,/^# .*/{/^#topic/!{/^# .*/!d;};}' "${3}"
### Insert the topics from the topics file
sed -i '/^#topic/ r '"${2}" "${3}"
### Append a space after the last topic inserted
sed -i "\|^${1}|"'a \
' "${3}"
}
### Map arguments to meaningful variables
TOPICS_FILE="$1"
BRIDGE_CONF_FILE="$2"
if [ -z "${TOPICS_FILE}" ]; then
handleError "Topic file env variable not set!" $TOPIC_NOT_SET_CODE
fi
if [ -z "${BRIDGE_CONF_FILE}" ]; then
handleError "Configuration file env variable not set!" $BRIDGE_CONF_NOT_SET_CODE
fi
if [ ! -f "${BRIDGE_CONF_FILE}" ]; then
handleError "Target configuration file not found!" $BRIDGE_CONF_FILE_NOT_FOUND_CODE
fi
if [ ! -f "${TOPICS_FILE}" ]; then
handleError "Topic file not found!" $TOPIC_FILE_NOT_FOUND_CODE
fi
## Get the last topic from the topics file
LAST_TOPIC=$(tail -1 "${TOPICS_FILE}")
if [ -z "${LAST_TOPIC}" ]; then
handleError "No topics found!" $TOPIC_EMPTY_CODE
fi
setTopics "${LAST_TOPIC}" "${TOPICS_FILE}" "${BRIDGE_CONF_FILE}"
exit 0