From 082755816024bb0e93f505dd7a91e41fda6b5f01 Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Tue, 28 Apr 2020 01:32:40 +0300 Subject: [PATCH] new type: __find_exec --- type/__find_exec/explorer/find | 41 +++++++++++ type/__find_exec/gencode-remote | 66 +++++++++++++++++ type/__find_exec/man.rst | 74 ++++++++++++++++++++ type/__find_exec/parameter/optional | 2 + type/__find_exec/parameter/optional_multiple | 2 + type/__find_exec/parameter/required_multiple | 1 + 6 files changed, 186 insertions(+) create mode 100755 type/__find_exec/explorer/find create mode 100755 type/__find_exec/gencode-remote create mode 100644 type/__find_exec/man.rst create mode 100644 type/__find_exec/parameter/optional create mode 100644 type/__find_exec/parameter/optional_multiple create mode 100644 type/__find_exec/parameter/required_multiple diff --git a/type/__find_exec/explorer/find b/type/__find_exec/explorer/find new file mode 100755 index 0000000..9aa8a9a --- /dev/null +++ b/type/__find_exec/explorer/find @@ -0,0 +1,41 @@ +#!/bin/sh -e +# +# 2020 Ander Punnar (ander-at-kvlt-dot-ee) +# +# 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 . +# + +if [ -f "$__object/parameter/path" ] +then + path="$( cat "$__object/parameter/path" )" +else + path="/$__object_id" +fi + +if [ ! -e "$path" ] +then + exit 0 +fi + +set -- find "$path" + +while read -r l +do + set -- "$@" "$l" +done \ + < "$__object/parameter/exp" + +eval "$@" diff --git a/type/__find_exec/gencode-remote b/type/__find_exec/gencode-remote new file mode 100755 index 0000000..1fda51c --- /dev/null +++ b/type/__find_exec/gencode-remote @@ -0,0 +1,66 @@ +#!/bin/sh -e +# +# 2020 Ander Punnar (ander-at-kvlt-dot-ee) +# +# 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 . +# + +if [ ! -s "$__object/explorer/find" ] +then + exit 0 +fi + +if [ ! -f "$__object/parameter/exec" ] \ + && [ ! -f "$__object/parameter/execdir" ] +then + echo '-exec or -execdir must be used' >&2 + exit 1 +fi + +if [ -f "$__object/parameter/path" ] +then + path="$( cat "$__object/parameter/path" )" +else + path="/$__object_id" +fi + +set -- find "$path" + +while read -r l +do + set -- "$@" "$l" +done \ + < "$__object/parameter/exp" + +for p in exec execdir +do + if [ -f "$__object/parameter/$p" ] + then + while read -r l + do + set -- "$@" "-$p $l" + echo "$p: $l" >> "$__messages_out" + done \ + < "$__object/parameter/$p" + fi +done + +echo "$@" + +if [ -f "$__object/parameter/onchange" ] +then + cat "$__object/parameter/onchange" +fi diff --git a/type/__find_exec/man.rst b/type/__find_exec/man.rst new file mode 100644 index 0000000..ddf13cc --- /dev/null +++ b/type/__find_exec/man.rst @@ -0,0 +1,74 @@ +cdist-type__find_exec(7) +======================== + +NAME +---- +cdist-type__find_exec - Find files and execute commands on them + + +DESCRIPTION +----------- +``find`` in explorer will only run with expressions given with ``--exp`` parameter. + +Code will be generated only, if explorer produces output. + +For code generation ``--exec`` and/or ``--execdir`` parameters are used. + +Please see your OS ``find`` manpage for details. + + +REQUIRED MULTIPLE PARAMETERS +---------------------------- +exp + Expression to add to ``find`` command after starting point. + DO NOT add ``-exec`` or ``-execdir`` here. + + +OPTIONAL MULTIPLE PARAMETERS +---------------------------- +exec + See ``find`` manpage about ``-exec``. + +execdir + See ``find`` manpage about ``-execdir``. + + +OPTIONAL PARAMETERS +------------------- +path + Use this path instead of object id. + +onchange + The code to run if something happens. + + +EXAMPLES +-------- + +.. code-block:: sh + + # find *.list files under /etc/apt/sources.list.d, delete them and update apt cache + __find_exec /etc/apt/sources.list.d \ + --exp '-name "*.list"' \ + --exec 'rm "{}" \;' \ + --onchange 'apt-get update || true' + + # find files with execute bit under /some/path and remove execute bit + __find_exec remove-exec-bit \ + --path /some/path \ + --exp '-type f' \ + --exp '-perm /111' \ + --exec 'chmod -x "{}" \;' + + +AUTHORS +------- +Ander Punnar + + +COPYING +------- +Copyright \(C) 2020 Ander Punnar. 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/type/__find_exec/parameter/optional b/type/__find_exec/parameter/optional new file mode 100644 index 0000000..a063581 --- /dev/null +++ b/type/__find_exec/parameter/optional @@ -0,0 +1,2 @@ +path +onchange diff --git a/type/__find_exec/parameter/optional_multiple b/type/__find_exec/parameter/optional_multiple new file mode 100644 index 0000000..23962c7 --- /dev/null +++ b/type/__find_exec/parameter/optional_multiple @@ -0,0 +1,2 @@ +exec +execdir diff --git a/type/__find_exec/parameter/required_multiple b/type/__find_exec/parameter/required_multiple new file mode 100644 index 0000000..0f25033 --- /dev/null +++ b/type/__find_exec/parameter/required_multiple @@ -0,0 +1 @@ +exp