diff --git a/lib/files.cf b/lib/files.cf index 454f815576..6cd743b97a 100644 --- a/lib/files.cf +++ b/lib/files.cf @@ -423,6 +423,8 @@ bundle edit_line set_variable_values_ini(tab, sectionName) # If the line is commented out with `#`, it gets uncommented first. # Adds a new line if none exists. # +# It does support = in value +# # @param tab An associative array containing `tab[sectionName][LHS]="RHS"`. # The value is not changed when the `RHS` is "dontchange" # @param sectionName The section in the file within which values should be @@ -436,33 +438,43 @@ bundle edit_line set_variable_values_ini(tab, sectionName) # Be careful if the index string contains funny chars "cindex[$(index)]" string => canonify("$(index)"); + # Escape the value so that it can be regexed + "ctab[$(index)]" string => escape("$($(tab)[$(sectionName)][$(index)])"); + classes: "edit_$(cindex[$(index)])" not => strcmp("$($(tab)[$(sectionName)][$(index)])","dontchange"), comment => "Create conditions to make changes"; - field_edits: - - # If the line is there, but commented out, first uncomment it - "#+\s*$(index)\s*=.*" - select_region => INI_section(escape("$(sectionName)")), - edit_field => col("\s*=\s*","1","$(index)","set"), - if => "edit_$(cindex[$(index)])"; - - # match a line starting like the key something - "\s*$(index)\s*=.*" - edit_field => col("\s*=\s*","2","$($(tab)[$(sectionName)][$(index)])","set"), - select_region => INI_section(escape("$(sectionName)")), - classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"), - if => "edit_$(cindex[$(index)])"; + "pass2" expression => "pass1"; + "pass1" expression => "any"; insert_lines: "[$(sectionName)]" location => start, comment => "Insert lines"; + pass2:: "$(index)=$($(tab)[$(sectionName)][$(index)])" select_region => INI_section(escape("$(sectionName)")), - if => "!(set_variable_values_ini_not_$(cindex[$(index)])_kept|set_variable_values_ini_not_$(cindex[$(index)])_repaired).edit_$(cindex[$(index)])"; + if => "edit_$(cindex[$(index)])"; + + replace_patterns: + # If the line is commented out, uncomment and replace with + # the correct value + "^\s*#\s*($(index)\s*=\s*.*)$" + comment => "If we find a commented entry we uncomment it", + select_region => INI_section(escape("$(set_variable_values_ini.sectionName)")), + replace_with => value("$(index)=$($(tab)[$(sectionName)][$(index)])"), + if => "edit_$(cindex[$(index)]).!set_variable_values_ini_not_$(cindex[$(index)])_reached"; + + # If the line is there with the wrong value, replace with + # the correct value + "^\s*($(index)\s*=\s*(?!$(ctab[$(index)])$).*)$" + comment => "Correct the value $(index)", + replace_with => value("$(index)=$($(tab)[$(sectionName)][$(index)])"), + select_region => INI_section(escape("$(set_variable_values_ini.sectionName)")), + classes => results("bundle", "set_variable_values_ini_not_$(cindex[$(index)])"), + if => "edit_$(cindex[$(index)])"; } diff --git a/tests/acceptance/lib/files/set_variable_values_ini.cf b/tests/acceptance/lib/files/set_variable_values_ini.cf index 41707edf9b..071218d74a 100644 --- a/tests/acceptance/lib/files/set_variable_values_ini.cf +++ b/tests/acceptance/lib/files/set_variable_values_ini.cf @@ -31,7 +31,7 @@ bundle agent init bundle agent test { meta: - "description" -> { "CFE-3221", "CFE-3244" } + "description" -> { "CFE-3221", "CFE-3244", "CFE-413" } string => "Test expected behavior of set_variable_values_ini"; vars: @@ -50,6 +50,9 @@ bundle agent test "config[CFE-3244][keyone]" string => "1"; "config[CFE-3244][keytwo]" string => "valuetwo"; + "config[CFE-413][keyone]" string => "one"; + "config[CFE-413][keytwo]" string => "value=one"; + files: "$(G.testfile).actual" @@ -61,6 +64,8 @@ bundle agent test "$(G.testfile).actual" -> { "CFE-3244" } edit_line => set_variable_values_ini( "test.config", "CFE-3244" ); + "$(G.testfile).actual" -> { "CFE-413" } + edit_line => set_variable_values_ini( "test.config", "CFE-413" ); } ####################################################### diff --git a/tests/acceptance/lib/files/set_variable_values_ini.cf.finish b/tests/acceptance/lib/files/set_variable_values_ini.cf.finish index 71418aff59..202fe89fd8 100644 --- a/tests/acceptance/lib/files/set_variable_values_ini.cf.finish +++ b/tests/acceptance/lib/files/set_variable_values_ini.cf.finish @@ -15,3 +15,6 @@ variablename4=desired value4 keytwo = valuetwo [something] keyone=something1 +[CFE-413] +keytwo=value=one +keyone=one diff --git a/tests/acceptance/lib/files/set_variable_values_ini.cf.start b/tests/acceptance/lib/files/set_variable_values_ini.cf.start index 9f49f0a54b..1f4e2f65d3 100644 --- a/tests/acceptance/lib/files/set_variable_values_ini.cf.start +++ b/tests/acceptance/lib/files/set_variable_values_ini.cf.start @@ -14,3 +14,5 @@ variablename3 =desired value3 # keytwo = bar [something] keyone=something1 +[CFE-413] +keytwo=value=two