-
Notifications
You must be signed in to change notification settings - Fork 4
/
step.sh
executable file
·75 lines (63 loc) · 2.4 KB
/
step.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
71
72
73
74
75
#!/bin/bash
THIS_SCRIPTDIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
source "${THIS_SCRIPTDIR}/_utils.sh"
source "${THIS_SCRIPTDIR}/_formatted_output.sh"
# init / cleanup the formatted output
echo "" > "${formatted_output_file_path}"
CONFIG_env_file_path="$HOME/.bash_profile"
if [ -n "${ENVIRONMENT_PROFILE_FILE}" ] ; then
CONFIG_env_file_path="${ENVIRONMENT_PROFILE_FILE}"
fi
if [ ! -n "${ENVIRONMENT_KEY}" ]; then
write_section_to_formatted_output "# Error!"
write_section_to_formatted_output "* Reason: no Environment Key was specified!"
exit 1
fi
if [ ! -n "${ENVIRONMENT_VALUE}" ]; then
write_section_to_formatted_output "**No value provided, will be set to empty value!**"
fi
echo
echo "--- Config:"
echo "Environment Key: ${ENVIRONMENT_KEY}"
echo "Environment Value: ${ENVIRONMENT_VALUE}"
echo "Environment profile path: ${CONFIG_env_file_path}"
echo "---"
echo
if [ ! -n "${ENVIRONMENT_VALUE}" ]; then
# No value - make it empty
echo '' >> "${CONFIG_env_file_path}"
echo '# Exported Environment' >> "${CONFIG_env_file_path}"
echo "export ${ENVIRONMENT_KEY}=''" >> "${CONFIG_env_file_path}"
echo '' >> "${CONFIG_env_file_path}"
else
#
# Safe multi-line environment export, with
# here-doc syntax.
# Basically it'll append something like this to the specified Env Profile file:
# export YOUR_ENV_KEY=`cat << EOF_ENV_VAR
# The content of the variable, what you specified in ENVIRONMENT_VALUE
# EOF_ENV_VAR`
#
echo '' >> "${CONFIG_env_file_path}"
echo '# Exported Environment' >> "${CONFIG_env_file_path}"
printf '%s' "export ${ENVIRONMENT_KEY}=" >> "${CONFIG_env_file_path}"
printf '%s' '`cat << EOF_ENV_VAR' >> "${CONFIG_env_file_path}"
echo '' >> "${CONFIG_env_file_path}"
echo "${ENVIRONMENT_VALUE}" >> "${CONFIG_env_file_path}"
printf '%s' 'EOF_ENV_VAR`' >> "${CONFIG_env_file_path}"
echo '' >> "${CONFIG_env_file_path}"
echo '' >> "${CONFIG_env_file_path}"
fi
if [ $? -ne 0 ] ; then
write_section_to_formatted_output "# Error!"
write_section_to_formatted_output "Failed to write the environment variable into the specified environment profile file!"
exit 1
fi
write_section_to_formatted_output "# Success"
if [ ! -n "${ENVIRONMENT_VALUE}" ]; then
write_section_to_formatted_output "${ENVIRONMENT_KEY}: **not provided (empty)**"
else
write_section_to_formatted_output "${ENVIRONMENT_KEY}:"
write_section_to_formatted_output "${ENVIRONMENT_VALUE}"
fi
exit 0