diff options
| author | Anton Bobov <anton@bobov.name> | 2024-09-07 12:17:07 +0500 |
|---|---|---|
| committer | Anton Bobov <anton@bobov.name> | 2024-09-07 12:17:49 +0500 |
| commit | 44170123a09b9767fc4a185398b81fa93f3c5446 (patch) | |
| tree | d6e935e81d991060ec98383fd6ab14c8d987b3cb | |
| parent | 8a19d8bb7a9245d7bd699fd939b8c9302b22eb93 (diff) | |
Add systemd unit edit script
| -rwxr-xr-x | sce | 35 |
1 files changed, 35 insertions, 0 deletions
@@ -0,0 +1,35 @@ +#!/usr/bin/env bash +# Open given systemd unit in editor. + +set -euo pipefail + +usage() { + echo "Usage: ${0##*/} [OPTIONS] unit-name" >&2 + exit 1 +} + +get_unit_path() { + path=$(systemctl show -P FragmentPath "$@") + if [ -z "$path" ]; then + echo "Unit file not found" >&2 + exit 2 + fi + echo "$path" +} + +is_user_unit() { + [ "${1##"$HOME"/}" == "$1" ] +} + +main() { + [ $# -eq 0 ] && usage + + path=$(get_unit_path "$@") + if is_user_unit "$path"; then + sudo -e "$path" + else + "${EDITOR:-vim}" "$path" + fi +} + +main "$@" |
