#!/usr/bin/env bash

# Copyright (C) 2018 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2018 DebOps <https://debops.org/>
# SPDX-License-Identifier: GPL-3.0-only

# Return Redis password on the standard output

# Usage:
#
#     redis-password [instance] [config file]
#
#     redis-cli -a $(redis-password)
#
# If instance is not specified, "main" is used by default


set -o nounset -o pipefail -o errexit

redis_instance="${1:-main}"
redis_conf="${2:-/etc/redis/${redis_instance}/redis.conf}"

if [ -r "${redis_conf}" ] ; then
    grep -E '^masterauth' "${redis_conf}" | awk '{print $2}' | sed -e 's/^"//' -e 's/"$//'
else
    exit 1
fi
