IN this case, you need to define a class regardless if what happens when the 
pattern
replace is made.

 replace_patterns:

   "${p}"
     replace_with => value("${l}"),
     classes => always("done");

 insert_lines:

  done::

    "${l}"; # add only if it doesn't exist

This should work as long as the $(l) is always a complete line, otherwise your 
promise is
ambiguous. What happens if the pattern matches a partial line? cfengine inserts 
the line
because there is no complete line matching this expression. Perhaps your 
pattern should be
"^$(p)$" ?



Diego Zamboni wrote:
> Hi Mark,
> 
> Thanks. In fact, my very first attempt was something very similar to
> set_variable_values(), only with a replace_patterns section instead of
> field_edits. The problem I found was that according to normal
> ordering, insert_lines is run before replace_patterns (whereas
> field_edits occurs before insert_lines), which defeats the test with
> the classes. The line is always inserted, regardless of whether the
> replacement works or not, because the negation of a non-existing class
> is true.
> 
> Here's that very first version, for reference:
> 
> bundle edit_line replace_or_add(p,l)
> {
> replace_patterns:
>   "${p}"
>   replace_with => value("${l}"),
>   classes => if_ok("line_exists");
> 
> insert_lines:
>   "${l}"
>   ifvarclass => "!line_exists";
> }
> 
> What am I missing?
> 
> Thanks again,
> --Diego
> 
> 
> On Mon, Jan 25, 2010 at 12:29 PM, Mark Burgess <mark.burg...@iu.hio.no> wrote:
>> Try looking at the set-variable bundle in the standard library
>>
>> M
>>
>> Diego Zamboni wrote:
>>> Hi,
>>>
>>> I've been working on putting together an edit_line bundle that does
>>> the following:
>>>
>>> - If a certain pattern exists, replace it with a given string
>>> - If the pattern does not exist, add the line to the file.
>>>
>>> Below is what I came up with, which works, but somehow feels inelegant
>>> because I have to pass it the filename for the check using regline. I
>>> tried doing it by setting different classes in the replace_patterns:
>>> and insert_lines: sections, but could not get it to work.
>>>
>>> I would appreciate any ideas! I am just learning cfengine, so this is
>>> as much an intellectual exercise as something I need (and maybe could
>>> be a useful addition to the stdlib?)
>>>
>>> Thanks,
>>> --Diego
>>>
>>>
>>> bundle edit_line replace_or_add(file,pattern,line)
>>> {
>>> classes:
>>>   "lineexists" expression => regline("$(pattern)","$(file)");
>>>
>>> replace_patterns:
>>>   lineexists::
>>>   "${pattern}"
>>>   replace_with => value("${line}");
>>>
>>> insert_lines:
>>>   !lineexists::
>>>   "${line}";
>>>
>>> }
>>> _______________________________________________
>>> Help-cfengine mailing list
>>> Help-cfengine@cfengine.org
>>> https://cfengine.org/mailman/listinfo/help-cfengine
>> --
>> Mark Burgess
>>
>> -------------------------------------------------
>> Professor of Network and System Administration
>> Oslo University College, Norway
>>
>> Personal Web: http://www.iu.hio.no/~mark
>> Office Telf : +47 22453272
>> -------------------------------------------------
>>

-- 
Mark Burgess

-------------------------------------------------
Professor of Network and System Administration
Oslo University College, Norway

Personal Web: http://www.iu.hio.no/~mark
Office Telf : +47 22453272
-------------------------------------------------
_______________________________________________
Help-cfengine mailing list
Help-cfengine@cfengine.org
https://cfengine.org/mailman/listinfo/help-cfengine

Reply via email to