summaryrefslogtreecommitdiff
path: root/sce
blob: 53f92f67e8461ef8693e82d2e109caa4376fbff2 (plain) (blame)
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
#!/usr/bin/env bash
# Open given systemd unit in editor.

set -euo pipefail

usage() {
  cat <<EOF >&2
Usage: ${0##*/} [OPTIONS] unit-name

OPTIONS is systemctl options.  User --user for user service.
EOF
  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 "$@"