forked from metabrainz/musicbrainz-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-replication-token
executable file
·83 lines (67 loc) · 1.8 KB
/
set-replication-token
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
#!/usr/bin/env bash
set -e -u
# shellcheck source=admin/lib/common.inc.bash
source "$(dirname "${BASH_SOURCE[0]}")/lib/common.inc.bash"
HELP=$(cat <<EOH
Usage: $SCRIPT_NAME [option]
Read token and write it to local/secrets/metabrainz_access_token.
Option:
-f, --force Overwrite existing token if any.
-h, --help Print this help message
Environment variable:
METABRAINZ_ACCESS_TOKEN_PATH
This environment variable allows to specify another path to write to.
EOH
)
# Parse arguments
declare -i overwrite=0
if [ $# -eq 1 ]
then
case "$1" in
-f|--force)
overwrite=1
shift
;;
-h|--help|help)
echo "$HELP"
exit 0 # EX_OK
;;
*)
echo >&2 "$SCRIPT_NAME: unrecognized argument: '$1'"
echo >&2 "Try '$SCRIPT_NAME --help' for usage."
exit 64 # EX_USAGE
;;
esac
fi
if [ $# -ne 0 ]
then
echo >&2 "$SCRIPT_NAME: too many arguments"
echo >&2 "Try '$SCRIPT_NAME --help' for usage."
exit 64 # EX_USAGE
fi
declare token=''
while [ -z "$token" ]
do
read -n 40 -p 'Please enter your MetaBrainz access token (will not be echoed)...' -r -s token
echo
if [ ${#token} -ne 40 ]
then
echo Token should be 40 characters long, you entered only ${#token}.
token=''
fi
done
echo 'Thank you!'
declare -r token_default_path="$MB_DOCKER_ROOT/local/secrets/metabrainz_access_token"
declare -r TOKEN_PATH="${METABRAINZ_ACCESS_TOKEN_PATH:-$token_default_path}"
if [[ -f $TOKEN_PATH && $overwrite -ne 1 ]]
then
echo >&2 "$SCRIPT_NAME: token file already exists at: '$TOKEN_PATH'"
echo >&2 "Try '$SCRIPT_NAME --help' for usage."
exit 73 # EX_CANTCREAT
fi
mkdir -p "$(dirname "$TOKEN_PATH")"
touch "$TOKEN_PATH"
chmod 600 "$TOKEN_PATH"
echo "$token" > "$TOKEN_PATH"
echo "Successfully wrote token in '$TOKEN_PATH'."
# vi: set et sts=2 sw=2 ts=2 :