Debian Codegraph @

src/make-dbeaver-configuration

raw symbols SCIP
1 #!/usr/bin/env bash
2 # ==========================================================================
3 # _ _ _ ____ ____ _____
4 # | | | (_) _ \ ___ _ __ / ___|___ _ _|_ _| __ __ _ ___ ___ _ __
5 # | |_| | | |_) / _ \ '__| | / _ \| '_ \| || '__/ _` |/ __/ _ \ '__|
6 # | _ | | __/ __/ | | |__| (_) | | | | || | | (_| | (_| __/ |
7 # |_| |_|_|_| \___|_| \____\___/|_| |_|_||_| \__,_|\___\___|_|
8 #
9 # --- High-Performance Connectivity Tracer (HiPerConTracer) ---
10 # https://www.nntb.no/~dreibh/hipercontracer/
11 # ==========================================================================
12 #
13 # High-Performance Connectivity Tracer (HiPerConTracer)
14 # Copyright (C) 2015-2026 by Thomas Dreibholz
15 #
16 # This program is free software: you can redistribute it and/or modify
17 # it under the terms of the GNU General Public License as published by
18 # the Free Software Foundation, either version 3 of the License, or
19 # (at your option) any later version.
20 #
21 # This program is distributed in the hope that it will be useful,
22 # but WITHOUT ANY WARRANTY; without even the implied warranty of
23 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 # GNU General Public License for more details.
25 #
26 # You should have received a copy of the GNU General Public License
27 # along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #
29 # Contact: [email protected]
30
31 # Bash options:
32 set -euo pipefail
33
34
35 # ###### Usage ##############################################################
36 usage () {
37 local exitCode="$1"
38 echo >&2 "Usage: $0 database_configuration_file ... [--join] [-h|--help] [-v|--version]"
39 exit "${exitCode}"
40 }
41
42
43 # ###### Version ############################################################
44 version () {
45 echo "Make-DBeaver-Configuration @BUILD_MAJOR@.@BUILD_MINOR@.@BUILD_PATCH@"
46 exit 0
47 }
48
49
50
51 # ###### Main program #######################################################
52
53 # ====== Handle arguments ===================================================
54 if (( BASH_VERSINFO[0] < 4 )) || (( (BASH_VERSINFO[0] == 4) && (BASH_VERSINFO[1] < 4) )) ; then
55 echo "ERROR: Bash 4.4 or higher is required, but Bash ${BASH_VERSINFO[0]}.${BASH_VERSINFO[1]} is installed!"
56 exit 1
57 fi
58 UNAME="$(uname)"
59 case "${UNAME}" in
60 FreeBSD)
61 GETOPT="$(PATH="/usr/local/bin:${PATH}" command -v getopt || true)"
62 ;;
63 NetBSD)
64 GETOPT="$(PATH="/usr/pkg/bin:${PATH}" command -v getopt || true)"
65 ;;
66 OpenBSD)
67 GETOPT="$(command -v gnugetopt || true)"
68 ;;
69 Darwin)
70 GETOPT="$(env PATH="/opt/homebrew/opt/gnu-getopt/bin:${PATH}" command -v getopt || true)"
71 ;;
72 *)
73 GETOPT="$(command -v getopt || true)"
74 ;;
75 esac
76 if "${GETOPT}" -T >/dev/null 2>&1 || [[ $? -ne 4 ]]; then
77 echo >&2 "ERROR: Enhanced/GNU getopt is required!"
78 exit 1
79 fi
80 options="$(${GETOPT} -o jhv --long join,help,version -a -- "$@")"
81 # shellcheck disable=SC2181
82 if [[ $? -ne 0 ]]; then
83 usage 1
84 fi
85
86 JOIN=0
87 eval set -- "${options}"
88 while [ $# -gt 0 ] ; do
89 case "$1" in
90 -j | --join)
91 JOIN=1
92 ;;
93 -h | --help)
94 usage 0
95 ;;
96 -v | --version)
97 version
98 ;;
99 --)
100 shift
101 break
102 ;;
103 *)
104 # This should not happen: wrong getopt parameters, or missing case?
105 echo >&2 "INTERNAL ERROR: Unhandled option $1!"
106 exit 1
107 ;;
108 esac
109 shift
110 done
111 if [ $# -lt 1 ] ; then
112 usage 1
113 fi
114 DIRNAME="$(dirname "$0")"
115
116
117 # ====== Process configuration files ========================================
118 echo -e "\x1b[34m$(date +"%F %H:%M:%S"): Generating DBeaver configuration ...\x1b[0m"
119 tempDirectory="$(mktemp -d -t "make-dbeaver-configuration-XXXXXX")"
120 trap 'rm -rf "${tempDirectory}"' EXIT
121
122 dbeaverConfig=""
123 dbeaverCredentials=""
124 while [ $# -gt 0 ] ; do
125 echo -e "\x1b[37mProcessing $(basename "$1") ...\x1b[0m"
126 "${DIRNAME}/dbshell" "$1" --dry-run --quiet --write-dbeaver-config "${tempDirectory}/$(basename "${1//.conf/}")"
127 shift
128 done
129
130
131 # ====== Read existing configuration ========================================
132 if [ ${JOIN} -eq 1 ] ; then
133 echo -e "\x1b[37mLooking for existing DBeaver configuration ...\x1b[0m"
134 for dbeaverDirectory in \
135 "${HOME}/snap/dbeaver-ce/current/.local/share/DBeaverData/workspace6/General/.dbeaver" \
136 "${HOME}/.local/share/DBeaverData/workspace6/General/.dbeaver" ; do
137 if [ -e "${dbeaverDirectory}" ] ; then
138 if [ -e "${dbeaverDirectory}/data-sources.json" ] ; then
139 dbeaverConfig="${dbeaverDirectory}/data-sources.json"
140 echo -e "\x1b[37mJoining data sources in ${dbeaverConfig}\x1b[0m"
141 fi
142 if [ -e "${dbeaverDirectory}/credentials-config.json" ] ; then
143 dbeaverCredentials="${tempDirectory}/credentials-config.json"
144 echo -e "\x1b[37mJoining credentials in ${dbeaverDirectory}/credentials-config.json\x1b[0m"
145 "${DIRNAME}/decrypt-dbeaver-configuration" \
146 "${dbeaverDirectory}/credentials-config.json" \
147 "${dbeaverCredentials}"
148 fi
149 break
150 fi
151 done
152 if [ ! -e "${dbeaverDirectory}" ] ; then
153 echo >&2 "ERROR: Unable to find DBeaver configuration directory!"
154 exit 1
155 fi
156 fi
157
158
159 # ====== Join new DBeaver configuration files ===============================
160 echo -e "\x1b[37mJoining configurations ...\x1b[0m"
161 # shellcheck disable=SC2086
162 jq --sort-keys --slurp 'reduce .[] as $item ({}; . * $item)' ${dbeaverConfig} "${tempDirectory}"/*-data-source.json >"${tempDirectory}/data-sources.json"
163 # shellcheck disable=SC2086
164 jq --sort-keys --slurp 'reduce .[] as $item ({}; . * $item)' ${dbeaverCredentials} "${tempDirectory}"/*-credentials-config.json >"${tempDirectory}/credentials-config.json.plaintext"
165
166 # Encrypt the new credentials files:
167 "${DIRNAME}/encrypt-dbeaver-configuration" \
168 "${tempDirectory}/credentials-config.json.plaintext" \
169 "${tempDirectory}/credentials-config.json"
170
171
172 # ====== Join with existing configuration ===================================
173 if [ ${JOIN} -eq 1 ] ; then
174 if [ "${dbeaverConfig}" != "" ] ; then
175 if ! diff "${tempDirectory}/data-sources.json" "${dbeaverDirectory}/data-sources.json" >/dev/null ; then
176 dbeaverConfigBackup="${dbeaverConfig}.backup-$(date +"%F-%H:%M:%S")"
177 echo -e "\x1b[37mMaking data sources backup in ${dbeaverConfigBackup} ...\x1b[0m"
178 cp "${dbeaverConfig}" "${dbeaverConfigBackup}"
179 fi
180 fi
181 echo -e "\x1b[37mUpdating data sources ${dbeaverDirectory}/data-sources.json ...\x1b[0m"
182 mv "${tempDirectory}/data-sources.json" "${dbeaverDirectory}/data-sources.json"
183
184 if [ "${dbeaverCredentials}" != "" ] ; then
185 if ! diff "${dbeaverCredentials}" "${tempDirectory}/credentials-config.json.plaintext" >/dev/null ; then
186 dbeaverCredentialsBackup="${dbeaverDirectory}/credentials-config.json-$(date +"%F-%H:%M:%S")"
187 echo -e "\x1b[37mKeeping backup in ${dbeaverCredentialsBackup} ...\x1b[0m"
188 cp "${dbeaverCredentials}" "${dbeaverCredentialsBackup}"
189 fi
190 fi
191 echo -e "\x1b[37mUpdating credentials ${dbeaverDirectory}/credentials-config.json ...\x1b[0m"
192 mv "${tempDirectory}/credentials-config.json" "${dbeaverDirectory}/credentials-config.json"
193
194 else
195
196 # Just generated the configuration files -> no joining was performed!
197 mv "${tempDirectory}/data-sources.json" "${tempDirectory}/credentials-config.json" .
198 echo "Generated configuration files:"
199 ls -l credentials-config.json data-sources.json
200
201 fi
202
203
204 # ====== Clean up ===========================================================
205 echo -e "\x1b[37mDone!\x1b[0m"
206 rm -rf "${tempDirectory}"