[__sensible_editor] Add support for RedHat derivatives

They have added sensible-utils lately.
select-editor(1) does not work because their alternatives system is broken,
but sensible-editor(1) works just fine, so we can support it.
This commit is contained in:
Dennis Camera 2019-10-08 23:05:23 +02:00
parent 9a6ca1a343
commit b6898b097f
3 changed files with 78 additions and 39 deletions

View file

@ -22,76 +22,92 @@
# absolute path. # 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") case $("${__explorer}/os")
in in
debian|devuan|ubuntu) debian|devuan|ubuntu)
: # supported has_alternatives=true
editors=$(update-alternatives --list editor)
;; ;;
*) *)
exit 0 # will produce an error message in the manifest # 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 esac
editor=$(cat "${__object}/parameter/editor") editor=$(cat "${__object}/parameter/editor")
editors=$(update-alternatives --list editor)
if test $(echo "${editors}" | wc -l) -lt 1
then
echo 'No editors have been found on this system.' >&2
exit 1
fi
case $editor case $editor
in in
/*) /*)
is_path=true is_abspath=true
;; ;;
*/*) */*)
echo 'Relative editor paths are not supported' >&2 die 'Relative editor paths are not supported'
exit 1
;; ;;
*) *)
is_path=false is_abspath=false
;; ;;
esac esac
IFS=' if $has_alternatives && test "$(echo "${editors}" | wc -l)" -gt 0
'
if $is_path
then then
if ! test -f "${editor}" IFS='
'
if ! $is_abspath
then then
echo "Editor ${editor} is missing on the target system." >&2 # First, try to resolve the absolute path using $editors.
exit 1 for e in $editors
do
if test "$(basename "${e}")" = "${editor}"
then
editor="${e}"
break
fi
done
fi fi
# Check if path is present
test -f "${editor}" || editor_missing "${editor}"
for e in $editors for e in $editors
do do
if test "${editor}" = "${e}" if test "${editor}" = "${e}"
then then
# Editor is present and part of the alternatives list -> use it! # Editor is part of the alternatives list -> use it!
echo "${editor}" echo "${editor}"
exit 0 exit 0
fi fi
done done
echo "Editor ${editor} is not in the alternatives list of the target system." >&2 editor_no_alternative "${editor}"
exit 1
else else
for e in $editors # NOTE: This branch is mostly for RedHat-based systems which do
do # not track editor alternatives. To make this type useful
if test "$(basename "${e}")" = "${editor}" # on RedHat at all we allow an absoloute path to be provided
then # in any case.
# Editor could be found by basename in the alternatives list -> use it!
echo "${e}"
exit 0
fi
done
echo "Editor ${editor} is missing on the target system." >&2 if $is_abspath
exit 1 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 fi
# The script should never reach this statement!
exit 1 exit 1

View file

@ -8,14 +8,15 @@ cdist-type__sensible_editor - Select the sensible-editor
DESCRIPTION DESCRIPTION
----------- -----------
This cdist type allows you to select the sensible-editor on Debian-based systems This cdist type allows you to select the :strong:`sensible-editor` for
for a given user. a given user.
REQUIRED PARAMETERS REQUIRED PARAMETERS
------------------- -------------------
editor editor
Name or path of the editor to be selected. Name or path of the editor to be selected.
On systems other than Debian derivatives an absolute path is required.
OPTIONAL PARAMETERS OPTIONAL PARAMETERS
@ -33,9 +34,17 @@ EXAMPLES
__sensible_editor noob --editor nano __sensible_editor noob --editor nano
LIMITATIONS
-----------
This type only works on operating systems on which the sensible-utils package
is available.
Hint: On RedHat-based systems setting up the EPEL repo might be necessary.
SEE ALSO SEE ALSO
-------- --------
none :strong:`select-editor`\ (1), :strong:`sensible-editor`\ (1).
AUTHOR AUTHOR

View file

@ -27,14 +27,28 @@ user=$__object_id
case $os case $os
in in
debian|devuan|ubuntu) debian|devuan|ubuntu)
test "${state}" = 'present' && __package_apt sensible-utils --state present test "${state}" != 'absent' \
&& __package sensible-utils --state present --type apt
;;
centos|fedora|redhat|scientific)
test "${state}" != 'absent' \
&& __package sensible-utils --state present --type yum
;; ;;
*) *)
echo "OS ${os} does not support select-editor." >&2 echo "OS ${os} does not support sensible-editor." >&2
echo "If it does, please provide a patch." >&2
exit 1 exit 1
;; ;;
esac esac
if test "${state}" != 'present' && test "${state}" != 'absent'
then
echo 'Only "present" and "absent" are allowed for --state' >&2
exit 1
fi
test "${state}" = 'absent' || export __require='__package/sensible-utils'
editor_path=$(cat "${__object}/explorer/editor_path") editor_path=$(cat "${__object}/explorer/editor_path")
user_home=$(cat "${__object}/explorer/user_home") user_home=$(cat "${__object}/explorer/user_home")
group=$(cat "${__object}/explorer/group") group=$(cat "${__object}/explorer/group")