-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalma-install.sh
executable file
·151 lines (121 loc) · 4.79 KB
/
alma-install.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/bin/zsh
# Function to save GitHub credentials
save_github_credentials() {
# Check if GitHub credentials have already been added
if grep -q "######## GitHub Credentials Start" ~/.zshrc; then
echo "GitHub credentials have already been added to your .zshrc file."
return 0
fi
# Ask the user for their credentials
read "github_token?Please enter your GitHub token: "
read "github_user?Please enter your GitHub username: "
read "github_email?Please enter your GitHub email: "
# Check if any of the inputs are empty and return if true
if [[ -z "$github_token" || -z "$github_user" || -z "$github_email" ]]; then
echo "One or more inputs were not provided. Exiting without changes."
return 1
fi
# Add a header for GitHub credentials
echo "\n######## GitHub Credentials Start ########\n" >> ~/.zshrc
# Set the GitHub credentials
set_env_var GITHUB_TOKEN "$github_token"
set_env_var GITHUB_USER "$github_user"
set_env_var GITHUB_EMAIL "$github_email"
# Add a footer for GitHub credentials
echo "\n######## GitHub Credentials End ########\n" >> ~/.zshrc
echo "Your GitHub credentials have been saved."
}
# Function to safely set environment variables
set_env_var() {
local var_name="$1"
local var_value="$2"
local file=~/.zshrc
# Escape any characters that would break the .zshrc syntax
var_value=$(printf '%s\n' "$var_value" | sed 's/[&/\]/\\&/g')
# Append the new environment variable
echo "export $var_name=\"$var_value\"" >> "$file"
}
create_scripts_aliases() {
local SCRIPTS_DIR="$(pwd)"
if ! grep -q "######## Alma Aliases Start" ~/.zshrc; then
# Add a header for Alma aliases
echo "\n######## Alma Aliases Start ########\n" >> ~/.zshrc
fi
# Iterate over each script file and create an alias
find "$SCRIPTS_DIR" -type f -name "*.sh" | while read file; do
local filename=$(basename -- "$file")
local alias_name="${filename%.*}"
create_alias "$alias_name" "$file"
done
add_klogs_aliases
if ! grep -q "######## Alma Aliases End" ~/.zshrc; then
# Add a footer for Alma aliases
echo "\n######## Alma Aliases End ########\n" >> ~/.zshrc
fi
echo "Script aliases have been added to your .zshrc file."
}
# Function to create an individual alias
create_alias() {
local alias_name="$1"
local command="$2"
# Check if the alias is already in the .zshrc
if ! grep -q "alias $alias_name=" ~/.zshrc; then
# Append the new alias to the .zshrc file
echo "alias $alias_name='$command'" >> ~/.zshrc
echo "Added alias $alias_name."
else
echo "Alias $alias_name already exists, skipping..."
fi
}
add_klogs_aliases() {
# Define the aliases as an associative array
declare -A aliases
aliases=(
[otlpgw-logs]='/usr/local/bin/klogs -name=gw-otlp'
[ddgw-logs]='/usr/local/bin/klogs -name=gw-datadog'
[query-logs]='/usr/local/bin/klogs -name=query-processor'
[topic-logs]='/usr/local/bin/klogs -name=topic-processor'
[trace-agg-logs]='/usr/local/bin/klogs -name=trace-aggregator'
[trace-logs]='/usr/local/bin/klogs -name=trace-processor'
[otlp-logs]='/usr/local/bin/klogs -name=otlp-processor'
[api-logs]='/usr/local/bin/klogs -name=api-backend'
[tests-logs]='/usr/local/bin/klogs -name=integration-tests-job'
[usecase-logs]='/usr/local/bin/klogs -name=use-case-controller'
)
# Add each alias
for name in "${(@k)aliases}"; do
create_alias $name "${aliases[$name]}"
done
}
install() {
ZSHRC_FILE="$HOME/.zshrc"
cp "$ZSHRC_FILE" "${ZSHRC_FILE}.backup"
if ! grep -q "######## Alma Scripts Start" ~/.zshrc; then
# Add a header for Alma scripts
echo "\n############### Alma Scripts Start ###############" >> ~/.zshrc
fi
# Export the SCRIPTS_DIR to PATH if it's not already present
if ! grep -q "export PATH=\"\$PATH:$(pwd)\"" ~/.zshrc; then
echo "export PATH=\"\$PATH:$(pwd)\"" >> ~/.zshrc
fi
# Check if INFRA_PATH is already set and add it if not
if ! grep -q "export INFRA_PATH=" ~/.zshrc; then
# Prompt user for the absolute path of the infra repository
read "infra_abs_path?Please enter your infra repo absolute path (for example /Users/tomertwig/Alma/infra): "
# Remove any trailing slashes to avoid double slashes in paths
infra_abs_path="${infra_abs_path%/}"
# Write the INFRA_PATH variable to .zshrc without backslashes or quotes
echo "export INFRA_PATH="${infra_abs_path}"" >> ~/.zshrc
fi
# Call function to save GitHub credentials
save_github_credentials
# Call function to create scripts aliases
create_scripts_aliases
if ! grep -q "######## Alma Scripts End" ~/.zshrc; then
# Add a footer for Alma scripts
echo "\n############### Alma Scripts End ###############\n" >> ~/.zshrc
fi
echo "\n############ Please run 'source ~/.zshrc' to apply the changes ############"
}
# Call the install function
install