From 44170123a09b9767fc4a185398b81fa93f3c5446 Mon Sep 17 00:00:00 2001 From: Anton Bobov Date: Sat, 7 Sep 2024 12:17:07 +0500 Subject: Add systemd unit edit script --- sce | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100755 sce diff --git a/sce b/sce new file mode 100755 index 0000000..c1f7659 --- /dev/null +++ b/sce @@ -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 "$@" -- cgit v1.2.3