Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CFE-413: set_variable_values_ini for values with equal sign #1873

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions lib/files.cf
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ewwwww. Why are you forcing a specific pass restriction?

Presumably, something isn't yet defined or hasn't been completed. Or, not able to select region until the second pass?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exactly. If it's on the first pass, if the section doesn't exist yet, it cannot select the region, and fails. It won't reevaluate at the 2nd pass

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but since the promise to insert the section is before the promise to set key values for the region, it should be able to select it during the same pass.

"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)])";

}

Expand Down
7 changes: 6 additions & 1 deletion tests/acceptance/lib/files/set_variable_values_ini.cf
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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"
Expand All @@ -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" );
}

#######################################################
Expand Down
3 changes: 3 additions & 0 deletions tests/acceptance/lib/files/set_variable_values_ini.cf.finish
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ variablename4=desired value4
keytwo = valuetwo
[something]
keyone=something1
[CFE-413]
keytwo=value=one
keyone=one
2 changes: 2 additions & 0 deletions tests/acceptance/lib/files/set_variable_values_ini.cf.start
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ variablename3 =desired value3
# keytwo = bar
[something]
keyone=something1
[CFE-413]
keytwo=value=two