summaryrefslogtreecommitdiffstats
path: root/sysz
blob: 2fcb88a29ea6458548be5a572ecf3e9ac6671d1e (plain)
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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
#!/usr/bin/env bash

set -o pipefail
shopt -s lastpipe
shopt -s extglob

export SHELL=bash

PROG=$(basename "$0")
SYSZ_VERSION=1.4.3
SYSZ_HISTORY=${SYSZ_HISTORY:-${XDG_CACHE_HOME:-~/.cache}/sysz/history}
declare -a STATES

_sysz_keys() {
  cat <<EOF
Keybindings:
  TAB           Toggle selection.
  ctrl-v        'cat' the unit in the preview window.
  ctrl-s        Select states to match. Selection is reset.
  ctrl-r        Run daemon-reload. Selection is reset.
  ctrl-p        History previous.
  ctrl-n        History next.
  ?             Show keybindings.
EOF
}

_sysz_help() {
  cat >&2 <<EOF
A utility for using systemctl interactively via fzf.

Usage: $PROG [OPTS...] [CMD] [-- ARGS...]

sudo is invoked automatically, if necessary.

If only one unit is chosen, available commands will be presented
based on the state of the unit (e.g. "start" only shows if unit is "active").

OPTS:
  -u, --user               Only show --user units
  --sys, --system          Only show --system units
  -s STATE, --state STATE  Only show units in STATE (repeatable)
  -V, --verbose            Print the systemctl command
  -v, --version            Print the version
  -h, --help               Print this message

  If no options are given, both system and user units are shown.

CMD:
  start                  systemctl start <unit>
  stop                   systemctl stop <unit>
  r, restart             systemctl restart <unit>
  s, stat, status        systemctl status <unit>
  ed, edit               systemctl edit <unit>
  reload                 systemctl reload <unit>
  en, enable             systemctl enable <unit>
  d, dis, disable        systemctl disable <unit>
  c, cat                 systemctl cat <unit>

  If no command is given, one or more can be chosen interactively.

ARGS are passed to the systemctl command for each selected unit.

$(_sysz_keys)

History:
  $PROG is stored in $SYSZ_HISTORY
  This can be changed with the environment variable: SYSZ_HISTORY

Some units are colored based on state:
  green       active
  red         failed
  yellow      not-found

Examples:
  $PROG -u                      User units
  $PROG --sys -s active          Active system units
  $PROG --user --state failed   Failed user units

Examples with commands:
  $PROG start                  Start a unit
  $PROG --sys s                Get the status of system units
  $PROG --user edit            Edit user units
  $PROG s -- -n100             Show status with 100 log lines
  $PROG --sys -s active stop    Stop an active system unit
  $PROG -u --state failed r    Restart failed user units
EOF

  exit 0
}

_sysz_run() {
  [[ $VERBOSE = true ]] && echo '>' "$@" >&2
  eval "$@" || return $?
}

_sysz_systemctl() {
  if [[ $EUID -ne 0 && $1 = --system ]]; then
    # only run sudo if we aren't root and it's a system unit
    _sysz_run sudo systemctl "$@"
  else
    _sysz_run systemctl "$@"
  fi
}

_sysz_journalctl() {
  # remove --system from journalctl
  if [[ $1 = --system ]]; then
    shift
    if [[ $EUID -ne 0 ]]; then
      # only run sudo if we aren't root and it's a system unit
      _sysz_run sudo journalctl "$@"
      return
    fi
  fi

  # remove --user flag from journalctl
  # --user and --system don't work the same on journalctl
  # as they do for systemctl
  if [[ $1 = --user ]]; then
    shift
  fi

  _sysz_run journalctl "$@"
}

_sysz_manager() {
  case ${1%% *} in
  '[user]')
    echo --user
    ;;
  '[system]')
    echo --system
    ;;
  *)
    echo "ERROR: Unknown manager: $1" >&2
    exit 1
    ;;
  esac
}

