cdist/cdist/conf/type/__sensible_editor/explorer/editor_path

114 lines
2.5 KiB
Bash

#!/bin/sh -e
#
# 2019 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
#
# Check if the given editor is present on the target system and determine its
# absolute path.
#
die() {
echo "$@" >&2
exit 1
}
editor_missing() { die "Editor '$1' is missing on the target system."; }
editor_no_alternative() { die "Editor '$1' is not in the alternatives list of the target system."; }
case $("${__explorer}/os")
in
debian|devuan|ubuntu)
has_alternatives=true
editors=$(update-alternatives --list editor)
;;
*)
# NOTE: RedHat has an alternatives system but it doesn't usually track
# editors and it is a pain to extract the list.
has_alternatives=false
;;
esac
editor=$(cat "${__object}/parameter/editor")
case $editor
in
/*)
is_abspath=true
;;
*/*)
die 'Relative editor paths are not supported'
;;
*)
is_abspath=false
;;
esac
if $has_alternatives && test "$(echo "${editors}" | wc -l)" -gt 0
then
IFS='
'
if ! $is_abspath
then
# First, try to resolve the absolute path using $editors.
for e in $editors
do
if test "$(basename "${e}")" = "${editor}"
then
editor="${e}"
break
fi
done
fi
# Check if path is present
test -f "${editor}" || editor_missing "${editor}"
for e in $editors
do
if test "${editor}" = "${e}"
then
# Editor is part of the alternatives list -> use it!
echo "${editor}"
exit 0
fi
done
editor_no_alternative "${editor}"
else
# NOTE: This branch is mostly for RedHat-based systems which do
# not track editor alternatives. To make this type useful
# on RedHat at all we allow an absoloute path to be provided
# in any case.
if $is_abspath
then
test -x "${editor}" || editor_missing "${editor}"
echo "${editor}"
exit 0
else
die "The target doesn't list any editor alternatives. " \
"Please specify an absolute path or populate the alternatives list."
fi
fi
# The script should never reach this statement!
exit 1