Skip to content

Commit 6caf8c2

Browse files
authored
Merge pull request #1811 from nickanderson/ENT-6117/master
ENT-6117/master: Added replace_uncommented_substrings
2 parents bfeed7b + d77f5de commit 6caf8c2

4 files changed

Lines changed: 94 additions & 0 deletions

File tree

lib/files.cf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,41 @@ bundle edit_line replace_line_end(start,end)
245245
edit_field => line("(^|\s)$(start)\s*", "2", "$(end)","set");
246246
}
247247

248+
bundle edit_line replace_uncommented_substrings( _comment, _find, _replace )
249+
# @brief Replace all occurrences of `_find` with `_replace` on lines that do not follow a `_comment`
250+
# @param _comment Sequence of characters, each indicating the start of a comment.
251+
# @param _find String matching substring to replace
252+
# @param _replace String to substitute `_find` with
253+
#
254+
# **Example:**
255+
#
256+
# ```cf3
257+
# bundle agent example_replace_uncommented_substrings
258+
# {
259+
# files:
260+
# "/tmp/file.txt"
261+
# edit_line => replace_uncommented_substrings( "#", "ME", "YOU");
262+
# }
263+
# ```
264+
#
265+
# **Notes:**
266+
#
267+
# * Only single character comments are supported as `_comment` is used in the PCRE character group (`[^...]`).
268+
# * `-` in `_comment` is interpreted as a range unless it's used as the first or last character. For example, setting `_comment` to `0-9` means any digit starts a comment.
269+
#
270+
# **History:**
271+
#
272+
# * Introduced 3.17.0
273+
{
274+
vars:
275+
"_reg_match_uncommented_lines_containing_find"
276+
string => "^([^$(_comment)]*)\Q$(_find)\E(.*$)";
277+
278+
replace_patterns:
279+
"$(_reg_match_uncommented_lines_containing_find)"
280+
replace_with => text_between_match1_and_match2( $(_replace) );
281+
}
282+
248283
##
249284

250285
bundle edit_line append_to_line_end(start,end)
@@ -1146,6 +1181,14 @@ body edit_field line(split,col,newval,method)
11461181

11471182
##
11481183

1184+
body replace_with text_between_match1_and_match2( _text )
1185+
# @brief Replace matched line with substituted string
1186+
# @param _text String to substitute between first and second match
1187+
{
1188+
replace_value => "$(match.1)$(_text)$(match.2)";
1189+
occurrences => "all";
1190+
}
1191+
11491192
body replace_with value(x)
11501193
# @brief Replace matching lines
11511194
# @param x The replacement string
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
body common control
2+
{
3+
inputs => { '../../default.cf.sub',
4+
};
5+
bundlesequence => { default("$(this.promise_filename)") };
6+
version => "1.0";
7+
}
8+
#######################################################
9+
10+
bundle common classes
11+
{
12+
classes:
13+
14+
"testing_masterfiles_policy_framework"
15+
comment => "This class is needed so that dcs.cf.sub includes the stdlib instead of using plucked.cf.sub from core which might get out of date and cause us to not test current code from the MPF.";
16+
}
17+
18+
bundle agent test
19+
{
20+
meta:
21+
"description" -> { "ENT-6117" }
22+
string => "Test that replace_uncommented_substrings behaves as expected";
23+
24+
files:
25+
"$(this.promise_filename).before"
26+
edit_line => replace_uncommented_substrings( "#", "ME", "YOU");
27+
}
28+
bundle agent check
29+
{
30+
methods:
31+
32+
# Note: .before and .after differ beyond the substitution in that .after
33+
# has a trailing newline. edit_line seems to append a trailing new line
34+
# when it edits, perhaps related to CFE-270.
35+
36+
"check"
37+
usebundle => dcs_check_diff( "$(this.promise_filename).before",
38+
"$(this.promise_filename).after",
39+
"$(this.promise_filename)");
40+
41+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Don't replace ME please
2+
# please don't replace ME either
3+
You should replace YOU, and YOU though, and YOU also, and YOU, and YOU, and YOU, and YOU, # But, ME will not get replaced since it's after a comment
4+
And, also you should replace YOU too
5+
# Not ME
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Don't replace ME please
2+
# please don't replace ME either
3+
You should replace ME, and ME though, and ME also, and ME, and ME, and ME, and ME, # But, ME will not get replaced since it's after a comment
4+
And, also you should replace ME too
5+
# Not ME

0 commit comments

Comments
 (0)