/
repomenue_systemctl_user
executable file
·116 lines (105 loc) · 3.2 KB
/
repomenue_systemctl_user
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
#!/usr/bin/env bash
# -*-coding:utf-8 -*-
# Auto updated?
# Yes
#File :
# repomenue_systemctl_user
#Author:
# The-Repo-Club [wayne6324@gmail.com]
#Github:
# https://github.com/The-Repo-Club/
#
# Created:
# Wed 10 March 2021, 12:34:47 PM [GMT]
# Modified:
# Fri 21 January 2022, 02:29:56 PM [GMT]
#
# Description:
# Use systemctl --user from repomenu. Useful in tiling wms.
#
unset opts
unset service
unset unit
unset cmd
# make sure systemd is running
initsys=$(cat /proc/1/comm)
[[ "$initsys" == "systemd" ]] || exit 100
##
# Consistent repomenu commands, specific colors for repomenu.
# params: message, print, ...
# message: To be displayed as the prompt for repomenu
# print: The words to populate the list for repomenu
##
repomenucmd() {
local mesg=$1
shift
local printing=("$@")
printf "%s\n" "${printing[@]}" | repomenu -c -w 700 -l 10\
-p "$mesg"
}
# Specify the options you want to see. `status` is treated specially and should
# not be removed from the array.
opts=("start"
"run"
"stop"
"restart" "try-restart" "reload-or-restart"
"status"
"list-unit-files")
# This gets the list of units, limited to services and sockets (What is
# mostly manipulated with systemctl --user)
# Use a param to limit this, something like "loaded" or "loaded active"
# (see below)
get_systemctl_list() {
printf -- "%s" "$(systemctl --no-legend --full --user --type=service,socket --all list-units | sed -n "s/${1}.*//p")"
}
get_systemctl_files() {
printf -- "%s" "$(systemctl --no-legend --full --user --type=service,socket --all list-unit-files | sed -n "s/ .*//p")"
}
get_path_files() {
printf -- "%s" "$(repomenu_path)"
}
# Depending on the case, we want to filter by a specific state of unit.
# start, status: Get all units that are loaded
# stop, the *restarts: Get all units that are already active
# something else: Get all the files that are loaded
find_services () {
local action="$1"
local orig_list=()
case $action in
start)
orig_list=($(get_systemctl_files ) )
;;
status)
orig_list=($(get_systemctl_list "loaded") )
;;
stop|*restart)
orig_list=($(get_systemctl_list "loaded *active") )
;;
run)
orig_list=($(get_path_files) )
;;
*)
orig_list=($(get_systemctl_files ) )
;;
esac
printf -- "%s " ${orig_list[@]/.service/}
}
# Get the command to be run from the `opts` list
cmd="$(repomenucmd "Action:" "${opts[@]}")"
[[ -z $cmd ]] && exit 1
# Get the unit that is going to be run by systemctl. Can be a list of units,
# just as running it from the command line, though repomenu will only display the
# first one.
service=($(find_services "$cmd"))
unit="$(repomenucmd "systemctl --user $cmd" "${service[@]}")"
[[ -z $unit ]] && exit 2
# The actual running of commands. `status`, as stated above, is treated
# specially, and will open a alacritty window displaying the status output,
# otherwise we just run systemctl --user with the specified command and unit.
if [[ $cmd == "status" ]]; then
alacritty --title "repomenu-systemctl-user" -e /usr/bin/bash -c "systemctl --user $cmd $unit; read "
elif [[ $cmd == "run" ]]; then
systemd-run --user --unit="run-${unit}" "$unit"
else
systemctl --user "$cmd" "$unit"
fi