From b76d8485404b6f023cd6acf7022d95459e3c3704 Mon Sep 17 00:00:00 2001 From: Matthias Stecher Date: Thu, 11 Mar 2021 21:50:55 +0100 Subject: [PATCH] __ini_value: add delimiter space detection This adds spaces around the delimiter to provide some flexibility than just do it in the delimiter string directly. It can be set via the parameter `--delimiter-space`. --- cdist/conf/type/__ini_value/explorer/state | 14 +++++++--- cdist/conf/type/__ini_value/files/common.awk | 26 ++++++++++--------- cdist/conf/type/__ini_value/man.rst | 8 ++++++ .../conf/type/__ini_value/parameter/optional | 1 + 4 files changed, 34 insertions(+), 15 deletions(-) diff --git a/cdist/conf/type/__ini_value/explorer/state b/cdist/conf/type/__ini_value/explorer/state index 5f98f869..e387d104 100755 --- a/cdist/conf/type/__ini_value/explorer/state +++ b/cdist/conf/type/__ini_value/explorer/state @@ -48,6 +48,7 @@ BEGIN { getline delimiter < (_param "delimiter") getline value < (_param "value") getline indentation < (_param "indentation") + getline delimiter_space < (_param "delimiter-space") do_normalization = (system("test -f " (_param "normalize")) == 0) @@ -108,10 +109,9 @@ found_section { # must only contain spaces else start_spaces = ikey - 1 } - # start_spaces set or 0 idelspace_start = ikey + length(key) - idelspace_length = idel - (idelspace_start) + idelspace_length = idel - idelspace_start # check for delimiter is only preceded with spaces if(idelspace_length == 0 || check_spaces(substr(line, idelspace_start, idelspace_length))) { found = 1 @@ -129,8 +129,16 @@ found_section { else { # check if the format is important if(do_normalization) { + if(match(found_value, /^[ \t]+/) == 1) { + found_value = substr(found_value, 1 + RLENGTH) + del_val_spacelen = RLENGTH + } + else + del_val_spacelen = 0 + # the format must exactly match, else it is incorrect - if(indentation != start_spaces || idelspace_length != 0 || found_value != is_value) + if(start_spaces != indentation || found_value != is_value || + idelspace_length != delimiter_space || del_val_spacelen != delimiter_space) state("wrongformat") } diff --git a/cdist/conf/type/__ini_value/files/common.awk b/cdist/conf/type/__ini_value/files/common.awk index 8874f0b8..7611638c 100644 --- a/cdist/conf/type/__ini_value/files/common.awk +++ b/cdist/conf/type/__ini_value/files/common.awk @@ -6,9 +6,14 @@ BEGIN { value = get_param_string("value") comment = get_param_string("comment") indentation = get_param_string("indentation") + delimiter_space = get_param_string("delimiter-space") get_param_array("comment-sign", comment_signs) comment_sign = comment_signs[0] + + base_spaces = spaces(indentation) + delimiter_spaces = spaces(delimiter_space + delimiter_w_spaces = (delimiter_spaces delimiter delimiter_spaces) } function trim(var) { @@ -16,6 +21,12 @@ function trim(var) { sub(/[ \t]*$/, "", var) return var } +function spaces(a) { + rspaces = "" + for(b = 0; b < a; b++) + rspaces = (rspaces " ") + return rspaces +} function check_spaces(part) { return match(part, /^[ \t]*$/) == 1 } @@ -40,22 +51,13 @@ function get_param_array(name, arr) { # print value function v_print() { - spaces = "" - for(i = 0; i < indentation; i++) - spaces = (spaces " ") - printf "%s%s%s%s%s", spaces, key, delimiter, value, ORS + printf "%s%s%s%s%s", base_spaces, key, delimiter_w_spaces, value, ORS } # print commented value function v_print_commented() { - spaces = "" - for(i = 0; i < indentation; i++) - spaces = (spaces " ") - printf "%s%s%s%s%s%s", spaces, comment_sign, key, delimiter, value, ORS + printf "%s%s%s%s%s%s", base_spaces, comment_sign, key, delimiter_w_spaces, value, ORS } # print comment function c_print() { - spaces = "" - for(i = 0; i < indentation; i++) - spaces = (spaces " ") - printf "%s%s%s%s%s", spaces, comment_sign, " ", comment, ORS + printf "%s%s%s%s%s", base_spaces, comment_sign, " ", comment, ORS } diff --git a/cdist/conf/type/__ini_value/man.rst b/cdist/conf/type/__ini_value/man.rst index c583d5ad..0812d432 100644 --- a/cdist/conf/type/__ini_value/man.rst +++ b/cdist/conf/type/__ini_value/man.rst @@ -63,6 +63,14 @@ comment-sign parameters are possible. It uses the first specified sign as comment character if this type needs to insert comments. +delimiter-space + The number of spaces before and after the delimiter which should be free. + This number applies to each site of the delimiter separately, so one space + means one space to the left and right side of the delimiter. + + The delimiter will be matched independendtly of this parameter and will + only be corrected if ``--normalize`` is set. + BOOLEAN PARAMETERS ------------------ diff --git a/cdist/conf/type/__ini_value/parameter/optional b/cdist/conf/type/__ini_value/parameter/optional index 031c807b..6acbf8e5 100644 --- a/cdist/conf/type/__ini_value/parameter/optional +++ b/cdist/conf/type/__ini_value/parameter/optional @@ -4,3 +4,4 @@ state value indentation comment +delimiter-space