From 644768cc02a4bf0a58a9ae83d3aaa3a318f5f9c8 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Wed, 2 Oct 2019 15:37:51 +0200 Subject: [PATCH] Add __select_editor type --- .../type/__select_editor/explorer/editor_path | 97 +++++++++++++++++++ .../conf/type/__select_editor/explorer/group | 26 +++++ .../type/__select_editor/explorer/user_home | 26 +++++ cdist/conf/type/__select_editor/man.rst | 51 ++++++++++ cdist/conf/type/__select_editor/manifest | 59 +++++++++++ .../__select_editor/parameter/default/state | 1 + .../type/__select_editor/parameter/optional | 1 + .../type/__select_editor/parameter/required | 1 + 8 files changed, 262 insertions(+) create mode 100644 cdist/conf/type/__select_editor/explorer/editor_path create mode 100644 cdist/conf/type/__select_editor/explorer/group create mode 100644 cdist/conf/type/__select_editor/explorer/user_home create mode 100644 cdist/conf/type/__select_editor/man.rst create mode 100644 cdist/conf/type/__select_editor/manifest create mode 100644 cdist/conf/type/__select_editor/parameter/default/state create mode 100644 cdist/conf/type/__select_editor/parameter/optional create mode 100644 cdist/conf/type/__select_editor/parameter/required diff --git a/cdist/conf/type/__select_editor/explorer/editor_path b/cdist/conf/type/__select_editor/explorer/editor_path new file mode 100644 index 00000000..88952e23 --- /dev/null +++ b/cdist/conf/type/__select_editor/explorer/editor_path @@ -0,0 +1,97 @@ +#!/bin/sh +# +# 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 . +# +# +# Check if the given editor is present on the target system and determine its +# absolute path. +# + +case $("${__explorer}/os") +in + debian|devuan|ubuntu) + : # supported + ;; + *) + exit 0 # will produce an error message in the manifest + ;; +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 + ;; + */*) + echo 'Relative editor paths are not supported' >&2 + exit 1 + ;; + *) + is_path=false + ;; +esac + + +IFS=' +' +if $is_path +then + if ! test -f "${editor}" + then + echo "Editor ${editor} is missing on the target system." >&2 + exit 1 + fi + + for e in $editors + do + if test "${editor}" = "${e}" + then + # Editor is present and 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 +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 + + echo "Editor ${editor} is missing on the target system." >&2 + exit 1 +fi + +exit 1 diff --git a/cdist/conf/type/__select_editor/explorer/group b/cdist/conf/type/__select_editor/explorer/group new file mode 100644 index 00000000..5d288189 --- /dev/null +++ b/cdist/conf/type/__select_editor/explorer/group @@ -0,0 +1,26 @@ +#!/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 . +# +# +# Determines the primary group of the user. +# + +user=$__object_id + +id -gn "${user}" 2>/dev/null diff --git a/cdist/conf/type/__select_editor/explorer/user_home b/cdist/conf/type/__select_editor/explorer/user_home new file mode 100644 index 00000000..dc1725a0 --- /dev/null +++ b/cdist/conf/type/__select_editor/explorer/user_home @@ -0,0 +1,26 @@ +#!/bin/sh +# +# 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 . +# +# +# Determines the home folder of the target user. +# + +user=$__object_id + +getent passwd "${user}" | cut -d':' -f6 diff --git a/cdist/conf/type/__select_editor/man.rst b/cdist/conf/type/__select_editor/man.rst new file mode 100644 index 00000000..98e5ef81 --- /dev/null +++ b/cdist/conf/type/__select_editor/man.rst @@ -0,0 +1,51 @@ +cdist-type__select_editor(7) +============================ + +NAME +---- +cdist-type__select_editor - Select the sensible-editor + + +DESCRIPTION +----------- +This cdist type allows you to select the sensible-editor on Debian-based systems +for a given user. + + +REQUIRED PARAMETERS +------------------- +editor + Name or path of the editor to be selected. + + +OPTIONAL PARAMETERS +------------------- +state + either "present" or "absent". Defaults to "present". + + +EXAMPLES +-------- + +.. code-block:: sh + + __select_editor root --editor /bin/ed # ed(1) is the standard + __select_editor noob --editor nano + + +SEE ALSO +-------- +none + + +AUTHOR +------- +Dennis Camera + + +COPYING +------- +Copyright \(C) 2019 Dennis Camera. +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. diff --git a/cdist/conf/type/__select_editor/manifest b/cdist/conf/type/__select_editor/manifest new file mode 100644 index 00000000..5ed97533 --- /dev/null +++ b/cdist/conf/type/__select_editor/manifest @@ -0,0 +1,59 @@ +#!/bin/sh -e +# -*- mode: sh; indent-tabs-mode: t -*- +# +# 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 . +# + +os=$(cat "${__global}/explorer/os") + +state=$(cat "${__object}/parameter/state") +user=$__object_id + +editor_path=$(cat "${__object}/explorer/editor_path") +user_home=$(cat "${__object}/explorer/user_home") +group=$(cat "${__object}/explorer/group") + +case $os +in + debian|devuan|ubuntu) + test "${state}" = 'present' && __package_apt sensible-utils --state present + ;; + *) + echo "OS ${os} does not support select-editor." >&2 + exit 1 + ;; +esac + +if test -z "${user_home}" +then + echo "Could not find ${user}'s home directory." >&2 + exit 1 +fi + +if test -z "${editor_path}" +then + echo "Editor \"$(cat "${__object}/parameter/editor")\" is missing on the target system." >&2 + exit 1 +fi + +__file "${user_home}/.selected_editor" --state "${state}" \ + --owner "${user}" --group "${group}" --mode 0644 \ + --source - <