From b6898b097f01941d68860c02118f5f5f2ddb8576 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Tue, 8 Oct 2019 23:05:23 +0200 Subject: [PATCH] [__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. --- .../__sensible_editor/explorer/editor_path | 84 +++++++++++-------- cdist/conf/type/__sensible_editor/man.rst | 15 +++- cdist/conf/type/__sensible_editor/manifest | 18 +++- 3 files changed, 78 insertions(+), 39 deletions(-) diff --git a/cdist/conf/type/__sensible_editor/explorer/editor_path b/cdist/conf/type/__sensible_editor/explorer/editor_path index a1673a52..8b10b17b 100644 --- a/cdist/conf/type/__sensible_editor/explorer/editor_path +++ b/cdist/conf/type/__sensible_editor/explorer/editor_path @@ -22,76 +22,92 @@ # 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) - : # 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 - 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 in /*) - is_path=true + is_abspath=true ;; */*) - echo 'Relative editor paths are not supported' >&2 - exit 1 + die 'Relative editor paths are not supported' ;; *) - is_path=false + is_abspath=false ;; esac -IFS=' -' -if $is_path +if $has_alternatives && test "$(echo "${editors}" | wc -l)" -gt 0 then - if ! test -f "${editor}" + IFS=' +' + if ! $is_abspath then - echo "Editor ${editor} is missing on the target system." >&2 - exit 1 + # 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 present and part of the alternatives list -> use it! + # Editor is part of the alternatives list -> use it! echo "${editor}" exit 0 fi done - echo "Editor ${editor} is not in the alternatives list of the target system." >&2 - exit 1 + editor_no_alternative "${editor}" else - for e in $editors - do - if test "$(basename "${e}")" = "${editor}" - then - # Editor could be found by basename in the alternatives list -> use it! - echo "${e}" - exit 0 - fi - done + # 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. - echo "Editor ${editor} is missing on the target system." >&2 - exit 1 + 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 diff --git a/cdist/conf/type/__sensible_editor/man.rst b/cdist/conf/type/__sensible_editor/man.rst index 2d7682a6..9e2245ef 100644 --- a/cdist/conf/type/__sensible_editor/man.rst +++ b/cdist/conf/type/__sensible_editor/man.rst @@ -8,14 +8,15 @@ cdist-type__sensible_editor - Select the sensible-editor DESCRIPTION ----------- -This cdist type allows you to select the sensible-editor on Debian-based systems -for a given user. +This cdist type allows you to select the :strong:`sensible-editor` for +a given user. REQUIRED PARAMETERS ------------------- editor Name or path of the editor to be selected. + On systems other than Debian derivatives an absolute path is required. OPTIONAL PARAMETERS @@ -33,9 +34,17 @@ EXAMPLES __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 -------- -none +:strong:`select-editor`\ (1), :strong:`sensible-editor`\ (1). AUTHOR diff --git a/cdist/conf/type/__sensible_editor/manifest b/cdist/conf/type/__sensible_editor/manifest index b02625e2..56a6b02f 100644 --- a/cdist/conf/type/__sensible_editor/manifest +++ b/cdist/conf/type/__sensible_editor/manifest @@ -27,14 +27,28 @@ user=$__object_id case $os in 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 ;; 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") user_home=$(cat "${__object}/explorer/user_home") group=$(cat "${__object}/explorer/group")