File: files.cf

package info (click to toggle)
cfengine3 3.24.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 37,552 kB
  • sloc: ansic: 163,161; sh: 10,296; python: 2,950; makefile: 1,744; lex: 784; yacc: 633; perl: 211; pascal: 157; xml: 21; sed: 13
file content (2556 lines) | stat: -rw-r--r-- 68,795 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
# Files bodies

bundle common files_common
# @brief Enumerate policy files used by this policy file for inclusion to inputs
{
  vars:
      "inputs" slist => { "$(this.promise_dirname)/common.cf" };
}

body file control
# @brief Include policy files used by this policy file as part of inputs
{
      inputs => { @(files_common.inputs) };
}

###################################################
# edit_line bundles
###################################################

bundle edit_line insert_before_if_no_line(before, string)
# @brief Insert `string` before `before` if `string` is not found in the file
# @param before The regular expression matching the line which `string` will be
# inserted before
# @param string The string to be prepended
#
{
  insert_lines:
      "$(string)"
        location => before($(before)),
        comment => "Prepend a line to the file if it doesn't already exist";
}

##

bundle edit_line insert_file(templatefile)
# @brief Reads the lines from `templatefile` and inserts those into the
# file being edited.
# @param templatefile The name of the file from which to import lines.
{
  insert_lines:

      "$(templatefile)"
      comment => "Insert the template file into the file being edited",
      insert_type => "file";
}

##

bundle edit_line lines_present(lines)
# @brief Ensure `lines` are present in the file. Lines that do not exist are appended to the file
# @param lines List or string that should be present in the file
#
# **Example:**
#
# ```cf3
# bundle agent example
# {
#  vars:
#    "nameservers" slist => { "8.8.8.8", "8.8.4.4" };
#
#  files:
#      "/etc/resolv.conf" edit_line => lines_present( @(nameservers) );
#      "/etc/ssh/sshd_config" edit_line => lines_present( "PermitRootLogin no" );
# }
# ```
{
  insert_lines:

      "$(lines)"
        comment => "Append lines if they don't exist";
}

bundle edit_line insert_lines(lines)
# @brief Alias for `lines_present`
# @param lines List or string that should be present in the file
{
  insert_lines:

      "$(lines)"
        comment => "Append lines if they don't exist";
}

bundle edit_line append_if_no_line(lines)
# @brief Alias for `lines_present`
# @param lines List or string that should be present in the file
{
  insert_lines:

      "$(lines)"
        comment => "Append lines if they don't exist";
}

bundle edit_line append_if_no_lines(lines)
# @brief Alias for `lines_present`
# @param lines List or string that should be present in the file
{
  insert_lines:

      "$(lines)"
        comment => "Append lines if they don't exist";
}

##

bundle edit_line comment_lines_matching(regex,comment)
# @brief Comment lines in the file that matching an [anchored] regex
# @param regex Anchored regex that the entire line needs to match
# @param comment A string that is prepended to matching lines
{
  replace_patterns:

      "^($(regex))$"

      replace_with => comment("$(comment)"),
      comment => "Search and replace string";
}

##

bundle edit_line contains_literal_string(string)
# @brief Ensure the literal string is present in the promised file
# @description If the string is not found in the file it is inserted according
# to CFEngine defaults.
# @param string The string (potentially multiline) to ensure exists in the
# promised file.
{

   insert_lines:
     "$(string)"
       insert_type => "preserve_block",
       expand_scalars => "false",
       whitespace_policy => { "exact_match" };
}

##

bundle edit_line uncomment_lines_matching(regex,comment)
# @brief Uncomment lines of the file where the regex matches
# the entire text after the comment string
# @param regex The regex that lines need to match after `comment`
# @param comment The prefix of the line that is removed
{
  replace_patterns:

      "^$(comment)\s?($(regex))$"

      replace_with => uncomment,
      comment => "Uncomment lines matching a regular expression";
}

##

bundle edit_line comment_lines_containing(regex,comment)
# @brief Comment lines of the file matching a regex
# @param regex A regex that a part of the line needs to match
# @param comment A string that is prepended to matching lines
{
  replace_patterns:

      "^((?!$(comment)).*$(regex).*)$"

      replace_with => comment("$(comment)"),
      comment => "Comment out lines in a file";
}

##

bundle edit_line uncomment_lines_containing(regex,comment)
# @brief Uncomment lines of the file where the regex matches
# parts of the text after the comment string
# @param regex The regex that lines need to match after `comment`
# @param comment The prefix of the line that is removed
{
  replace_patterns:

      "^$(comment)\s?(.*$(regex).*)$"

      replace_with => uncomment,
      comment => "Uncomment a line containing a fragment";
}

##

bundle edit_line delete_lines_matching(regex)
# @brief Delete lines matching a regular expression
# @param regex The regular expression that the lines need to match
{
  delete_lines:

      "$(regex)"

      comment => "Delete lines matching regular expressions";
}

##

bundle edit_line warn_lines_matching(regex)
# @brief Warn about lines matching a regular expression
# @param regex The regular expression that the lines need to match
{
  delete_lines:

      "$(regex)"

      comment => "Warn about lines in a file",
      action => warn_only;
}

##

bundle edit_line prepend_if_no_line(string)
# @brief Prepend `string` if it doesn't exist in the file
# @param string The string to be prepended
#
# **See also:**
#
# * [`insert_lines` promise type][insert_lines]
# * [`edit_line` bundles][edit_line]
{
  insert_lines:
      "$(string)"
      location => start,
      comment => "Prepend a line to the file if it doesn't already exist";
}

##

bundle edit_line replace_line_end(start,end)
# @brief Give lines starting with `start` the ending given in `end`
#
# Whitespaces will be left unmodified. For example,
# `replace_line_end("ftp", "2121/tcp")` would replace
#
# `"ftp             21/tcp"`
#
# with
#
# `"ftp             2121/tcp"`
#
# @param start The string lines have to start with
# @param end The string lines should end with
{
  field_edits:

      "\s*$(start)\s.*"
      comment => "Replace lines with $(this.start) and $(this.end)",
      edit_field => line("(^|\s)$(start)\s*", "2", "$(end)","set");
}

bundle edit_line replace_uncommented_substrings( _comment, _find, _replace )
# @brief Replace all occurrences of `_find` with `_replace` on lines that do not follow a `_comment`
# @param _comment Sequence of characters, each indicating the start of a comment.
# @param _find String matching substring to replace
# @param _replace String to substitute `_find` with
#
# **Example:**
#
# ```cf3
# bundle agent example_replace_uncommented_substrings
# {
#   files:
#     "/tmp/file.txt"
#       edit_line => replace_uncommented_substrings( "#", "ME", "YOU");
# }
# ```
#
# **Notes:**
#
# * Only single character comments are supported as `_comment` is used in the PCRE character group (`[^...]`).
# * `-` 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.
#
# **History:**
#
# * Introduced 3.17.0, 3.15.3
{
  vars:
      "_reg_match_uncommented_lines_containing_find"
        string => "^([^$(_comment)]*)\Q$(_find)\E(.*$)";

  replace_patterns:
    "$(_reg_match_uncommented_lines_containing_find)"
      replace_with => text_between_match1_and_match2( $(_replace) );
}

##

bundle edit_line append_to_line_end(start,end)
# @brief Append `end` to any lines beginning with `start`
#
# `end` will be appended to all lines starting with `start` and not
# already ending with `end`.  Whitespaces will be left unmodified.
#
# For example, `append_to_line_end("kernel", "vga=791")` would replace
# `kernel /boot/vmlinuz root=/dev/sda7`
#
# with
#
# `kernel /boot/vmlinuz root=/dev/sda7 vga=791`
#
# **WARNING**: Be careful not to have multiple promises matching the same line, which would result in the line growing indefinitely.
#
# @param start pattern to match lines of interest
# @param end string to append to matched lines
#
# **Example:**
#
# ```cf3
#  files:
#      "/tmp/boot-options" edit_line => append_to_line_end("kernel", "vga=791");
# ```
#
{
  field_edits:

      "\s*$(start)\s.*"
      comment => "Append lines with $(this.start) and $(this.end)",
      edit_field => line("(^|\s)$(start)\s*", "2", "$(end)","append");
}

