Forum: Cfengine Help
Subject: Re: AppendIfNoLineMatching and Cfengine 3
Author: zzamboni
Link to topic: https://cfengine.com/forum/read.php?3,19997,21005#msg-21005

Rob,

Sorry it took me a while to respond. The reason why this works is that the 
normal order inside edit_line bundles is as follows: vars, classes, 
delete_lines, field_edits, insert_lines, replace_patterns and reports. This 
means that insert_lines happens before replace_patterns. If we did the 
insert_lines unconditionally, we would end up with a duplicated line once the 
replace_patterns fixes a matching line that was not already equal to the new 
one. Instead, I make use of the three passes that cfengine does in each bundle 
to skip the insert_lines the first time around, and only execute it on the 
second pass. By this time, if replace_patterns has already fixed the line, it 
will not be inserted again, because insert_lines is smart enough not to do 
that. On the contrary, if replace_patterns didn't find a line to fix, 
insert_lines will do its job and add it to the file.

This is why the replace_done class is defined no matter what - in this case it 
is not used as an indication of the success of the promise, but rather as a 
flag to indicate that the replace_patterns promise has been processed at least 
once already. Maybe the class should be named "replace_attempted" instead of 
"replace_done" to better indicate its purpose.

I hope this clarifies things.

_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to