From 485283f2e531f5fa23a876fa52411e0030f32589 Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Wed, 7 Jul 2021 20:47:22 +0300 Subject: [PATCH 01/11] new type: __sed --- cdist/conf/type/__sed/explorer/file | 22 +++++++++ cdist/conf/type/__sed/gencode-remote | 46 +++++++++++++++++ cdist/conf/type/__sed/man.rst | 49 +++++++++++++++++++ cdist/conf/type/__sed/parameter/boolean | 1 + cdist/conf/type/__sed/parameter/optional | 1 + .../type/__sed/parameter/required_multiple | 1 + 6 files changed, 120 insertions(+) create mode 100755 cdist/conf/type/__sed/explorer/file create mode 100755 cdist/conf/type/__sed/gencode-remote create mode 100644 cdist/conf/type/__sed/man.rst create mode 100644 cdist/conf/type/__sed/parameter/boolean create mode 100644 cdist/conf/type/__sed/parameter/optional create mode 100644 cdist/conf/type/__sed/parameter/required_multiple diff --git a/cdist/conf/type/__sed/explorer/file b/cdist/conf/type/__sed/explorer/file new file mode 100755 index 00000000..a40f71b3 --- /dev/null +++ b/cdist/conf/type/__sed/explorer/file @@ -0,0 +1,22 @@ +#!/bin/sh -e + +if [ -f "$__object/parameter/file" ] +then + file="$( cat "$__object/parameter/file" )" +else + file="/$__object_id" +fi + +if [ -f "$file" ] +then + if [ -s "$file" ] + then + cat "$file" + else + echo "$file is empty" >&2 + exit 1 + fi +else + echo "$file do not exist" >&2 + exit 1 +fi diff --git a/cdist/conf/type/__sed/gencode-remote b/cdist/conf/type/__sed/gencode-remote new file mode 100755 index 00000000..391e3bc1 --- /dev/null +++ b/cdist/conf/type/__sed/gencode-remote @@ -0,0 +1,46 @@ +#!/bin/sh -e + +if [ -f "$__object/parameter/file" ] +then + file="$( cat "$__object/parameter/file" )" +else + file="/$__object_id" +fi + +script="$( cat "$__object/parameter/script" )" + +if [ "$script" = '-' ] +then + script="$( cat "$__object/stdin" )" +elif + [ -f "$script" ] +then + script="$( cat "$script" )" +fi + +file_from_target="$__object/explorer/file" + +sed_cmd='sed' + +if [ -f "$__object/parameter/regexp-extended" ] +then + sed_cmd="$sed_cmd --regexp-extended" +fi + +if ! echo "$script" \ + | "$sed_cmd" -f - "$file_from_target" \ + | diff "$file_from_target" - \ + > /dev/null +then + echo 'tmp="$( mktemp )"' + + echo "$sed_cmd -f - '$file' > \"\$tmp\" << EOF" + + echo "$script" + + echo 'EOF' + + echo "cp \"\$tmp\" '$file'" + + echo 'rm -f "$tmp"' +fi diff --git a/cdist/conf/type/__sed/man.rst b/cdist/conf/type/__sed/man.rst new file mode 100644 index 00000000..fbac212d --- /dev/null +++ b/cdist/conf/type/__sed/man.rst @@ -0,0 +1,49 @@ +cdist-type__sed(7) +================== + +NAME +---- +cdist-type__sed - Transform text files with ``sed`` + + +DESCRIPTION +----------- +TODO + + +REQUIRED MULTIPLE PARAMETERS +---------------------------- +script + TODO + + +OPTIONAL PARAMETERS +------------------- +file + TODO + + +BOOLEAN PARAMETERS +------------------ +regexp-extended + TODO + + +EXAMPLES +-------- +.. code-block:: sh + + true + + +AUTHORS +------- +Ander Punnar + + +COPYING +------- +Copyright \(C) 2021 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/cdist/conf/type/__sed/parameter/boolean b/cdist/conf/type/__sed/parameter/boolean new file mode 100644 index 00000000..1ad75c5d --- /dev/null +++ b/cdist/conf/type/__sed/parameter/boolean @@ -0,0 +1 @@ +regexp-extended diff --git a/cdist/conf/type/__sed/parameter/optional b/cdist/conf/type/__sed/parameter/optional new file mode 100644 index 00000000..f73f3093 --- /dev/null +++ b/cdist/conf/type/__sed/parameter/optional @@ -0,0 +1 @@ +file diff --git a/cdist/conf/type/__sed/parameter/required_multiple b/cdist/conf/type/__sed/parameter/required_multiple new file mode 100644 index 00000000..84f7e31d --- /dev/null +++ b/cdist/conf/type/__sed/parameter/required_multiple @@ -0,0 +1 @@ +script From 7a5896acfad0f992d6e7fe9cbe0bd08035ce19db Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Wed, 7 Jul 2021 21:23:25 +0300 Subject: [PATCH 02/11] add --onchange, fix shellcheck --- cdist/conf/type/__sed/gencode-remote | 9 ++++++++- cdist/conf/type/__sed/man.rst | 3 +++ cdist/conf/type/__sed/parameter/optional | 1 + 3 files changed, 12 insertions(+), 1 deletion(-) diff --git a/cdist/conf/type/__sed/gencode-remote b/cdist/conf/type/__sed/gencode-remote index 391e3bc1..6b1bd216 100755 --- a/cdist/conf/type/__sed/gencode-remote +++ b/cdist/conf/type/__sed/gencode-remote @@ -32,6 +32,7 @@ if ! echo "$script" \ | diff "$file_from_target" - \ > /dev/null then + # shellcheck disable=SC2016 echo 'tmp="$( mktemp )"' echo "$sed_cmd -f - '$file' > \"\$tmp\" << EOF" @@ -41,6 +42,12 @@ then echo 'EOF' echo "cp \"\$tmp\" '$file'" - + + # shellcheck disable=SC2016 echo 'rm -f "$tmp"' + + if [ -f "$__object/parameter/onchange" ] + then + cat "$__object/parameter/onchange" + fi fi diff --git a/cdist/conf/type/__sed/man.rst b/cdist/conf/type/__sed/man.rst index fbac212d..4a728184 100644 --- a/cdist/conf/type/__sed/man.rst +++ b/cdist/conf/type/__sed/man.rst @@ -22,6 +22,9 @@ OPTIONAL PARAMETERS file TODO +onchange + TODO + BOOLEAN PARAMETERS ------------------ diff --git a/cdist/conf/type/__sed/parameter/optional b/cdist/conf/type/__sed/parameter/optional index f73f3093..fa86f917 100644 --- a/cdist/conf/type/__sed/parameter/optional +++ b/cdist/conf/type/__sed/parameter/optional @@ -1 +1,2 @@ file +onchange From cf0032d667eb4bd8b7fe1bebacec341bbf71ed42 Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Wed, 7 Jul 2021 21:28:00 +0300 Subject: [PATCH 03/11] add messaging and exit earlier --- cdist/conf/type/__sed/gencode-remote | 42 +++++++++++++++------------- 1 file changed, 23 insertions(+), 19 deletions(-) diff --git a/cdist/conf/type/__sed/gencode-remote b/cdist/conf/type/__sed/gencode-remote index 6b1bd216..836506f0 100755 --- a/cdist/conf/type/__sed/gencode-remote +++ b/cdist/conf/type/__sed/gencode-remote @@ -27,27 +27,31 @@ then sed_cmd="$sed_cmd --regexp-extended" fi -if ! echo "$script" \ +if echo "$script" \ | "$sed_cmd" -f - "$file_from_target" \ | diff "$file_from_target" - \ > /dev/null then - # shellcheck disable=SC2016 - echo 'tmp="$( mktemp )"' - - echo "$sed_cmd -f - '$file' > \"\$tmp\" << EOF" - - echo "$script" - - echo 'EOF' - - echo "cp \"\$tmp\" '$file'" - - # shellcheck disable=SC2016 - echo 'rm -f "$tmp"' - - if [ -f "$__object/parameter/onchange" ] - then - cat "$__object/parameter/onchange" - fi + exit 0 +fi + +# shellcheck disable=SC2016 +echo 'tmp="$( mktemp )"' + +echo "$sed_cmd -f - '$file' > \"\$tmp\" << EOF" + +echo "$script" + +echo 'EOF' + +echo "cp \"\$tmp\" '$file'" + +# shellcheck disable=SC2016 +echo 'rm -f "$tmp"' + +echo 'change' >> "$__messages_out" + +if [ -f "$__object/parameter/onchange" ] +then + cat "$__object/parameter/onchange" fi From d7fdc8006f747be86b64a2790266b50219bab507 Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Tue, 14 Sep 2021 21:54:45 +0300 Subject: [PATCH 04/11] allow empty file --- cdist/conf/type/__sed/explorer/file | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/cdist/conf/type/__sed/explorer/file b/cdist/conf/type/__sed/explorer/file index a40f71b3..5f0f4edd 100755 --- a/cdist/conf/type/__sed/explorer/file +++ b/cdist/conf/type/__sed/explorer/file @@ -9,13 +9,7 @@ fi if [ -f "$file" ] then - if [ -s "$file" ] - then - cat "$file" - else - echo "$file is empty" >&2 - exit 1 - fi + cat "$file" else echo "$file do not exist" >&2 exit 1 From 0f6e48dbc657a9d9145b965eacf79f021a1419f5 Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Tue, 14 Sep 2021 22:24:26 +0300 Subject: [PATCH 05/11] use $__object/tempfile in target instead of mktemp, add comments --- cdist/conf/type/__sed/gencode-remote | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cdist/conf/type/__sed/gencode-remote b/cdist/conf/type/__sed/gencode-remote index 836506f0..df929b42 100755 --- a/cdist/conf/type/__sed/gencode-remote +++ b/cdist/conf/type/__sed/gencode-remote @@ -35,8 +35,11 @@ then exit 0 fi +# we can't use -i, because it's not posix, so we fly with tempfile and cp +# and we use cp because we want to preserve destination file's attributes + # shellcheck disable=SC2016 -echo 'tmp="$( mktemp )"' +echo 'tmp="$__object/tempfile"' echo "$sed_cmd -f - '$file' > \"\$tmp\" << EOF" From 90488fcebcc00f3b4ea260867f8b31625df61e46 Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Tue, 14 Sep 2021 22:27:42 +0300 Subject: [PATCH 06/11] use -e --- cdist/conf/type/__sed/explorer/file | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cdist/conf/type/__sed/explorer/file b/cdist/conf/type/__sed/explorer/file index 5f0f4edd..9262b613 100755 --- a/cdist/conf/type/__sed/explorer/file +++ b/cdist/conf/type/__sed/explorer/file @@ -7,10 +7,10 @@ else file="/$__object_id" fi -if [ -f "$file" ] +if [ ! -e "$file" ] then - cat "$file" -else echo "$file do not exist" >&2 exit 1 fi + +cat "$file" From b7f392fa371ff78591abb910f7e224e7587c81be Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Tue, 14 Sep 2021 22:38:55 +0300 Subject: [PATCH 07/11] use -E for better compat (not really sure if it is posix at all) --- cdist/conf/type/__sed/gencode-remote | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdist/conf/type/__sed/gencode-remote b/cdist/conf/type/__sed/gencode-remote index df929b42..625448e6 100755 --- a/cdist/conf/type/__sed/gencode-remote +++ b/cdist/conf/type/__sed/gencode-remote @@ -24,7 +24,7 @@ sed_cmd='sed' if [ -f "$__object/parameter/regexp-extended" ] then - sed_cmd="$sed_cmd --regexp-extended" + sed_cmd="$sed_cmd -E" fi if echo "$script" \ From aabef7f44a98ad144a7e1fe68bfb562079916ef8 Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Tue, 14 Sep 2021 22:40:06 +0300 Subject: [PATCH 08/11] remove reading script from file --- cdist/conf/type/__sed/gencode-remote | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cdist/conf/type/__sed/gencode-remote b/cdist/conf/type/__sed/gencode-remote index 625448e6..be7690e1 100755 --- a/cdist/conf/type/__sed/gencode-remote +++ b/cdist/conf/type/__sed/gencode-remote @@ -12,10 +12,6 @@ script="$( cat "$__object/parameter/script" )" if [ "$script" = '-' ] then script="$( cat "$__object/stdin" )" -elif - [ -f "$script" ] -then - script="$( cat "$script" )" fi file_from_target="$__object/explorer/file" From 5bf0c71e7af49ff6e7c1cccec192f763a019503e Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Tue, 14 Sep 2021 22:45:36 +0300 Subject: [PATCH 09/11] update man --- cdist/conf/type/__sed/man.rst | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/cdist/conf/type/__sed/man.rst b/cdist/conf/type/__sed/man.rst index 4a728184..86789363 100644 --- a/cdist/conf/type/__sed/man.rst +++ b/cdist/conf/type/__sed/man.rst @@ -8,35 +8,40 @@ cdist-type__sed - Transform text files with ``sed`` DESCRIPTION ----------- -TODO +Transform text files with ``sed``. REQUIRED MULTIPLE PARAMETERS ---------------------------- script - TODO + ``sed`` script. + If ``-`` then the script is read from ``stdin``. OPTIONAL PARAMETERS ------------------- file - TODO + Path to the file. Defaults to ``$__object_id``. onchange - TODO + Execute this command if ``sed`` changes file. BOOLEAN PARAMETERS ------------------ regexp-extended - TODO + Use extended regular expressions in the script. + Might not be supported with every ``sed`` version. EXAMPLES -------- + .. code-block:: sh - true + __sed /tmp/foobar --script 's/foo/bar/' + + echo 's/foo/bar/' | __sed foobar --file /tmp/foobar --script - AUTHORS From cd4acde67ec9c3967e6e08450734cf65040e787f Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Wed, 15 Sep 2021 09:22:27 +0300 Subject: [PATCH 10/11] grammar --- cdist/conf/type/__sed/explorer/file | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cdist/conf/type/__sed/explorer/file b/cdist/conf/type/__sed/explorer/file index 9262b613..ec3d0fe8 100755 --- a/cdist/conf/type/__sed/explorer/file +++ b/cdist/conf/type/__sed/explorer/file @@ -9,7 +9,7 @@ fi if [ ! -e "$file" ] then - echo "$file do not exist" >&2 + echo "$file does not exist" >&2 exit 1 fi From 72ff48154c958ebdedc8b6d919b6f372efa63be2 Mon Sep 17 00:00:00 2001 From: Ander Punnar Date: Thu, 16 Sep 2021 21:36:39 +0300 Subject: [PATCH 11/11] add comments, add -u to diff --- cdist/conf/type/__sed/gencode-remote | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/cdist/conf/type/__sed/gencode-remote b/cdist/conf/type/__sed/gencode-remote index be7690e1..f99c5a88 100755 --- a/cdist/conf/type/__sed/gencode-remote +++ b/cdist/conf/type/__sed/gencode-remote @@ -14,6 +14,8 @@ then script="$( cat "$__object/stdin" )" fi +# since stdin is not available in explorer, we pull file from target with explorer + file_from_target="$__object/explorer/file" sed_cmd='sed' @@ -23,10 +25,10 @@ then sed_cmd="$sed_cmd -E" fi -if echo "$script" \ - | "$sed_cmd" -f - "$file_from_target" \ - | diff "$file_from_target" - \ - > /dev/null +# do sed dry run, diff result and if no change, then there's nothing to do +# also redirect diff's output to stderr for debugging purposes + +if echo "$script" | "$sed_cmd" -f - "$file_from_target" | diff -u "$file_from_target" - >&2 then exit 0 fi