##

bundle edit_line regex_replace(find,replace)
# @brief Find exactly a regular expression and replace exactly the match with a string.
# You can think of this like a PCRE powered sed.
# @param find The regular expression
# @param replace The replacement string
{
  replace_patterns:

      "$(find)"
      replace_with => value("$(replace)"),
      comment => "Search and replace string";
}

##

bundle edit_line resolvconf(search,list)
# @brief Adds search domains and name servers to the system
# resolver configuration.
#
# Use this bundle to modify `resolv.conf`. Existing entries for
# `search` and `nameserver` are replaced.
#
# @param search The search domains with space
# @param list An slist of nameserver addresses
{
  delete_lines:

      "search.*"     comment => "Reset search lines from resolver";
      "nameserver.*" comment => "Reset nameservers in resolver";

  insert_lines:

      "search $(search)"    comment => "Add search domains to resolver";
      "nameserver $(list)"  comment => "Add name servers to resolver";
}

##

bundle edit_line resolvconf_o(search,list,options)
# @brief Adds search domains, name servers and options to the system
# resolver configuration.
#
# Use this bundle to modify `resolv.conf`. Existing entries for
# `search`, `nameserver` and `options` are replaced.
#
# @param search The search domains with space
# @param list An slist of nameserver addresses
# @param options is an slist of variables to modify the resolver

{
  delete_lines:

      "search.*"     comment => "Reset search lines from resolver";
      "nameserver.*" comment => "Reset nameservers in resolver";
      "options.*"    comment => "Reset options in resolver";

  insert_lines:

      "search $(search)"    comment => "Add search domains to resolver";
      "nameserver $(list)"  comment => "Add name servers to resolver";
      "options $(options)"  comment => "Add options to resolver";
}

##

bundle edit_line manage_variable_values_ini(tab, sectionName)
# @brief Sets the RHS of configuration items in the file of the form
# `LHS=RHS`
#
# If the line is commented out with `#`, it gets uncommented first.
# Adds a new line if none exists.
# Removes any variable value pairs not defined for the ini section.
#
# @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
# modified
#
# **See also:** `set_variable_values_ini()`
{
  vars:
      "index" slist => getindices("$(tab)[$(sectionName)]");

  delete_lines:
      ".*"
      select_region => INI_section(escape("$(sectionName)")),
      comment       => "Remove all entries in the region so there are no extra entries";

  insert_lines:
      "[$(sectionName)]"
      location => start,
      comment => "Insert lines";

      "$(index)=$($(tab)[$(sectionName)][$(index)])"
      select_region => INI_section(escape("$(sectionName)"));
}

##

