-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsysmenu.sh
More file actions
executable file
·285 lines (244 loc) · 8.53 KB
/
sysmenu.sh
File metadata and controls
executable file
·285 lines (244 loc) · 8.53 KB
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
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'
# === Interactive systemd service manager using fzf, gum, and bat ===
# Traps
trap 'echo "Error on line $LINENO: $BASH_COMMAND"; coredump' ERR
trap 'echo "Script interrupted"; exit 130' INT TERM
# Exit if run as root
if [[ $EUID -eq 0 ]]; then
echo "This script must not be run as root"
exit 1
fi
# Global config variables
FAVORITES_FILE="$HOME/.sysmenu_favorites"
SHOW_FAVORITES_ONLY=false
RUN_AS_APP=false
# Function to check if a command is available
require_command() {
if ! command -v "$1" &>/dev/null; then
echo "Error: $1 is required but not installed"
exit 1
fi
}
# Commands required for this script
require_command fzf
require_command systemctl
require_command journalctl
require_command sudo
require_command awk
# Define the parameters used for this script
while [[ $# -gt 0 ]]; do
case "$1" in
--favorites | -f)
SHOW_FAVORITES_ONLY=true
shift
;;
--app | -a)
RUN_AS_APP=true
shift
;;
*)
echo "Unknown parameter: $1"
exit 1
;;
esac
done
# Check if gum is installed
if command -v gum &>/dev/null; then
IS_GUM_INSTALLED=true
else
IS_GUM_INSTALLED=false
fi
# Check if bat is installed and define its command
if command -v bat &>/dev/null; then
IS_BAT_INSTALLED=true
BAT_COMMAND="bat"
elif command -v batcat &>/dev/null; then
IS_BAT_INSTALLED=true
BAT_COMMAND="batcat"
else
IS_BAT_INSTALLED=false
fi
main() {
# Function to get systemd units
get_sysd_units() {
# Define arguments for systemctl commands
local args=(
--all
--plain
--no-legend
--no-pager
)
# List units with scope and format output
list_units_with_scope() {
local scope=$1
systemctl --"$scope" list-units "${args[@]}" |
awk -v scope="[$scope]" '{printf "%-60s %-15s %-10s %s\n", $1, $3, $4, scope}'
}
# List unit files with scope and format output
list_unit_files_with_scope() {
local scope=$1
systemctl --"$scope" list-unit-files "${args[@]}" |
awk -v scope="[$scope]" '{printf "%-60s %-15s %-10s %s\n", $1, $2, "(file)", scope}'
}
# List and sort units from both system and user scopes
all_units=$(
(
list_units_with_scope system
list_unit_files_with_scope system
list_units_with_scope user
list_unit_files_with_scope user
) | sort -u
)
# List favorite units from file if it exists
if [[ -f $FAVORITES_FILE ]]; then
# List favorite units
favorite_units=$(printf "%s\n" "$all_units" |
grep -Ff "$FAVORITES_FILE" |
sed 's/^/★ /')
# If showing only favorites, return only them
if $SHOW_FAVORITES_ONLY; then
printf "%s\n" "$favorite_units"
return
fi
# List other units
other_units=$(printf "%s\n" "$all_units" |
grep -Fvf "$FAVORITES_FILE" |
sed 's/^/ /')
# Combine favorite and other units, emphasizing favorites
printf "%s\n%s\n" "$favorite_units" "$other_units"
else
printf "%s\n" "$all_units"
fi
}
# Use fuzzy-finder to select one or multiple systemd services and return to the variable
selected=$(get_sysd_units |
fzf --preview "echo {} | sed 's/^[★ ]* *//' | awk '{print \$1}' | xargs systemctl status --no-pager" \
--preview-window=down:40%:wrap \
--header 'Select a systemd service to manage' \
--color "fg+:bold,hl:reverse,fg+:yellow,header:italic:underline" \
--ansi \
--multi \
--border \
--reverse)
services=$(echo "$selected" | sed 's/^[★ ]* *//' | awk '{print $1}')
scopes=$(echo "$selected" | sed 's/^[★ ]* *//' | awk '{print $NF}')
[[ -z $services ]] && exit 0
# Select an action to perform on the selected services using gum or fzf
if $IS_GUM_INSTALLED; then
action=$(
printf "start\nstop\nrestart\nenable\ndisable\nstatus\nlogs\ntoggle favorite" |
gum choose --header "$(printf "%s\n" "Select action for:" "${services[*]}")"
)
else
action=$(
printf "start\nstop\nrestart\nenable\ndisable\nstatus\nlogs\ntoggle favorite" |
fzf --header "$(printf "%s\n" "Select action for:" "${services[*]}")" \
--border \
--reverse
)
fi
[[ -z $action ]] && exit 0
# Function to execute the selected action
execute_action() {
local services=$1
local scopes=$2
local action=$3
# Run the selected action on the chosen services
case $action in
logs)
# Show logs with bat or less
local journalctl_args=()
for service in $services; do
journalctl_args+=(-u "$service")
done
if $IS_BAT_INSTALLED; then
sudo journalctl "${journalctl_args[@]}" -xe | $BAT_COMMAND --paging=always -l log
else
sudo journalctl "${journalctl_args[@]}" -xe | less
fi
;;
toggle\ favorite)
# Create favorites file if it doesn't exist
if [[ ! -f $FAVORITES_FILE ]]; then
touch "$FAVORITES_FILE"
chmod 600 "$FAVORITES_FILE"
fi
# Add or remove services to/from favorites list
for service in $services; do
# Add service to favorites list
if ! grep -qxF "$service" "$FAVORITES_FILE" 2>/dev/null; then
echo "$service" >>"$FAVORITES_FILE"
else
# Remove service from favorites list if it exists
grep -vxF "$service" "$FAVORITES_FILE" >"${FAVORITES_FILE}.tmp" && mv "${FAVORITES_FILE}.tmp" "$FAVORITES_FILE"
fi
done
;;
*)
mapfile -t services_arr < <(printf "%s\n" "$services")
mapfile -t scopes_arr < <(printf "%s\n" "$scopes")
local -a system_services=()
local -a user_services=()
for i in "${!services_arr[@]}"; do
local service=${services_arr[$i]}
local scope=${scopes_arr[$i]}
if [[ -z "$service" ]]; then
continue
fi
if [[ "$scope" == "[system]" ]]; then
system_services+=("$service")
elif [[ "$scope" == "[user]" ]]; then
user_services+=("$service")
fi
done
# Execute action on system services
if ((${#system_services[@]} > 0)); then
sudo systemctl --system --no-pager "$action" "${system_services[@]}"
fi
# Execute action on user services
if ((${#user_services[@]} > 0)); then
systemctl --user --no-pager "$action" "${user_services[@]}"
fi
# Display success message
if $IS_GUM_INSTALLED; then
gum style "Action successfully executed on:" "${services[*]}!" \
--foreground 212 \
--border double \
--margin "1 1" \
--padding "1 2" \
--align center \
--bold
else
echo "Action successfully executed on: ${services[*]}!"
fi
if $RUN_AS_APP; then
read -rp "Press Enter to continue..."
clear
fi
;;
esac
}
# Confirm the action with the user using gum or fzf
if $IS_GUM_INSTALLED; then
gum confirm "$(printf "%s\n" "Execute '$action' on" "${services[*]}"?)" || exit 0
gum spin --spinner dot --title "$(printf "%s\n" "Running $action on" "${services[*]}...")" -- sleep 0.5
execute_action "$services" "$scopes" "$action"
else
yesno=$(printf "yes\nno" |
fzf --header "$(printf "%s\n" "Execute '$action' on" "${services[*]}"?)" \
--border \
--reverse \
--disabled)
[[ $yesno != "yes" ]] && exit 0
execute_action "$services" "$scopes" "$action"
fi
}
if $RUN_AS_APP; then
while true; do
main
done
else
main
fi