_fzf_cat() {
  local MANAGER
  MANAGER=$(_sysz_manager "$1")
  local UNIT
  UNIT=${1##* }

  SYSTEMD_COLORS=1 systemctl "$MANAGER" cat -- "$UNIT"
}

_fzf_preview() {
  local MANAGER
  MANAGER=$(_sysz_manager "$1")
  local UNIT
  UNIT=${1##* }

  if [[ $UNIT = *@.* ]]; then
    _fzf_cat "$@"
  else
    SYSTEMD_COLORS=1 systemctl "$MANAGER" status --no-pager -- "$UNIT"
  fi
  exit 0
}

_sysz_show() {
  local manager
  manager=$(_sysz_manager "$1")
  local unit
  unit=${1##* }

  _sysz_systemctl "$manager" show "$unit" -p "$2" --value
}

_sysz_sort() {
  local str
  local mgr
  local unit
  local n
  while IFS= read -r str; do
    mgr=${str%% *}
    unit_colored=${str##* }
    unit=${unit_colored//$'\e'[\[(]*([0-9;])[@-n]/}

    if [[ $unit =~ \.service$ ]]; then
      n=0
      [[ $mgr = "[system]" ]] && n=1
    elif [[ $unit =~ \.timer$ ]]; then
      n=2
      [[ $mgr = "[system]" ]] && n=3
    elif [[ $unit =~ \.socket$ ]]; then
      n=4
      [[ $mgr = "[system]" ]] && n=5
    elif [[ $mgr = "[user]" ]]; then
      n=6
    else
      # then the rest based on file extension
      n=7
    fi
    type=${unit##*.}
    unit_undashed=${unit//-/}
    echo "$n$type$unit_undashed $mgr $unit_colored"
  done | sort -bifu | cut -d' ' -f2-
}

_sysz_list() {
  local args
  declare -a args
  args=(
    --all
    --no-legend
    --full
    --plain
    --no-pager
    "${STATES[@]}"
    "$@"
  )
  (
    systemctl list-units "${args[@]}"
    systemctl list-unit-files "${args[@]}"
  ) | sort -u -t ' ' -k1,1 |
    while read -r line; do
      unit=${line%% *}
      if [[ $line = *" active "* ]]; then
        printf '\033[0;32m%s\033[0m\n' "$unit" # green
      elif [[ $line = *" failed "* ]]; then
        printf '\033[0;31m%s\033[0m\n' "$unit" # red
      elif [[ $line = *" not-found "* ]]; then
        printf '\033[1;33m%s\033[0m\n' "$unit" # red
      else
        echo "$unit"
      fi
    done
}

_sysz_list_units() {
  for MANAGER in "${MANAGERS[@]}"; do
    _sysz_list "--$MANAGER" | sed -e "s/^/[$MANAGER] /"
  done | _sysz_sort
}

# main

# check fzf version
MIN_FZF=0.27.1
if [[ "$(printf '%s\n' "$MIN_FZF" "$(fzf --version | cut -d' ' -f1)" | sort -V | head -n1)" != "$MIN_FZF" ]]; then
  echo "ERROR: fzf >= $MIN_FZF required" >&2
  echo "https://github.com/junegunn/fzf#upgrading-fzf" >&2
  exit 1
fi

# root doesn't have user units
if [[ $EUID -eq 0 ]]; then
  MANAGERS=(system)
else
  MANAGERS=(user system)
fi

declare -a STATES
while [[ -n $1 ]]; do
  case $1 in
  -u | --user)
    MANAGERS=(user)
    shift
    ;;
  --sys | --system)
    MANAGERS=(system)
    shift
    ;;
  -s | --state)
    STATES+=("--state=$2")
    shift
    shift
    ;;
  --state=*)
    STATES+=("$1")
    shift
    ;;
  -v | --version)
    echo "$PROG" $SYSZ_VERSION
    exit 0
    ;;
  -V | --verbose)
    VERBOSE=true
    shift
    ;;
  -h | --help)
    _sysz_help
    ;;
  *)
    break
    ;;
  esac
done

for STATE in "${STATES[@]}"; do
  STATE="${STATE##*=}"
  if [[ -n $STATE ]] && ! systemctl --state=help | grep -q "^${STATE}$"; then
    echo "ERROR: Invalid state: $STATE" >&2
    exit 1
  fi
done

declare CMD
declare -a ARGS
while [[ -n $1 ]]; do
  case $1 in
  _fzf_preview)
    shift
    _fzf_preview "$@"
    ;;
  _fzf_cat)
    shift
    _fzf_cat "$@"
    ;;
  h | help)
    _sysz_help
    ;;
  # Handle short names
  re)
    CMD=restart
    ;;
  s)
    CMD=status
    ;;
  ed)
    CMD=edit
    ;;
  en)
    CMD=enable
    ;;
  d<