-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_secrets.sh
executable file
·71 lines (62 loc) · 1.86 KB
/
setup_secrets.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
#!/bin/bash
OUTPUT_PATH=".env"
function usage() {
echo "Invalid parameter, check guide from <link of Maisha documnetation>"
}
function check_parameters() {
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
--output_path)
OUTPUT_PATH=$2
shift
shift
;;
*)
usage
;;
esac
done
}
function check_package() {
local package="$1"
which "${package}" &> /dev/null
if [ $? -ne 0 ]
then
echo "${package} not found in the local machine,
check the documentation for installation <link of Maisha documnetation>"
exit 1
fi
}
function check_packages() {
check_package "vlt"
check_package "jq"
local os_type=$(uname)
if [[ "$os_type" == "MINGW"* || "$os_type" == "MSYS"* || "$os_type" == "CYGWIN"* ]];
then
echo "You are currently on a windows machine, check installation of xdg-utils..."
check_package "xdg-utils"
fi
}
function get_secrets() {
check_packages
check_parameters "$@"
local login_message="$(vlt login)"
echo $login_message
if [[ "$login_message" != "Successfully logged in" ]]; then
echo "Currently not loggin, login first to the vault secret to set up .env file"
exit 1
fi
local all_secrets_json=$(vlt secrets -format json)
local all_secrets_names="$(jq -r '.[]."name"' <<< ${all_secrets_json})"
for secret_name in $(echo $all_secrets_names);
do
local secret_val=$(vlt secrets get -plaintext $secret_name)
local upper_secret_name=$(echo $secret_name | tr '[:lower:]' '[:upper:]')
local new_env_line="${upper_secret_name}=${secret_val}"
echo "Adding ${upper_secret_name} to ${OUTPUT_PATH}"
echo $new_env_line >> $OUTPUT_PATH
done
echo "Successfully adding all variables to ${OUTPUT_PATH}"
}
get_secrets "$@"