Skip to content

Commit 13e606c

Browse files
committed
Refactor entrypoint script for improved flexibility
Change the shebang to use env, add helper functions to handle environment variables, and dynamically append command arguments. This improves script flexibility and ensures critical variables are set or loaded from files.
1 parent af7729b commit 13e606c

File tree

2 files changed

+94
-68
lines changed

2 files changed

+94
-68
lines changed

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
ARG ALPINE_VERSION=latest
2-
FROM alpine:${ALPINE_VERSION} as build
2+
FROM alpine:${ALPINE_VERSION} AS build
33

44
ENV CXXFLAGS=""
55
WORKDIR /usr/src/telegram-bot-api

docker-entrypoint.sh

100644100755
Lines changed: 93 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,87 +1,113 @@
1-
#!/bin/sh
1+
#!/usr/bin/env sh
22
set -e
33

4-
file_env() {
5-
local var_name="$1"
6-
local file_var_name="$2"
4+
USERNAME="telegram-bot-api"
5+
GROUPNAME="telegram-bot-api"
76

8-
var_value=$(printenv "$var_name") || var_value=""
9-
file_path=$(printenv "$file_var_name") || file_path=""
7+
COMMAND="telegram-bot-api"
108

11-
if [ -z "$var_value" ] && [ -z "$file_path" ]; then
12-
echo "error: expected $var_name or $file_var_name env vars to be set"
13-
exit 1
9+
# Appends an argument to the COMMAND variable.
10+
append_args() {
11+
COMMAND="$COMMAND $1"
12+
}
1413

15-
elif [ -n "$var_value" ] && [ -n "$file_path" ]; then
16-
echo "both and $var_name $file_var_name env vars are set, expected only one of them"
17-
exit 1
14+
# Sets $env_var from $file_env_var content or directly from $env_var.
15+
# Usage: file_env <env_var> <file_env_var>
16+
# - If both or neither variables are set, exits with an error.
17+
# - If only $file_env_var is set, reads content from the file path and sets $env_var.
18+
# - Exits with an error if the file does not exist.
19+
file_env() {
20+
env_var="$1"
21+
file_env_var="$2"
22+
env_value=$(printenv "$env_var") || env_value=""
23+
file_path=$(printenv "$file_env_var") || file_path=""
1824

19-
else
20-
if [ -n "$file_path" ] && [ "$file_path" != "" ]; then
21-
if [ -f "$file_path" ]; then
22-
file_content=$(cat "$file_path")
23-
export "$var_name=$file_content"
24-
else
25-
echo "error: $var_name=$file_path: file '$file_path' does not exist"
25+
if [ -z "$env_value" ] && [ -z "$file_path" ]; then
26+
echo "error: expected $env_var or $file_env_var env vars to be set"
2627
exit 1
27-
fi
28+
elif [ -n "$env_value" ] && [ -n "$file_path" ]; then
29+
echo "both $env_var and $file_env_var env vars are set, expected only one of them"
30+
exit 1
31+
elif [ -n "$file_path" ]; then
32+
if [ -f "$file_path" ]; then
33+
export "$env_var=$(cat "$file_path")"
34+
else
35+
echo "error: $env_var=$file_path: file '$file_path' does not exist"
36+
exit 1
37+
fi
2838
fi
39+
}
40+
41+
# Checks if an environment variable is set.
42+
# Usage: check_required_env <var_name>
43+
# - Exits with an error if the variable is not set.
44+
check_required_env() {
45+
var_name="$1"
46+
47+
if [ -z "$(printenv "$var_name")" ]; then
48+
echo "error: environment variable $var_name is required"
49+
exit 1
2950
fi
3051
}
3152

32-
USERNAME=telegram-bot-api
33-
GROUPNAME=telegram-bot-api
53+
# Appends an argument to CUSTOM_ARGS based on the environment variable value.
54+
# Usage: append_arg_from_env <var_name> <arg_name> <default_value>
55+
# - If <var_name> is set, uses its value; otherwise, uses <default_value>.
56+
# - Appends "<arg_name>=<value>" to CUSTOM_ARGS if a value is found.
57+
append_arg_from_env() {
58+
var_name="$1"
59+
arg_name="$2"
60+
default_value="$3"
61+
env_value=$(printenv "$var_name") || env_value=""
3462

35-
chown ${USERNAME}:${GROUPNAME} "${TELEGRAM_WORK_DIR}"
63+
[ -n "$env_value" ] || env_value="$default_value"
64+
if [ -n "$env_value" ]; then
65+
append_args "${arg_name}=$env_value"
66+
fi
67+
}
68+
69+
# Appends a flag to CUSTOM_ARGS if the environment variable is set (non-empty).
70+
# Usage: append_flag_from_env <var_name> <flag_name>
71+
# - If <var_name> is set, appends <flag_name> to CUSTOM_ARGS.
72+
append_flag_from_env() {
73+
var_name="$1"
74+
flag_name="$2"
3675

37-
if [ -n "${1}" ]; then
38-
exec "${*}"
39-
fi
76+
if [ -n "$(printenv "$var_name")" ]; then
77+
append_args "$flag_name"
78+
fi
79+
}
4080

81+
check_required_env "TELEGRAM_WORK_DIR"
82+
chown "${USERNAME}:${GROUPNAME}" "${TELEGRAM_WORK_DIR}"
83+
84+
# Telegram Bot API Server knows how to read the API ID and API Hash from a environment variable.
85+
# Is not needed to pass it as arguments.
4186
file_env "TELEGRAM_API_ID" "TELEGRAM_API_ID_FILE"
4287
file_env "TELEGRAM_API_HASH" "TELEGRAM_API_HASH_FILE"
4388

44-
DEFAULT_ARGS="--dir=${TELEGRAM_WORK_DIR} --temp-dir=${TELEGRAM_TEMP_DIR} --username=${USERNAME} --groupname=${GROUPNAME}"
45-
CUSTOM_ARGS=""
46-
47-
if [ -n "$TELEGRAM_LOG_FILE" ]; then
48-
CUSTOM_ARGS=" --log=${TELEGRAM_LOG_FILE}"
49-
fi
50-
if [ -n "$TELEGRAM_STAT" ]; then
51-
CUSTOM_ARGS="${CUSTOM_ARGS} --http-stat-port=8082"
52-
fi
53-
if [ -n "$TELEGRAM_FILTER" ]; then
54-
CUSTOM_ARGS="${CUSTOM_ARGS} --filter=$TELEGRAM_FILTER"
55-
fi
56-
if [ -n "$TELEGRAM_MAX_WEBHOOK_CONNECTIONS" ]; then
57-
CUSTOM_ARGS="${CUSTOM_ARGS} --max-webhook-connections=$TELEGRAM_MAX_WEBHOOK_CONNECTIONS"
58-
fi
59-
if [ -n "$TELEGRAM_VERBOSITY" ]; then
60-
CUSTOM_ARGS="${CUSTOM_ARGS} --verbosity=$TELEGRAM_VERBOSITY"
61-
fi
62-
if [ -n "$TELEGRAM_MAX_CONNECTIONS" ]; then
63-
CUSTOM_ARGS="${CUSTOM_ARGS} --max-connections=$TELEGRAM_MAX_CONNECTIONS"
64-
fi
65-
if [ -n "$TELEGRAM_PROXY" ]; then
66-
CUSTOM_ARGS="${CUSTOM_ARGS} --proxy=$TELEGRAM_PROXY"
67-
fi
68-
if [ -n "$TELEGRAM_LOCAL" ]; then
69-
CUSTOM_ARGS="${CUSTOM_ARGS} --local"
70-
fi
71-
if [ -n "$TELEGRAM_HTTP_IP_ADDRESS" ]; then
72-
CUSTOM_ARGS="${CUSTOM_ARGS} --http-ip-address=$TELEGRAM_HTTP_IP_ADDRESS"
73-
fi
74-
75-
# Set http-port arg
76-
if [ -n "$TELEGRAM_HTTP_PORT" ]; then
77-
CUSTOM_ARGS="${CUSTOM_ARGS} --http-port=$TELEGRAM_HTTP_PORT"
78-
else
79-
CUSTOM_ARGS="${CUSTOM_ARGS} --http-port=8081"
80-
fi
81-
82-
COMMAND="telegram-bot-api ${DEFAULT_ARGS}${CUSTOM_ARGS}"
89+
# Default arguments, passed from the Dockerfile.
90+
# Potentially can be overwritten by environment variables, if needed, but is not recommended.
91+
append_arg_from_env "TELEGRAM_WORK_DIR" "--dir"
92+
check_required_env "TELEGRAM_TEMP_DIR"
93+
append_arg_from_env "TELEGRAM_TEMP_DIR" "--temp-dir"
94+
append_args "--username=${USERNAME}"
95+
append_args "--groupname=${GROUPNAME}"
8396

84-
echo "$COMMAND"
97+
check_required_env "TELEGRAM_API_ID"
98+
check_required_env "TELEGRAM_API_HASH"
8599

86-
# shellcheck disable=SC2086
100+
# Environment variables that can be passed as arguments from environment by administrator.
101+
append_arg_from_env "TELEGRAM_HTTP_PORT" "--http-port" "8081"
102+
append_flag_from_env "TELEGRAM_LOCAL" "--local"
103+
append_flag_from_env "TELEGRAM_STAT" "--http-stat-port=8082" # maybe change it to dynamic variable in the future
104+
append_arg_from_env "TELEGRAM_LOG_FILE" "--log"
105+
append_arg_from_env "TELEGRAM_FILTER" "--filter"
106+
append_arg_from_env "TELEGRAM_MAX_WEBHOOK_CONNECTIONS" "--max-webhook-connections"
107+
append_arg_from_env "TELEGRAM_VERBOSITY" "--verbosity"
108+
append_arg_from_env "TELEGRAM_MAX_CONNECTIONS" "--max-connections"
109+
append_arg_from_env "TELEGRAM_PROXY" "--proxy"
110+
append_arg_from_env "TELEGRAM_HTTP_IP_ADDRESS" "--http-ip-address"
111+
112+
echo "$COMMAND"
87113
exec $COMMAND

0 commit comments

Comments
 (0)