Forum: Cfengine Help
Subject: Re: Edit region of file - remove existing content
Author: toddnni
Link to topic: https://cfengine.com/forum/read.php?3,18633,20842#msg-20842

phnakarin

I tried the code you posted. It didn't work for me. Here is a modified version 
of your code which produces an unexpected result.


body common control {
  #bundlesequence => { "fill_file" };
  bundlesequence => { "fill_file", "test_region" };
}

bundle agent fill_file {
files:
  "/tmp/test_file.txt" 
   comment => "Fill the file with text.",
   edit_defaults => empty,
   create => "true",
   edit_line => insert;
}

body edit_defaults empty {
  empty_file_before_editing => "true";
}

bundle edit_line insert {
insert_lines:
"This is a test file. The lines in the end should not be deleted.
test
## CF3 PROTECTED AREA DO NOT EDIT ##
This should be edited!
## END CF3 PROTECTED AREA DO NOT EDIT ##
test
test
test
test";
}

bundle agent test_region {
files:
  "/tmp/test_file.txt"
   comment => "Test, insert stuff into selected region of file",
   edit_line => insert_between_region_002(
     "## CF3 PROTECTED AREA DO NOT EDIT ##",
     "## END CF3 PROTECTED AREA DO NOT EDIT ##",
     "123456790");
}

bundle edit_line insert_between_region_002(sdelim,edelim,insert_file) {
delete_lines:
  ".*"
   delete_select => example_002("$(sdelim)","$(edelim)"), # try to comment
   select_region => start_end("$(sdelim)","$(edelim)");
insert_lines:
  "$(insert_file)"
   select_region => start_end("$(sdelim)","$(edelim)");
}

body delete_select example_002(s1,s2) {
  delete_if_not_match_from_list => { "$(s1)","$(s2)" };
}

body select_region start_end(start,end) {
  select_start => "$(start)";
  select_end => "$(end)";
  include_start_delimiter => "false";
  include_end_delimiter => "false";
}


And the result on my FreeBSD 8.1 system is


This is a test file. The lines in the end should not be deleted.
test
## CF3 PROTECTED AREA DO NOT EDIT ##
123456790
## END CF3 PROTECTED AREA DO NOT EDIT ##


The '## END CF3 PROTECTED AREA DO NOT EDIT ##' also gets unexpectedly deleted 
if we remove the 'delete_select' line.

Thanks for your time.

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

Reply via email to