bundle edit_line set_variable_values_ini(tab, sectionName)
# @brief Sets the RHS of configuration items in the file of the form
# `LHS=RHS`
#
# If the line is commented out with `#`, it gets uncommented first.
# Adds a new line if none exists.
#
# @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
# modified
#
# **See also:** `manage_variable_values_ini()`
{
  vars:
      "index" slist => getindices("$(tab)[$(sectionName)]");

      # Be careful if the index string contains funny chars
      "cindex[$(index)]" string => canonify("$(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)])";

  insert_lines:
      "[$(sectionName)]"
      location => start,
      comment => "Insert lines";

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

}

bundle edit_line insert_ini_section(name, config)
# @brief Inserts a INI section with content
#
# ```
# # given an array "barray"
# files:
#     "myfile.ini" edit_line => insert_ini_section("foo", "barray");
# ```
#
# Inserts a section in an INI file with the given configuration
# key-values from the array `config`.
#
# @param name the name of the INI section
# @param config The fully-qualified name of an associative array containing `v[LHS]="rhs"`
{
  vars:
      # TODO: refactor once 3.7.x is EOL
      "indices" slist => getindices($(config));
      "k" slist => sort("indices", lex);

  insert_lines:
      "[$(name)]"
      location => start,
      comment => "Insert an ini section with values if not present";

      "$(k)=$($(config)[$(k)])"
      location => after("[$(name)]");
}


bundle edit_line set_quoted_values(v)
# @brief Sets the RHS of variables in shell-like files of the form:
#
# ```
#      LHS="RHS"
# ```
#
# Adds a new line if no LHS exists, and replaces RHS values if one does exist.
# If the line is commented out with #, it gets uncommented first.
#
# @param v The fully-qualified name of an associative array containing `v[LHS]="rhs"`
#
# **Example:**
#
# ```cf3
#     vars:
#        "stuff[lhs-1]" string => "rhs1";
#        "stuff[lhs-2]" string => "rhs2";
#
#     files:
#        "myfile"
#          edit_line => set_quoted_values(stuff)
# ```
#
# **See also:** `set_variable_values()`
{
  meta:
      "tags"
      slist =>
      {
        "deprecated=3.6.0",
        "deprecation-reason=Generic reimplementation",
        "replaced-by=set_line_based"
      };

  vars:
      "index" slist => getindices("$(v)");
      # Be careful if the index string contains funny chars

      "cindex[$(index)]" string => canonify("$(index)");

  field_edits:
      # If the line is there, but commented out, first uncomment it
      "#+\s*$(index)\s*=.*"
      edit_field => col("=","1","$(index)","set");

      # match a line starting like the key = something
      "\s*$(index)\s*=.*"
      edit_field => col("=","2",'"$($(v)[$(index)])"',"set"),
      classes    => results("bundle", "$(cindex[$(index)])_in_file"),
      comment    => "Match a line starting like key = something";

  insert_lines:
      '$(index)="$($(v)[$(index)])"'
      comment    => "Insert a variable definition",
        if => "!($(cindex[$(index)])_in_file_kept|$(cindex[$(index)])_in_file_repaired)";
}

##

bundle edit_line set_variable_values(v)
# @brief Sets the RHS of variables in files of the form:
#
# ```
#      LHS=RHS
# ```
#
# Adds a new line if no LHS exists, and replaces RHS values if one does exist.
# If the line is commented out with #, it gets uncommented first.
#
# @param v The fully-qualified name of an associative array containing `v[LHS]="rhs"`
#
# **Example:**
#
# ```cf3
#     vars:
#        "stuff[lhs-1]" string => "rhs1";
#        "stuff[lhs-2]" string => "rhs2";
#
#     files:
#        "myfile"
#          edit_line => set_variable_values(stuff)
# ```
#
# **See also:** `set_quoted_values()`
{
  meta:
      "tags"
      slist =>
      {
        "deprecated=3.6.0",
        "deprecation-reason=Generic reimplementation",
        "replaced-by=set_line_based"
      };

  vars:

      "index" slist => getindices("$(v)");

      # Be careful if the index string contains funny chars

      "cindex[$(index)]" string => canonify("$(index)");
      "cv"               string => canonify("$(v)");

  field_edits:

      # match a line starting like the key = something

      "\s*$(index)\s*=.*"

      edit_field => col("\s*$(index)\s*=","2","$($(v)[$(index)])","set"),
      classes => results("bundle", "$(cv)_$(cindex[$(index)])_in_file"),
      comment => "Match a line starting like key = something";

  insert_lines:

      "$(index)=$($(v)[$(index)])"

      comment => "Insert a variable definition",
        if => "!($(cv)_$(cindex[$(index)])_in_file_kept|$(cv)_$(cindex[$(index)])_in_file_repaired)";
}

bundle edit_line set_config_values(v)
# @brief Sets the RHS of configuration items in the file of the form:
#
# ```
#   LHS RHS
# ```
#
# If the line is commented out with `#`, it gets uncommented first.
#
# Adds a new line if none exists.
#
# @param v The fully-qualified name of an associative array containing `v[LHS]="rhs"`
{
  meta:
      "tags"
      slist =>
      {
        "deprecated=3.6.0",
        "deprecation-reason=Generic reimplementation",
        "replaced-by=set_line_based"
      };

  vars:
      "index" slist => getindices("$(v)");

      # Be careful if the index string contains funny chars
      "cindex[$(index)]" string => canonify("$(index)");

      # Escape the value (had a problem with special characters and regex's)
      "ev[$(index)]" string => escape("$($(v)[$(index)])");

      # Do we have more than one line commented out?
      "index_comment_matches_$(cindex[$(index)])"
        int => countlinesmatching("^\s*#\s*($(index)\s+.*|$(index))$","$(edit.filename)");


  classes:
      # Check to see if this line exists
      "line_exists_$(cindex[$(index)])"
        expression => regline("^\s*($(index)\s.*|$(index))$","$(edit.filename)"),
        scope => "bundle";

      # if there's more than one comment, just add new (don't know who to use)
      "multiple_comments_$(cindex[$(index)])"
        expression => isgreaterthan("$(index_comment_matches_$(cindex[$(index)]))","1"),
        scope => "bundle";

  replace_patterns:
      # If the line is commented out, uncomment and replace with
      # the correct value
      "^\s*#\s*($(index)\s+.*|$(index))$"
        comment => "If we find a single commented entry we can uncomment it to
                    keep the settings near any inline documentation. If there
                    are multiple comments, then we don't try to replace them and
                    instead will later append the new value after the first
                    commented occurrence of $(index).",
        handle => "set_config_values_replace_commented_line",
        replace_with => value("$(index) $($(v)[$(index)])"),
                  if => "!line_exists_$(cindex[$(index)]).!replace_attempted_$(cindex[$(index)])_reached.!multiple_comments_$(cindex[$(index)])",
             classes => results("bundle", "uncommented_$(cindex[$(index)])");

      # If the line is there with the wrong value, replace with
      # the correct value
      "^\s*($(index)\s+(?!$(ev[$(index)])$).*|$(index))$"
           comment => "Correct the value $(index)",
      replace_with => value("$(index) $($(v)[$(index)])"),
           classes => results("bundle", "replace_attempted_$(cindex[$(index)])");

  insert_lines:
      # If the line doesn't exist, or there is more than one occurrence
      # of the LHS commented out, insert a new line and try to place it
      # after the commented LHS (keep new line with old comments)
      "$(index) $($(v)[$(index)])"
         comment => "Insert the value, marker exists $(index)",
        location => after("^\s*#\s*($(index)\s+.*|$(index))$"),
              if => "replace_attempted_$(cindex[$(index)])_reached.multiple_comments_$(cindex[$(index)])";

      # If the line doesn't exist and there are no occurrences
      # of the LHS commented out, insert a new line at the eof
      "$(index) $($(v)[$(index)])"
         comment => "Insert the value, marker doesn't exist $(index)",
              if => "replace_attempted_$(cindex[$(index)])_reached.!multiple_comments_$(cindex[$(index)])";

}

bundle edit_line set_line_based(v, sep, bp, kp, cp)
# @brief Sets the RHS of configuration items in the file of the form:
#
# ```
#   LHS$(sep)RHS
# ```
#
# Example usage for `x=y` lines (e.g. rsyncd.conf):
#
# ```cf3
# "myfile"
# edit_line => set_line_based("test.config", "=", "\s*=\s*", ".*", "\s*#\s*");
# ```
#
# Example usage for `x y` lines (e.g. sshd_config):
#
# ```cf3
# "myfile"
# edit_line => set_line_based("test.config", " ", "\s+", ".*", "\s*#\s*");
# ```
#
# If the line is commented out with `$(cp)`, it gets uncommented first.
#
# Adds a new line if none exists or if more than one commented-out
# possible matches exist.
#
#
# **Note:** If the data structure being used for the first parameter is in the current bundle, you can use `$(this.bundle).variable`.
#
# Originally `set_config_values` by Ed King.
#
# @param v The fully-qualified name (`bundlename.variable`) of an associative array containing `v[LHS]="rhs"`
# @param sep The separator to insert, e.g. ` ` for space-separated
# @param bp The key-value separation regex, e.g. `\s+` for space-separated
# @param kp The keys to select from v, use `.*` for all
# @param cp The comment pattern from line-start, e.g. `\s*#\s*`
{
  meta:
      "tags"
      slist =>
      {
        "replaces=set_config_values",
        "replaces=set_config_values_matching",
        "replaces=set_variable_values",
        "replaces=set_quoted_values",
        "replaces=maintain_key_values",
      };

  vars:
      "vkeys" slist => getindices("$(v)");
      "i" slist => grep($(kp), vkeys);

      # Be careful if the index string contains funny chars
      "ci[$(i)]" string => canonify("$(i)");

      # Escape the value (had a problem with special characters and regex's)
      "ev[$(i)]" string => escape("$($(v)[$(i)])");

      # Do we have more than one line commented out?
      "comment_matches_$(ci[$(i)])"
      int => countlinesmatching("^$(cp)($(i)$(bp).*|$(i))$",
                                $(edit.filename));


  classes:
      # 3.21.0 and greater know about a file being emptied before editing and
      # skip this check since it does not make sense.
@if minimum_version(3.21)
      # Check to see if this line exists
      "exists_$(ci[$(i)])"
      expression => regline("^\s*($(i)$(bp).*|$(i))$",
                            $(edit.filename)),
      unless => strcmp( "true", $(edit.empty_before_use) );
@endif

@if minimum_version(3.18)
    !(cfengine_3_18_0|cfengine_3_18_1|cfengine_3_18_2)::
      "exists_$(ci[$(i)])"
        expression => regline("^\s*($(i)$(bp).*|$(i))$",
                              $(edit.filename)),
        unless => strcmp( "true", $(edit.empty_before_use) );
@endif

    (cfengine_3_15|cfengine_3_16|cfengine_3_17|cfengine_3_18_0|cfengine_3_18_1|cfengine_3_18_2|cfengine_3_19|cfengine_3_20)::
      # Version 3.15.0 does not know about the before_version macro, so we keep the same behavior
      # TODO Remove after 3.21 is no longer supported. (3.15.0 was supported when 3.21 was released)
      # Check to see if this line exists
      "exists_$(ci[$(i)])"
        expression => regline("^\s*($(i)$(bp).*|$(i))$",
                              $(edit.filename));

    any::

      # if there's more than one comment, just add new (don't know who to use)
      "multiple_comments_$(ci[$(i)])"
      expression => isgreaterthan("$(comment_matches_$(ci[$(i)]))",
                                  "1");


  replace_patterns:
      # If the line is commented out, uncomment and replace with
      # the correct value
      "^$(cp)($(i)$(bp).*|$(i))$"
             comment => "Uncommented the value '$(i)'",
        replace_with => value("$(i)$(sep)$($(v)[$(i)])"),
                  if => "!exists_$(ci[$(i)]).!replace_attempted_$(ci[$(i)])_reached.!multiple_comments_$(ci[$(i)])",
             classes => results("bundle", "uncommented_$(ci[$(i)])");

      # If the line is there with the wrong value, replace with
      # the correct value
      "^\s*($(i)$(bp)(?!$(ev[$(i)])$).*|$(i))$"
           comment => "Correct the value '$(i)'",
      replace_with => value("$(i)$(sep)$($(v)[$(i)])"),
           classes => results("bundle", "replace_attempted_$(ci[$(i)])");

  insert_lines:
      # If the line doesn't exist, or there is more than one occurrence
      # of the LHS commented out, insert a new line and try to place it
      # after the commented LHS (keep new line with old comments)
      "$(i)$(sep)$($(v)[$(i)])"
         comment => "Insert the value, marker '$(i)' exists",
        location => after("^$(cp)($(i)$(bp).*|$(i))$"),
              if => "replace_attempted_$(ci[$(i)])_reached.multiple_comments_$(ci[$(i)])";

      # If the line doesn't exist and there are no occurrences
      # of the LHS commented out, insert a new line at the eof
      "$(i)$(sep)$($(v)[$(i)])"
         comment => "Insert the value, marker '$(i)' doesn't exist",
              if => "replace_attempted_$(ci[$(i)])_reached.!multiple_comments_$(ci[$(i)]).!exists_$(ci[$(i)])";

  reports:
    verbose_mode|EXTRA::
      "$(this.bundle): Line for '$(i)' exists" if => "exists_$(ci[$(i)])";
      "$(this.bundle): Line for '$(i)' does not exist" if => "!exists_$(ci[$(i)])";
}

bundle edit_line set_config_values_matching(v,pat)
# @brief Sets the RHS of configuration items in the file of the form
#
# ```
#   LHS RHS
# ```
#
# If the line is commented out with `#`, it gets uncommented first.
# Adds a new line if none exists.
#
# @param v the fully-qualified name of an associative array containing v[LHS]="rhs"
# @param pat Only elements of `v` that match the regex `pat` are use
{
  meta:
      "tags"
      slist =>
      {
        "deprecated=3.6.0",
        "deprecation-reason=Generic reimplementation",
        "replaced-by=set_line_based"
      };

  vars:
      "allparams" slist => getindices("$(v)");
      "index"     slist => grep("$(pat)", "allparams");

      # Be careful if the index string contains funny chars
      "cindex[$(index)]" string => canonify("$(index)");

  replace_patterns:
      # If the line is there, maybe commented out, uncomment and replace with
      # the correct value
      "^\s*($(index)\s+(?!$($(v)[$(index)])).*|# ?$(index)\s+.*)$"
      comment => "Correct the value",
      replace_with => value("$(index) $($(v)[$(index)])"),
      classes => results("bundle", "replace_attempted_$(cindex[$(index)])");

  insert_lines:
      "$(index) $($(v)[$(index)])"
      if => "replace_attempted_$(cindex[$(index)])_reached";

}

##

bundle edit_line maintain_key_values(v,sep)
# @brief Sets the RHS of configuration items with an giving separator
#
# Contributed by David Lee
{
  meta:
      "tags"
      slist =>
      {
        "deprecated=3.6.0",
        "deprecation-reason=Generic reimplementation",
        "replaced-by=set_line_based"
      };

  vars:
      "index" slist => getindices("$(v)");
      # Be careful if the index string contains funny chars
      "cindex[$(index)]" string => canonify("$(index)");
      # Matching pattern for line (basically key-and-separator)
      "keypat[$(index)]" string => "\s*$(index)\s*$(sep)\s*";

      # Values may contain regexps. Escape them for replace_pattern matching.
      "ve[$(index)]" string => escape("$($(v)[$(index)])");

  classes:
      "$(cindex[$(index)])_key_in_file"
      comment => "Dynamic Class created if patterns matching",
      expression => regline("^$(keypat[$(index)]).*", "$(edit.filename)");

  replace_patterns:
      # For convergence need to use negative lookahead on value:
      # "key sep (?!value).*"
      "^($(keypat[$(index)]))(?!$(ve[$(index)])$).*"
      comment => "Replace definition of $(index)",
      replace_with => value("$(match.1)$($(v)[$(index)])");

  insert_lines:
      "$(index)$(sep)$($(v)[$(index)])"
      comment => "Insert definition of $(index)",
      if => "!$(cindex[$(index)])_key_in_file";
}

##

bundle edit_line append_users_starting(v)
# @brief For adding to `/etc/passwd` or `etc/shadow`
# @param v An array `v[username] string => "line..."`
#
# **Note:** To manage local users with CFEngine 3.6 and later,
# consider making `users` promises instead of modifying system files.
{
  vars:

      "index"        slist => getindices("$(v)");

  classes:

      "add_$(index)"     not => userexists("$(index)"),
      comment => "Class created if user does not exist";

  insert_lines:

      "$($(v)[$(index)])"

      comment => "Append users into a password file format",
      if => "add_$(index)";
}

##

bundle edit_line append_groups_starting(v)
# @brief For adding groups to `/etc/group`
# @param v An array `v[groupname] string => "line..."`
#
# **Note:** To manage local users with CFEngine 3.6 and later,
# consider making `users` promises instead of modifying system files.
{
  vars:

      "index"        slist => getindices("$(v)");

  classes:

      "add_$(index)"     not => groupexists("$(index)"),
      comment => "Class created if group does not exist";

  insert_lines:

      "$($(v)[$(index)])"

      comment => "Append users into a group file format",
      if => "add_$(index)";

}

##

bundle edit_line set_colon_field(key,field,val)
# @brief Set the value of field number `field` of the line whose
# first field is `key` to the value `val`, in a colon-separated file.
# @param key The value the first field has to match
# @param field The field to be modified
# @param val The new value of `field`
{
  field_edits:

      "$(key):.*"

      comment => "Edit a colon-separated file, using the first field as a key",
      edit_field => col(":","$(field)","$(val)","set");
}

##

bundle edit_line set_user_field(user,field,val)
# @brief Set the value of field number "field" in a `:-field`
# formatted file like `/etc/passwd`
# @param user A regular expression matching the user(s) to be modified
# @param field The field that should be modified
# @param val The value for `field`
#
# **Note:** To manage local users with CFEngine 3.6 and later,
# consider making `users` promises instead of modifying system files.
#
# **See also:**
#
# * [bundle edit_line set_escaped_user_field][lib/files.cf#set_escaped_user_field]
# * [edit_line field_edits][field_edits]
{
  field_edits:

      "$(user):.*"

        comment => "Edit a user attribute in the password file",
        edit_field => col(":","$(field)","$(val)","set");
}

##

bundle edit_line set_escaped_user_field(user,field,val)
# @brief Set the value of field number "field" in a `:-field`
# formatted file like `/etc/passwd`
# @param user The user to be modified
# @param field The field that should be modified
# @param val The value for `field`
#
# **Note:** To manage local users with CFEngine 3.6 and later,
# consider making `users` promises instead of modifying system files.
#
# **See also:**
#
# * [bundle edit_line set_user_field][lib/files.cf#set_user_field]
# * [edit_line field_edits][field_edits]
{
  vars:
      "escaped_user"
        string => escape( "$(user)" );

  field_edits:

      "$(escaped_user):.*"

        comment => "Edit a user attribute in the password file",
        edit_field => col(":","$(field)","$(val)","set");
}
##

bundle edit_line append_user_field(group,field,allusers)
# @brief For adding users to to a file like `/etc/group`
# at field position `field`, comma separated subfields
# @param group The group to be modified
# @param field The field where users should be added
# @param allusers The list of users to add to `field`
#
# **Note:** To manage local users with CFEngine 3.6 and later,
# consider making `users` promises instead of modifying system files.
{
  field_edits:

      "$(group):.*"

      comment => "Append users into a password file format",
      edit_field => col(":","$(field)","$(allusers)","alphanum");
}

##

bundle edit_line expand_template(templatefile)
# @brief Read in the named text file and expand `$(var)` inside the file
# @param templatefile The name of the file
{
  insert_lines:

      "$(templatefile)"

      insert_type => "file",
      comment => "Expand variables in the template file",
      expand_scalars => "true";
}

bundle edit_line replace_or_add(pattern,line)
# @brief Replace a pattern in a file with a single line.
#
# If the pattern is not found, add the line to the file.
#
# @param pattern The pattern that should be replaced
# The pattern must match the whole line (it is automatically
# anchored to the start and end of the line) to avoid
# ambiguity.
# @param line The line with which to replace matches of `pattern`
{
  vars:
      "cline" string => canonify("$(line)");
      "eline" string => escape("$(line)");

  replace_patterns:
      "^(?!$(eline)$)$(pattern)$"
      comment => "Replace a pattern here",
      replace_with => value("$(line)"),
      classes => results("bundle", "replace_$(cline)");

  insert_lines:
      "$(line)"
      if => "replace_$(cline)_reached";
}

bundle edit_line converge(marker, lines)
# @brief Converge `lines` marked with `marker`
#
# Any content marked with `marker` is removed, then `lines` are
# inserted.  Every `line` should contain `marker`.
#
# @param marker The marker (not a regular expression; will be escaped)
# @param lines The lines to insert; all must contain `marker`
#
# **Example:**
#
# ```cf3
# bundle agent pam_d_su_include
# #@brief Ensure /etc/pam.d/su has includes configured properly
# {
#   files:
#     ubuntu::
#       "/etc/pam.d/su"
#         edit_line => converge( "@include", "@include common-auth
# @include common-account
# @include common-session");
# }
# ```
#
# **History:**
#
# * Introduced in 3.6.0
{
  vars:
      "regex" string => escape($(marker));

  delete_lines:
      ".*$(regex).*" comment => "Delete lines matching the marker";
  insert_lines:
      "$(lines)" comment => "Insert the given lines";
}

bundle edit_line converge_prepend(marker, lines)
# @brief Converge `lines` marked with `marker` to start of content
#
# Any content marked with `marker` is removed, then `lines` are
# inserted at *start* of content.  Every `line` should contain `marker`.
#
# @param marker The marker (not a regular expression; will be escaped)
# @param lines The lines to insert; all must contain `marker`
#
# **Example:**
#
# ```cf3
# bundle agent pam_d_su_session
# #@brief Ensure /etc/pam.d/su has session configured properly
# {
#   files:
#     ubuntu::
#       "/etc/pam.d/su"
#         edit_line => converge_prepend( "session", "session       required   pam_env.so readenv=1 envfile=/etc/default/locale
# session    optional   pam_mail.so nopen
# session    required   pam_limits.so" );
# }
# ```
#
# **History:**
#
# * Introduced in 3.17.0, 3.15.3, 3.12.6
{
  vars:
      "regex" string => escape($(marker));

  delete_lines:
      ".*$(regex).*" comment => "Delete lines matching the marker";
  insert_lines:
      "$(lines)" location => start, comment => "Insert the given lines";
}


bundle edit_line fstab_option_editor(method, mount, option)
# @brief Add or remove `/etc/fstab` options for a mount
#
# This bundle edits the options field of a mount.  The `method` is a
# `field_operation` which can be `append`, `prepend`, `set`, `delete`,
# or `alphanum`.  The option is OS-specific.
#
# @param method `field_operation` to apply
# @param mount the mount point
# @param option the option to add or remove
#
# **Example:**
#
# ```cf3
#  files:
#      "/etc/fstab" edit_line => fstab_option_editor("delete", "/", "acl");
#      "/etc/fstab" edit_line => fstab_option_editor("append", "/", "acl");
# ```
{
   field_edits:
      "(?!#)\S+\s+$(mount)\s.+"
      edit_field => fstab_options($(option), $(method));
}

##-------------------------------------------------------
## editing bodies
##-------------------------------------------------------

body edit_field fstab_options(newval, method)
# @brief Edit the options field in a fstab format
# @param newval the new option
# @param method `field_operation` to apply
#
# This body edits the options field in the fstab file format.  The
# `method` is a `field_operation` which can be `append`, `prepend`,
# `set`, `delete`, or `alphanum`.  The `newval` option is OS-specific.
#
# **Example:**
#
# ```cf3
#   # from the `fstab_options_editor`
#   field_edits:
#      "(?!#)\S+\s+$(mount)\s.+"
#      edit_field => fstab_options($(option), $(method));
# ```
{
      field_separator => "\s+";
      select_field    => "4";
      value_separator  => ",";
      field_value     => "$(newval)";
      field_operation => "$(method)";
}

body edit_field quoted_var(newval,method)
# @brief Edit the quoted value of the matching line
# @param newval The new value
# @param method The method by which to edit the field (append|prepend|alphanum|set|delete)
# Ref https://docs.cfengine.com/latest/reference-promise-types-files-edit_line-field_edits.html#field_operation
{
      field_separator => "\"";
      select_field    => "2";
      value_separator  => " ";
      field_value     => "$(newval)";
      field_operation => "$(method)";
      extend_fields => "false";
      allow_blank_fields => "true";
}

##

body edit_field col(split,col,newval,method)
# @brief Edit tabluar data with comma-separated sub-values
# @param split The separator that defines columns
# @param col The (1-based) index of the value to change
# @param newval The new value
# @param method The method by which to edit the field
{
      field_separator    => "$(split)";
      select_field       => "$(col)";
      value_separator    => ",";
      field_value        => "$(newval)";
      field_operation    => "$(method)";
      extend_fields      => "true";
      allow_blank_fields => "true";
}

##

body edit_field line(split,col,newval,method)
# @brief Edit tabular data with space-separated sub-values
# @param split The separator that defines columns
# @param col The (1-based) index of the value to change
# @param newval The new value
# @param method The method by which to edit the field
{
      field_separator    => "$(split)";
      select_field       => "$(col)";
      value_separator    => " ";
      field_value        => "$(newval)";
      field_operation    => "$(method)";
      extend_fields      => "true";
      allow_blank_fields => "true";
}

##

body replace_with text_between_match1_and_match2( _text )
# @brief Replace matched line with substituted string
# @param _text String to substitute between first and second match
{
        replace_value => "$(match.1)$(_text)$(match.2)";
        occurrences => "all";
}

body replace_with value(x)
# @brief Replace matching lines
# @param x The replacement string
{
      replace_value => "$(x)";
      occurrences => "all";
}

##

body select_region INI_section(x)
# @brief Restrict the `edit_line` promise to the lines in section `[x]`
# @param x The name of the section in an INI-like configuration file
{
      select_start => "\[$(x)\]\s*";
      select_end => "\[.*\]\s*";

@if minimum_version(3.10)
      select_end_match_eof => "true";
@endif
}

##-------------------------------------------------------
## edit_defaults
##-------------------------------------------------------

body edit_defaults std_defs
# @brief Standard definitions for `edit_defaults`
# Don't empty the file before editing starts and don't make a backup.
{
      empty_file_before_editing => "false";
      edit_backup => "false";
      #max_file_size => "300000";
}

##

body edit_defaults empty
# @brief Empty the file before editing
#
# No backup is made
{
      empty_file_before_editing => "true";
      edit_backup => "false";
      #max_file_size => "300000";
}

##

body edit_defaults no_backup
# @brief Don't make a backup of the file before editing
{
      edit_backup => "false";
}

##

body edit_defaults backup_timestamp
# @brief Make a timestamped backup of the file before editing
{
      empty_file_before_editing => "false";
      edit_backup => "timestamp";
      #max_file_size => "300000";
}

##-------------------------------------------------------
## location
##-------------------------------------------------------

body location start
# @brief Editing occurs before the matched line
{
      before_after => "before";
}

##

body location after(str)
# @brief Editing occurs after the line matching `str`
# @param str Regular expression matching the file line location
{
      before_after => "after";
      select_line_matching => "$(str)";
}

##

body location before(str)
# @brief Editing occurs before the line matching `str`
# @param str Regular expression matching the file line location
{
      before_after => "before";
      select_line_matching => "$(str)";
}

##-------------------------------------------------------
## replace_with
##-------------------------------------------------------

##

body replace_with comment(c)
# @brief Comment all lines matching the pattern by preprending `c`
# @param c The prefix that comments out lines
{
      replace_value => "$(c) $(match.1)";
      occurrences => "all";
}

##

body replace_with uncomment
# @brief Uncomment all lines matching the pattern by removing
# anything outside the matching string
{
      replace_value => "$(match.1)";
      occurrences => "all";
}

##-------------------------------------------------------
## copy_from
##-------------------------------------------------------

body copy_from secure_cp(from,server)
# @brief Download a file from a remote server over an encrypted channel
#
# Only copy the file if it is different from the local copy, and verify
# that the copy is correct.
#
# @param from The location of the file on the remote server
# @param server The hostname or IP of the server from which to download
{
      source      => "$(from)";
      servers     => { "$(server)" };
      compare     => "digest";
      encrypt     => "true";
      verify      => "true";
}

##

body copy_from remote_cp(from,server)
# @brief Download a file from a remote server.
#
# @param from The location of the file on the remote server
# @param server The hostname or IP of the server from which to download
{
      servers     => { "$(server)" };
      source      => "$(from)";
      compare     => "mtime";
}

##

body copy_from remote_dcp(from,server)
# @brief Download a file from a remote server if it is different from the local copy.
#
# @param from The location of the file on the remote server
# @param server The hostname or IP of the server from which to download
#
# **See Also:** `local_dcp()`
{
      servers     => { "$(server)" };
      source      => "$(from)";
      compare     => "digest";
}

##

body copy_from local_cp(from)
# @brief Copy a file if the modification time or creation time of the source
# file is newer (the default comparison mechanism).
# @param from The path to the source file.
#
# **Example:**
#
# ```cf3
# bundle agent example
# {
#   files:
#       "/tmp/file.bak"
#         copy_from => local_cp("/tmp/file");
# }
# ```
#
# **See Also:** `local_dcp()`
{
      source      => "$(from)";
}

##

body copy_from local_dcp(from)
# @brief Copy a local file if the hash on the source file differs.
# @param from The path to the source file.
#
# **Example:**
#
# ```cf3
# bundle agent example
# {
#   files:
#       "/tmp/file.bak"
#       copy_from => local_dcp("/tmp/file");
# }
# ```
#
# **See Also:** `local_cp()`, `remote_dcp()`
{
      source      => "$(from)";
      compare     => "digest";
}

##

body copy_from perms_cp(from)
# @brief Copy a local file and preserve file permissions on the local copy.
#
# @param from The path to the source file.
{
      source      => "$(from)";
      preserve    => "true";
}

body copy_from perms_dcp(from)
# @brief Copy a local file if it is different from the existing copy and
# preserve file permissions on the local copy.
#
# @param from The path to the source file.
{
      source      => "$(from)";
      preserve    => "true";
      compare     => "digest";
}

body copy_from backup_local_cp(from)
# @brief Copy a local file and  keep a backup of old versions.
#
# @param from The path to the source file.
{
      source      => "$(from)";
      copy_backup => "timestamp";
}

##

body copy_from seed_cp(from)
# @brief Copy a local file if the file does not already exist, i.e. seed the
# placement
# @param from The path to the source file.
#
# **Example:**
#
# ```cf3
# bundle agent home_dir_init
# {
#   files:
#       "/home/mark.burgess/."
#       copy_from => seed_cp("/etc/skel"),
#       depth_search => recurse(inf),
#       file_select => all,
#       comment => "We want to be sure that the home directory has files that are
#                   present in the skeleton.";
# }
# ```
{
      source      => "$(from)";
      compare     => "exists";
}

##

body copy_from sync_cp(from,server)
# @brief Synchronize a file with a remote server.
#
# * If the file does not exist on the remote server then it should be purged.
# * Allow types to change (directories to files and vice versa).
# * The mode of the remote file should be preserved.
# * Files are compared using the default comparison (mtime or ctime).
#
# @param from The location of the file on the remote server
# @param server The hostname or IP of the server from which to download
#
# **Example**:
#
# ```cf3
# files:
#   "/tmp/masterfiles/."
#     copy_from => sync_cp( "/var/cfengine/masterfiles", $(sys.policy_server) ),
#     depth_search => recurse(inf),
#     file_select => all,
#     comment => "Mirror masterfiles from the hub to a temporary directory";
# ```
#
# **See Also:** `dir_sync()`, `copyfrom_sync()`
{
      servers     => { "$(server)" };
      source      => "$(from)";
      purge       => "true";
      preserve    => "true";
      type_check  => "false";
}

##

body copy_from no_backup_cp(from)
# @brief Copy a local file and don't make any backup of the previous version
#
# @param from The path to the source file.
{
      source      => "$(from)";
      copy_backup => "false";
}

##

body copy_from no_backup_cp_compare(from, comparison)
# @brief Copy a local file (`from`) based on comparison (`comparison`) and don't make any backup of the previous version
#
# @param from The path to the source file.
# @param comparison The comparison to use. (mtime|ctime|atime|exists|binary|hash|digest)
{
        source      => "$(from)";
        copy_backup => "false";
        compare => "$(comparison)";
}

##

body copy_from no_backup_dcp(from)
# @brief Copy a local file if contents have changed, and don't make any backup
# of the previous version
#
# @param from The path to the source file.
{
      source      => "$(from)";
      copy_backup => "false";
      compare     => "digest";
}

##

body copy_from no_backup_rcp(from,server)
# @brief Download a file if it's newer than the local copy, and don't make any
# backup of the previous version
#
# @param from The location of the file on the remote server
# @param server The hostname or IP of the server from which to download
{
      servers     => { "$(server)" };
      source      => "$(from)";
      compare     => "mtime";
      copy_backup => "false";
}

##-------------------------------------------------------
## link_from
##-------------------------------------------------------

body link_from ln_s(x)
# @brief Create a symbolink link to `x`
# The link is created even if the source of the link does not exist.
# @param x The source of the link
{
      link_type => "symlink";
      source => "$(x)";
      when_no_source => "force";
}

##

body link_from linkchildren(tofile)
# @brief Create a symbolink link to `tofile`
# If the promiser is a directory, children are linked to the source, unless
# entries with identical names already exist.
# The link is created even if the source of the link does not exist.
#
# @param tofile The source of the link
{
      source        => "$(tofile)";
      link_type     => "symlink";
      when_no_source  => "force";
      link_children => "true";
      when_linking_children => "if_no_such_file"; # "override_file";
}

body link_from linkfrom(source, type)
# @brief Make any kind of link to a file
# @param source link to this
# @param type the link's type (`symlink` or `hardlink`)
{
      source => $(source);
      link_type => $(type);
}

##-------------------------------------------------------
## perms
##-------------------------------------------------------

body perms m(mode)
# @brief Set the file mode
# @param mode The new mode
{
      mode   => "$(mode)";

#+begin_ENT-951
# Remove after 3.20 is not supported
        rxdirs => "true";
@if minimum_version(3.20)
        rxdirs => "false";
@endif
#+end
}

##

body perms mo(mode,user)
# @brief Set the file's mode and owners
# @param mode The new mode
# @param user The username of the new owner
{
      owners => { "$(user)" };
      mode   => "$(mode)";

#+begin_ENT-951
# Remove after 3.20 is not supported
        rxdirs => "true";
@if minimum_version(3.20)
        rxdirs => "false";
@endif
#+end
}

##

body perms mog(mode,user,group)
# @brief Set the file's mode, owner and group
# @param mode The new mode
# @param user The username of the new owner
# @param group The group name
{
      owners => { "$(user)" };
      groups => { "$(group)" };
      mode   => "$(mode)";

#+begin_ENT-951
# Remove after 3.20 is not supported
        rxdirs => "true";
@if minimum_version(3.20)
        rxdirs => "false";
@endif
#+end
}

##

body perms og(u,g)
# @brief Set the file's owner and group
# @param u The username of the new owner
# @param g The group name
{
      owners => { "$(u)" };
      groups => { "$(g)" };
}

##

body perms owner(user)
# @brief Set the file's owner
# @param user The username of the new owner
{
      owners => { "$(user)" };
}

body perms system_owned(mode)
# @brief Set the file owner and group to the system default
# @param mode the access permission in octal format
#
# **Example:**
#
# ```cf3
# files:
#     "/etc/passwd" perms => system_owned("0644");
# ```
{
      mode   => "$(mode)";

#+begin_ENT-951
# Remove after 3.20 is not supported
        rxdirs => "true";
@if minimum_version(3.20)
        rxdirs => "false";
@endif
#+end

    !windows::
        owners => { "root" };

    windows::

      # NOTE: Setting owners will generate an error if the policy is not being
      # executed as the user who's ownership is being targeted. While it seems
      # that should typically be Administrator or SYSTEM, both are reported to
      # result in errors by users, thus owners is currently omitted for Windows.

      # ENT-9778
      groups => { "Administrators" };

    freebsd|openbsd|netbsd|darwin::
      groups => { "wheel" };

    linux::
      groups => { "root" };

    solaris::
      groups => { "sys" };

    aix::
      groups => { "system" };
}

##-------------------------------------------------------
## ACLS (extended Unix perms)
##-------------------------------------------------------

body acl access_generic(acl)
# @brief Set the `aces` of the access control as specified
#
# Default/inherited ACLs are left unchanged. This body is
# applicable for both files and directories on all platforms.
#
# @param acl The aces to be set
{
      acl_method => "overwrite";
      aces => { "@(acl)" };

    windows::
      acl_type => "ntfs";

    !windows::
      acl_type => "posix";
}

##

body acl ntfs(acl)
# @brief Set the `aces` on NTFS file systems, and overwrite
# existing ACLs.
#
# This body requires CFEngine Enterprise.
#
# @param acl The aces to be set
{
      acl_type => "ntfs";
      acl_method => "overwrite";
      aces => { "@(acl)" };
}

##

body acl strict
# @brief Limit file access via ACLs to users with administrator privileges,
# overwriting existing ACLs.
#
# **Note:** May need to take ownership of file/dir to be sure no-one else is
# allowed access.
{
      acl_method => "overwrite";

    windows::
      aces => { "user:Administrator:rwx" };
    !windows::
      aces => { "user:root:rwx" };
}

##-------------------------------------------------------
## depth_search
##-------------------------------------------------------

body depth_search recurse(d)
# @brief Search files and direcories recursively, up to the specified depth
# Directories on different devices are excluded.
#
# @param d The maximum search depth
{
      depth => "$(d)";
      xdev  => "true";
}

##

body depth_search recurse_ignore(d,list)
# @brief Search files and directories recursively,
# but don't recurse into the specified directories
#
# @param d The maximum search depth
# @param list The list of directories to be excluded
{
      depth => "$(d)";
      exclude_dirs => { @(list) };
}

##

body depth_search include_base
# @brief Search files and directories recursively,
# starting from the base directory.
{
      include_basedir => "true";
}

body depth_search recurse_with_base(d)
# @brief Search files and directories recursively up to the specified
# depth, starting from the base directory excluding directories on
# other devices.
#
# @param d The maximum search depth
{
      depth => "$(d)";
      xdev  => "true";
      include_basedir => "true";
}

##-------------------------------------------------------
## delete
##-------------------------------------------------------

body delete tidy
# @brief Delete the file and remove empty directories
# and links to directories
{
      dirlinks => "delete";
      rmdirs   => "true";
}

##-------------------------------------------------------
## rename
##-------------------------------------------------------

body rename disable
# @brief Disable the file
{
      disable => "true";
}

##

body rename rotate(level)
# @brief Rotate and store up to `level` backups of the file
# @param level The number of backups to store
{
      rotate => "$(level)";
}

##

body rename to(file)
# @brief Rename the file to `file`
# @param file The new name of the file
{
      newname => "$(file)";
}

##-------------------------------------------------------
## file_select
##-------------------------------------------------------

body file_select name_age(name,days)
# @brief Select files that have a matching `name` and have not been modified for at least `days`
# @param name A regex that matches the file name
# @param days Number of days
{
      leaf_name   => { "$(name)" };
      mtime       => irange(0,ago(0,0,"$(days)",0,0,0));
      file_result => "mtime.leaf_name";
}

##

body file_select days_old(days)
# @brief Select files that have not been modified for at least `days`
# @param days Number of days
{
      mtime       => irange(0,ago(0,0,"$(days)",0,0,0));
      file_result => "mtime";
}

##

body file_select size_range(from,to)
# @brief Select files that have a size within the specified range
# @param from The lower bound of the allowed file size
# @param to The upper bound of the allowed file size
{
      search_size => irange("$(from)","$(to)");
      file_result => "size";
}

##

body file_select bigger_than(size)
# @brief Select files that are above a given size
# @param size The number of bytes files have
{
      search_size => irange("0","$(size)");
      file_result => "!size";
}

##

body file_select exclude(name)
# @brief Select all files except those that match `name`
# @param name A regular expression
{
      leaf_name  => { "$(name)"};
      file_result => "!leaf_name";
}

##

body file_select not_dir
# @brief Select all files that are not directories
{
      file_types => { "dir" };
      file_result => "!file_types";
}

body file_select plain
# @brief Select plain, regular files
{
      file_types  => { "plain" };
      file_result => "file_types";
}

body file_select dirs
# @brief Select directories
{
      file_types  => { "dir" };
      file_result => "file_types";
}

##

body file_select by_name(names)
# @brief Select files that match `names`
# @param names A regular expression
{
      leaf_name  => { @(names)};
      file_result => "leaf_name";
}

##

body file_select ex_list(names)
# @brief Select all files except those that match `names`
# @param names A list of regular expressions
{
      leaf_name  => { @(names) };
      file_result => "!leaf_name";
}

##

body file_select all
# @brief Select all file system entries
{
      leaf_name => { ".*" };
      file_result => "leaf_name";
}

##

body file_select older_than(years, months, days, hours, minutes, seconds)
# @brief Select files older than the date-time specified
# @param years Number of years
# @param months Number of months
# @param days Number of days
# @param hours Number of hours
# @param minutes Number of minutes
# @param seconds Number of seconds
#
# Generic older_than selection body, aimed to have a common definition handy
# for every case possible.
{
      mtime       => irange(0,ago("$(years)","$(months)","$(days)","$(hours)","$(minutes)","$(seconds)"));
      file_result => "mtime";
}

##

body file_select filetype_older_than(filetype, days)
# @brief Select files of specified type older than specified number of days
#
# @param filetype File type to select
# @param days Number of days
#
# This body only takes a single filetype, see `filetypes_older_than()`
# if you want to select more than one type of file.
{
      file_types => { "$(filetype)" };
      mtime      => irange(0,ago(0,0,"$(days)",0,0,0));
      file_result => "file_types.mtime";
}

##

body file_select filetypes_older_than(filetypes, days)
# @brief Select files of specified types older than specified number of days
#
# This body only takes a list of filetypes
#
# @param filetypes A list of file types
# @param days Number of days
#
# **See also:** `filetype_older_than()`
{
      file_types => { @(filetypes) };
      mtime      => irange(0,ago(0,0,"$(days)",0,0,0));
      file_result => "file_types.mtime";
}

body file_select symlinked_to(target)
# @brief Select symlinks that point to $(target)
# @param target The file the symlink should point to in order to be selected
{
  file_types  => { "symlink" };
  issymlinkto => { "$(target)" };
  file_result => "issymlinkto";
}

##-------------------------------------------------------
## changes
##-------------------------------------------------------

body changes detect_all_change
# @brief Detect all file changes using the best hash method
#
# This is fierce, and will cost disk cycles
#
{
      hash           => "best";
      report_changes => "all";
      update_hashes  => "yes";
}

##

body changes detect_all_change_using(hash)
# @brief Detect all file changes using a given hash method
#
# Detect all changes using a configurable hashing algorithm
# for times when you care about both content and file stats e.g. mtime
#
# @param hash supported hashing algorithm (md5, sha1, sha224, sha256, sha384, sha512, best)
{
      hash           => "$(hash)";
      report_changes => "all";
      update_hashes  => "yes";
}

##

body changes detect_content
# @brief Detect file content changes using md5
#
# This is a cheaper alternative
{
      hash           => "md5";
      report_changes => "content";
      update_hashes  => "yes";
}

##

body changes detect_content_using(hash)
# @brief Detect file content changes using a given hash algorithm.
#
# For times when you only care about content, not file stats e.g. mtime
# @param hash - supported hashing algorithm (md5, sha1, sha224, sha256, sha384,
#   sha512, best)
{
      hash           => "$(hash)";
      report_changes => "content";
      update_hashes  => "yes";
}

##

body changes noupdate
# @brief Detect content changes in (small) files that should never change
{
      hash           => "sha256";
      report_changes => "content";
      update_hashes  => "no";
}

##

body changes diff
# @brief Detect file content changes using sha256
# and report the diff to CFEngine Enterprise
{
      hash           => "sha256";
      report_changes => "content";
      report_diffs   => "true";
      update_hashes  => "yes";
}

##

body changes all_changes
# @brief Detect all file changes using sha256
# and report the diff to CFEngine Enterprise
{
      hash           => "sha256";
      report_changes => "all";
      report_diffs   => "true";
      update_hashes  => "yes";
}

##

body changes diff_noupdate
# @brief Detect content changes in (small) files
# and report the diff to CFEngine Enterprise
{
      hash           => "sha256";
      report_changes => "content";
      report_diffs   => "true";
      update_hashes  => "no";
}

# template bundles

bundle agent file_mustache(mustache_file, json_file, target_file)
# @brief Make a file from a Mustache template and a JSON file
# @param mustache_file the file with the Mustache template
# @param json_file a file with JSON data
# @param target_file the target file to write
#
# **Example:**
#
# ```cf3
# methods:
#      "m" usebundle => file_mustache("x.mustache", "y.json", "z.txt");
# ```
{
  files:
      "$(target_file)"
      create => "true",
      edit_template => $(mustache_file),
      template_data => readjson($(json_file), "100k"),
      template_method => "mustache";
}

bundle agent file_mustache_jsonstring(mustache_file, json_string, target_file)
# @brief Make a file from a Mustache template and a JSON string
# @param mustache_file the file with the Mustache template
# @param json_string a string with JSON data
# @param target_file the target file to write
#
# **Example:**
#
# ```cf3
# methods:
#      "m" usebundle => file_mustache_jsonstring("x.mustache", '{ "x": "y" }', "z.txt");
# ```
{
  files:
      "$(target_file)"
      create => "true",
      edit_template => $(mustache_file),
      template_data => parsejson($(json_string)),
      template_method => "mustache";
}

bundle agent file_tidy(file)
# @brief Remove a file
# @param file to remove
#
# **Example:**
#
# ```cf3
# methods:
#      "" usebundle => file_tidy("/tmp/z.txt");
# ```
{
  files:
      "$(file)" delete => tidy;

  reports:
    "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): deleting $(file) with delete => tidy";
}

bundle agent dir_sync(from, to)
# @brief Synchronize a directory entire, deleting unknown files
# @param from source directory
# @param to destination directory
#
# **Example:**
#
# ```cf3
# methods:
#      "" usebundle => dir_sync("/tmp", "/var/tmp");
# ```
{
  files:
      "$(to)/."
      create => "true",
      depth_search => recurse("inf"),
      copy_from => copyfrom_sync($(from));

  reports:
    "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): copying directory $(from) to $(to)";
}

bundle agent file_copy(from, to)
# @brief Copy a file
# @param from source file
# @param to destination file
#
# **Example:**
#
# ```cf3
# methods:
#      "" usebundle => file_copy("/tmp/z.txt", "/var/tmp/y.txt");
# ```
{
  files:
      "$(to)"
      copy_from => copyfrom_sync($(from));

  reports:
    "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): copying file $(from) to $(to)";
}

body copy_from copyfrom_sync(f)
# @brief Copy a directory or file with digest checksums, preserving attributes and purging leftovers
# @param f the file or directory
{
      source => "$(f)";
      purge => "true";
      preserve => "true";
      type_check => "false";
      compare => "digest";
}

bundle agent file_make(file, str)
# @brief Make a file from a string
# @param file target
# @param str the string data
#
# **Example:**
#
# ```cf3
# methods:
#      "" usebundle => file_make("/tmp/z.txt", "Some text
# and some more text here");
# ```
{
  vars:
      "len" int => string_length($(str));
    summarize::
      "summary" string => format("%s...%s",
                                string_head($(str), 18),
                                string_tail($(str), 18));
  classes:
      "summarize" expression => isgreaterthan($(len), 40);

  files:
      "$(file)"
      create => "true",
      edit_line => insert_lines($(str)),
      edit_defaults => empty;

  reports:
    "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): creating $(file) with contents '$(str)'"
      if => "!summarize";

      "DEBUG $(this.bundle): creating $(file) with contents '$(summary)'"
      if => "summarize";
}

bundle agent file_make_mog(file, str, mode, owner, group)
# @brief Make a file from a string with mode, owner, group
# @param file target
# @param str the string data
# @param mode the file permissions in octal
# @param owner the file owner as a name or UID
# @param group the file group as a name or GID
#
# **Example:**
#
# ```cf3
# methods:
#      "" usebundle => file_make_mog("/tmp/z.txt", "Some text
# and some more text here", "0644", "root", "root");
# ```
{
  vars:
      "len" int => string_length($(str));
    summarize::
      "summary" string => format("%s...%s",
                                string_head($(str), 18),
                                string_tail($(str), 18));
  classes:
      "summarize" expression => isgreaterthan($(len), 40);

  files:
      "$(file)"
      create => "true",
      edit_line => insert_lines($(str)),
      perms => mog($(mode), $(owner), $(group)),
      edit_defaults => empty;

  reports:
    "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): creating $(file) with contents '$(str)', mode '$(mode)', owner '$(owner)' and group '$(group)'"
      if => "!summarize";

      "DEBUG $(this.bundle): creating $(file) with contents '$(summary)', mode '$(mode)', owner '$(owner)' and group '$(group)'"
      if => "summarize";
}

bundle agent file_make_mustache(file, template, data)
# @brief Make a file from a mustache template
# @param file Target file to render
# @param template Path to mustache template
# @param data Data container to use
#
# **Example:**
#
# ```cf3
# vars:
#   "state" data => datastate();
#
# methods:
#      "" usebundle => file_make_mustache( "/tmp/z.txt", "/tmp/z.mustache", @(state) );
# ```
{
  files:
      "$(file)"
        create => "true",
        edit_template => "$(template)",
        template_method => "mustache",
        template_data => @(data);

  reports:
      "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): rendering $(file) with template '$(template)'";
}

bundle agent file_make_mustache_with_perms(file, template, data, mode, owner, group)
# @brief Make a file from a mustache template
# @param file Target file to render
# @param template Path to mustache template
# @param data Data container to use
# @param mode File permissions
# @param owner Target file owner
# @param group Target file group
#
# **Example:**
#
# ```cf3
# vars:
#   "state" data => datastate();
#
# methods:
#      "" usebundle => file_make_mustache( "/tmp/z.txt", "/tmp/z.mustache", @(state),
#                                          600, "root", "root" );
# ```
{
  files:
      "$(file)"
        create => "true",
        edit_template => "$(template)",
        template_method => "mustache",
        perms => mog( $(mode), $(owner), $(group) ),
        template_data => @(data);

  reports:
      "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): rendering $(file) with template '$(template)'";
}

bundle agent file_empty(file)
# @brief Make an empty file
# @param file target
#
# **Example:**
#
# ```cf3
# methods:
#      "" usebundle => file_empty("/tmp/z.txt");
# ```
{
  files:
      "$(file)"
      create => "true",
      edit_defaults => empty;

  reports:
    "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): creating empty $(file) with 0 size";
}

bundle agent file_hardlink(target, link)
# @brief Make a hard link to a file
# @param target of link
# @param link the hard link's location
#
# **Example:**
#
# ```cf3
# methods:
#      "" usebundle => file_hardlink("/tmp/z.txt", "/tmp/z.link");
# ```
{
  files:
      "$(link)"
      move_obstructions => "true",
      link_from => linkfrom($(target), "hardlink");

  reports:
    "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): $(link) will be a hard link to $(target)";
}

bundle agent file_link(target, link)
# @brief Make a symlink to a file
# @param target of symlink
# @param link the symlink's location
#
# **Example:**
#
# ```cf3
# methods:
#      "" usebundle => file_link("/tmp/z.txt", "/tmp/z.link");
# ```
{
  files:
      "$(link)"
      move_obstructions => "true",
      link_from => linkfrom($(target), "symlink");

  reports:
    "DEBUG|DEBUG_$(this.bundle)"::
      "DEBUG $(this.bundle): $(link) will be a symlink to $(target)";
}