Skip to content

Commit 12072a8

Browse files
committed
add diff and write params to packer_fmt
1 parent d64ca9c commit 12072a8

File tree

3 files changed

+39
-8
lines changed

3 files changed

+39
-8
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
### 1.2.1 (Next)
22
- General code improvements.
33
- Improve `changed` detection for `terraform_fmt` module.
4+
- Add `diff` and `write` parameters to `packer_fmt` module.
45

56
### 1.2.0
67
- Finish adding Terraform modules.

plugins/modules/packer_fmt.py

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,21 @@
2727
required: false
2828
default: false
2929
type: bool
30+
diff:
31+
description: Display diffs of formatting changes.
32+
required: false
33+
default: false
34+
type: bool
3035
recursive:
3136
description: Also process files in subdirectories. By default only the given directory (or current directory) is processed.
3237
required: false
3338
default: false
3439
type: bool
40+
write:
41+
description: Write to source files.
42+
required: false
43+
default: true
44+
type: bool
3545
3646
requirements:
3747
- packer >= 1.7.0
@@ -40,10 +50,12 @@
4050
'''
4151

4252
EXAMPLES = r'''
43-
# rewrite Packer files in /path/to/packer_dir to canonical format
44-
- name: Rewrite packer files in /path/to/packer_dir to canonical format
53+
# rewrite Packer files in /path/to/packer_dir to canonical format, display diffs, and do not write to source files
54+
- name: Rewrite packer files in /path/to/packer_dir to canonical format, display diffs, and do not write to source files
4555
mschuchard.general.packer_fmt:
4656
config_dir: /path/to/packer_dir
57+
diff: true
58+
write: false
4759
4860
# verify canonical formatting of Packer files in /path/to/packer_dir
4961
- name: Verify canonical formatting of packer files in /path/to/packer_dir
@@ -76,8 +88,11 @@ def main() -> None:
7688
argument_spec={
7789
'check': {'type': 'bool', 'required': False},
7890
'config_dir': {'type': 'path', 'required': False, 'default': Path.cwd()},
79-
'recursive': {'type': 'bool', 'required': False}
91+
'diff': {'type': 'bool', 'required': False},
92+
'recursive': {'type': 'bool', 'required': False},
93+
'write': {'type': 'bool', 'required': False, 'default': True},
8094
},
95+
mutually_exclusive=[('check', 'write')],
8196
supports_check_mode=True
8297
)
8398

@@ -91,11 +106,24 @@ def main() -> None:
91106
if check:
92107
flags.append('check')
93108
changed = False
109+
if module.params.get('diff'):
110+
flags.append('diff')
94111
if module.params.get('recursive'):
95112
flags.append('recursive')
96113

114+
# check args
115+
args: dict = {}
116+
# reminder: the flag that must be argued instead
117+
# ruff complains so default should protect against falsey with None
118+
if not module.params.get('write'):
119+
args.update({'write': 'false'})
120+
changed = False
121+
122+
# convert ansible params to terraform args
123+
args = packer.ansible_to_packer(args)
124+
97125
# determine packer command
98-
command: list[str] = packer.cmd(action='fmt', flags=flags, target_dir=config_dir)
126+
command: list[str] = packer.cmd(action='fmt', flags=flags, args=args, target_dir=config_dir)
99127

100128
# exit early for check mode
101129
if module.check_mode:

tests/unit/plugins/modules/test_packer_fmt.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ def test_packer_fmt_defaults(capfd):
2323
assert not info['stdout']
2424

2525

26-
def test_packer_fmt_config(capfd):
27-
"""test packer fmt with config"""
28-
utils.set_module_args({'config_dir': str(utils.fixtures_dir())})
26+
def test_packer_fmt_config_diff_write(capfd):
27+
"""test packer fmt with config diff write"""
28+
utils.set_module_args({'diff': True, 'write': False, 'config_dir': str(utils.fixtures_dir())})
2929
with pytest.raises(SystemExit, match='0'):
3030
packer_fmt.main()
3131

@@ -35,7 +35,9 @@ def test_packer_fmt_config(capfd):
3535
info = json.loads(stdout)
3636
assert not info['changed']
3737
assert str(utils.fixtures_dir()) == info['command'][-1]
38-
assert not info['stdout']
38+
assert '-diff' in info['command']
39+
assert '-write=false' in info['command']
40+
assert '' == info['stdout']
3941

4042

4143
def test_packer_fmt_recursive_check(capfd):

0 commit comments

Comments
 (0)