-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvyman
executable file
·305 lines (250 loc) · 8.96 KB
/
envyman
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
#!/bin/bash
# Constants
LAST_LOADED_CONFIG_FILE=~/.envyman_last_loaded_config
# Function to retrieve the root path from ~/.envyman_root_path file
get_root_path() {
local default_root_path="$HOME/envyman"
local root_path
if [ -f ~/.envyman_root_path ]; then
root_path=$(cat ~/.envyman_root_path)
else
echo "Default root path set: $default_root_path"
mkdir -p "$default_root_path"
echo "$default_root_path" > ~/.envyman_root_path
root_path=$default_root_path
fi
echo "$root_path"
}
# Function to add environment variable configuration to a file
add_configuration() {
local root_path=$(get_root_path)
local config_file="$1"
shift
if [[ -z "$config_file" ]]; then
echo "Usage: envyman add [config_file] [VAR1=VALUE1 VAR2=VALUE2 ...]"
return
fi
# Append .sh extension if not present
if [[ ! $config_file == *.sh ]]; then
config_file="${config_file}.sh"
fi
local full_path="${root_path}/${config_file}"
local directory="$(dirname "${full_path}")"
# Create directory if it doesn't exist
if [[ ! -d "${directory}" ]]; then
mkdir -p "${directory}"
fi
echo "#!/bin/bash" > "${full_path}"
# Loop through the remaining arguments
for arg in "$@"; do
# Split each argument into variable name and value
IFS='=' read -r var_name var_value <<< "$arg"
echo "export $var_name=\"$var_value\"" >> "${full_path}"
done
echo "Configuration added to ${full_path}"
}
# Function to start using the environment variable configuration
start_configuration() {
local root_path=$(get_root_path)
local path="$1"
shift
if [[ -z "$path" ]]; then
echo "Usage: envyman start [path]"
return
fi
# Append .sh extension if not present
if [[ ! $path == *.sh ]]; then
path="${path}.sh"
fi
local full_path="${root_path}/${path}"
if [ -f "${full_path}" ]; then
local temp_env_file="/tmp/envyman"
echo "#!/bin/bash" > "$temp_env_file"
echo ". \"$full_path\"" >> "$temp_env_file"
. "$temp_env_file"
echo "$full_path" > "$LAST_LOADED_CONFIG_FILE"
echo "$@" >> "$LAST_LOADED_CONFIG_FILE"
echo "Configuration loaded from ${full_path}"
else
echo "Configuration file not found at ${full_path}"
fi
}
# Function to stop using the environment variable configuration
stop_configuration() {
# Get the path of the currently sourced configuration file
local current_config_file=$(grep -oP '(?<=. ").*(?=")' /tmp/envyman)
# Check if the configuration file exists
if [[ -f "$current_config_file" ]]; then
# Read the configuration file line by line
while read -r line; do
# Check if the line is an export statement and unset the variable
if [[ "$line" =~ ^export\ [a-zA-Z_][a-zA-Z_0-9]*=\" ]]; then
var_name=$(echo "$line" | cut -d= -f1 | cut -d' ' -f2)
unset "$var_name"
fi
done < "$current_config_file"
echo "Configuration stopped and variables unset for $current_config_file"
else
echo "No active configuration file found"
fi
local temp_env_file="/tmp/envyman"
echo "# no active connection" > "$temp_env_file"
rm -f "$LAST_LOADED_CONFIG_FILE"
}
# Function to move existing files to the new root path
move_files() {
local root_path="$1"
local old_root_path="$2"
# Move files to new root path
find "${old_root_path}" -type f -name '*.sh' -exec bash -c 'mv "$0" "${1}${0#"$2"}"' {} "${root_path}/" "${old_root_path}/" \;
}
# Function to set the root path
set_root_path() {
local new_root_path="$1"
local old_root_path
if [ -f ~/.envyman_root_path ]; then
old_root_path=$(cat ~/.envyman_root_path)
fi
if [ -d "$new_root_path" ]; then
if [[ -n "$old_root_path" && "$new_root_path" != "$old_root_path" ]]; then
echo "Root path changed from $old_root_path to $new_root_path"
move_files "$new_root_path" "$old_root_path"
else
echo "Root path is already set to $new_root_path"
fi
else
echo "Directory does not exist. Creating $new_root_path"
mkdir -p "$new_root_path"
fi
echo "$new_root_path" > ~/.envyman_root_path
echo "Root path set to $new_root_path"
}
# Function to list configuration files
list_configurations() {
local root_path=$(get_root_path)
local config_files=$(find "$root_path" -type f -name "*.sh")
if [ -z "$config_files" ]; then
echo "No configuration files found in $root_path"
else
echo "Configuration files:"
for file in $config_files; do
relative_path=${file#$root_path/}
config_name=${relative_path%.sh}
echo "$config_name"
done
fi
}
# Function to echo the current root path
get_current_root() {
local root_path=$(get_root_path)
echo "Current root path: $root_path"
}
# Function to check configuration and print loaded file
check_configuration() {
if [ -f "$LAST_LOADED_CONFIG_FILE" ]; then
local loaded_file=$(cat "$LAST_LOADED_CONFIG_FILE")
echo "Last loaded configuration: $loaded_file"
# source "$LAST_LOADED_CONFIG_FILE"
while read -r line; do
if [[ $line =~ ^[a-zA-Z_][a-zA-Z0-9_]*= ]]; then
variable_name="${line%%=*}"
if [[ "${!variable_name}" != "${line#*=}" ]]; then
echo "Variable $variable_name isn't loaded!"
echo "Configuration is inactive or partially overwritten, call \"envyman start\" to activate a configuration or reactivate the last one."
return 1
fi
fi
done < "$LAST_LOADED_CONFIG_FILE"
echo "Configuration is active"
return 0
fi
echo "Configuration is inactive, call \"envyman start\" to activate a configuration."
return 1
}
# Function to install envyman
install_envyman() {
local script_path=$(realpath "$0")
# Move script files to envyman directory
cp "$script_path" "$HOME/.local/bin/envyman"
# Make the Envyman script executable
chmod +x "$HOME/.local/bin/envyman"
# Init /tmp/envyman
touch "/tmp/envyman"
# add source envyman and alias to bashrc
echo "exec &>/dev/null" >> "$HOME/.bashrc"
echo ". \"/tmp/envyman\"" >> "$HOME/.bashrc"
echo "exec &>/dev/tty" >> "$HOME/.bashrc"
echo "alias EM=\"\. /tmp/envyman\"" >> "$HOME/.bashrc" # add alias to /tmp/envyman for easier sourcing
echo "Envyman installed successfully!"
}
# Function to uninstall envyman
uninstall_envyman() {
# Confirmation prompt
read -p "Are you sure you want to uninstall Envyman? This action is irreversible. (y/n): " confirm
if [[ $confirm != "y" ]]; then
echo "Uninstallation aborted."
return
fi
rm "$HOME/.local/bin/envyman"
# Remove envyman from .bashrc
sed -i '/^exec &>\/dev\/null$/d;/^\. "\/tmp\/envyman"$/d;/^exec &>\/dev\/tty$/d;/^alias EM="\. \/tmp\/envyman"$/d' "$HOME/.bashrc"
# Prompt to remove configurations
read -p "Remove configurations (y/n): " remove_configs
if [[ $remove_configs == "y" ]]; then
local root_path=$(get_root_path)
rm -rf "$root_path"
fi
echo "Envyman uninstalled successfully!"
}
# Main script logic
case "$1" in
"add")
shift
add_configuration "$@"
;;
"start")
start_configuration "$2"
;;
"stop")
stop_configuration
;;
"root")
set_root_path "$2"
;;
"ls")
list_configurations
;;
"groot")
get_current_root
;;
"check")
check_configuration
;;
"install")
install_envyman
;;
"uninstall")
uninstall_envyman
;;
*)
echo "Usage: envyman [command] [arguments]"
echo "Available commands:"
echo " add : Add environment variable configuration"
echo " Usage: envyman add [config_file] [VAR1=VALUE1 VAR2=VALUE2 ...]"
echo " start : Start using the environment variable configuration"
echo " Usage: envyman start [path]"
echo " Note: to commit the values to the current terminal session run \"EM\""
echo " stop : Stop using the environment variable configuration"
echo " Usage: envyman stop"
echo " root : Set the root path for configuration files"
echo " Usage: envyman root [path]"
echo " ls : List all configuration files in the root path"
echo " Usage: envyman ls"
echo " groot : Echo the current root path"
echo " Usage: envyman groot"
echo " check : Check configuration and print loaded file"
echo " Usage: envyman check"
echo " install : Install envyman"
echo " uninstall : Uninstall envyman"
;;
esac