Skip to content

Commit d3621a9

Browse files
committed
✨ document the new feature. moremoban/moban#217, #1
1 parent 02685b9 commit d3621a9

File tree

11 files changed

+167
-10
lines changed

11 files changed

+167
-10
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
targets:
2+
# remove a line from a file
3+
- output: absent.txt
4+
template: absent_file.txt
5+
template_type:
6+
base_type: lineinfile
7+
options:
8+
state: absent
9+
regexp: "^remove.*$"
10+
# ensure a line in a file
11+
- output: ensure.txt
12+
template: ensure_file.txt
13+
template_type:
14+
base_type: lineinfile
15+
options:
16+
regexp: "^code="
17+
line: "code=C"
18+
# append a line to a file
19+
- output: append.txt
20+
template: append_file.txt
21+
template_type:
22+
base_type: lineinfile
23+
options:
24+
line: "127.0.0.1 append"
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
level 1 - lineinfile
2+
======================
3+
4+
It has been requested to have line in file feature from ansible. So far, it supports three parameters.
5+
6+
7+
============ =================== =================================================================
8+
Parameter Choices/Defaults Comments
9+
============ =================== =================================================================
10+
state 'present', 'absent' Whether the line should be there or not.
11+
regrexp The regular expression to look for in every line of the file.
12+
line The line to insert/replace into the file.
13+
14+
15+
Here are currently supported features::
16+
17+
targets:
18+
# remove a line from a file
19+
- output: absent.txt
20+
template: absent_file.txt
21+
template_type:
22+
base_type: lineinfile
23+
options:
24+
state: absent
25+
regexp: "^remove.*$"
26+
# ensure a line in a file
27+
- output: ensure.txt
28+
template: ensure_file.txt
29+
template_type:
30+
base_type: lineinfile
31+
options:
32+
regexp: "^code="
33+
line: "code=C"
34+
# append a line to a file
35+
- output: append.txt
36+
template: append_file.txt
37+
template_type:
38+
base_type: lineinfile
39+
options:
40+
line: "127.0.0.1 append"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
remove me
2+
keep me
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
192.168.0.1 abc
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[configuration]
2+
author=A
3+
code=B

moban_ansible/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
ANSIBLE_LIBRARIES = "^moban_ansible_.+$"
66
ANSIBLE_EXTENSIONS = [
77
"moban_ansible.tests.files",
8+
"moban_ansible.engines.line_in_file",
89
]
910

1011

moban_ansible/engines/line_in_file.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@
55

66
@ContentProcessor("lineinfile", "Checking", "Checked")
77
def line_in_file(content: str, options: dict) -> str:
8+
content = content.decode()
89
regexp = options.get("regexp")
9-
create = options.get("create", False)
1010
state = options.get("state", "present")
1111

1212
lines = content.split("\n")
1313

1414
if state == "present":
1515
if regexp:
1616
lines = present(lines, options)
17-
elif create:
17+
else:
1818
# regexp is None or ''
1919
# append the line to the end
2020
line = options.get("line")
@@ -24,7 +24,8 @@ def line_in_file(content: str, options: dict) -> str:
2424
else:
2525
lines = list(absent(lines, options))
2626

27-
return "\n".join(lines)
27+
content = "\n".join(lines)
28+
return content.encode()
2829

2930

3031
def present(lines, options):

tests/__init__.py

Whitespace-only changes.

tests/test_docs.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from .utils import Docs
2+
3+
4+
class TestRegression(Docs):
5+
def setUp(self):
6+
super(TestRegression, self).setUp()
7+
self.base_folder = "docs"
8+
9+
def test_level_1(self):
10+
ensure = "[configuration]\nauthor=A\ncode=C\n"
11+
absent = "keep me\n"
12+
append = "192.168.0.1 abc\n\n127.0.0.1 append"
13+
14+
folder = "level-1-line-in-file"
15+
file_content_pairs = [
16+
("ensure.txt", ensure),
17+
("absent.txt", absent),
18+
("append.txt", append),
19+
]
20+
self.run_moban(["moban"], folder, file_content_pairs)

tests/test_line_in_file.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,33 +6,33 @@
66
def test_append_a_line():
77
options = {"create": "yes", "line": "192.168.1.1 foo.bar foo"}
88

9-
content = "127.0.0.1 localhost"
9+
content = b"127.0.0.1 localhost"
1010

1111
new_content = line_in_file(content, options)
12-
expected = [content, options["line"]]
12+
expected = "\n".join([content.decode(), options["line"]])
1313

14-
eq_(new_content, "\n".join(expected))
14+
eq_(new_content, expected.encode())
1515

1616

1717
def test_absent():
1818
absent_line = "192.168.1.1 foo.bar foo"
1919
options = {"regexp": ".*foo$", "state": "absent"}
2020

21-
content = f"127.0.0.1 localhost\n{absent_line}"
21+
content = f"127.0.0.1 localhost\n{absent_line}".encode()
2222

2323
new_content = line_in_file(content, options)
2424
expected = "127.0.0.1 localhost"
2525

26-
eq_(new_content, expected)
26+
eq_(new_content, expected.encode())
2727

2828

2929
def test_present():
3030
present_line = "192.168.1.1 foo.bar foo"
3131
options = {"regexp": ".*foo$", "line": present_line}
3232

33-
content = f"127.0.0.1 localhost\n192.162.1.1 foo"
33+
content = b"127.0.0.1 localhost\n192.162.1.1 foo"
3434

3535
new_content = line_in_file(content, options)
3636
expected = ["127.0.0.1 localhost", present_line]
3737

38-
eq_(new_content, "\n".join(expected))
38+
eq_(new_content, "\n".join(expected).encode())

0 commit comments

Comments
 (0)