Skip to content

Commit

Permalink
add diff and write params to packer_fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuchard committed Feb 24, 2025
1 parent d64ca9c commit 12072a8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
### 1.2.1 (Next)
- General code improvements.
- Improve `changed` detection for `terraform_fmt` module.
- Add `diff` and `write` parameters to `packer_fmt` module.

### 1.2.0
- Finish adding Terraform modules.
Expand Down
36 changes: 32 additions & 4 deletions plugins/modules/packer_fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@
required: false
default: false
type: bool
diff:
description: Display diffs of formatting changes.
required: false
default: false
type: bool
recursive:
description: Also process files in subdirectories. By default only the given directory (or current directory) is processed.
required: false
default: false
type: bool
write:
description: Write to source files.
required: false
default: true
type: bool
requirements:
- packer >= 1.7.0
Expand All @@ -40,10 +50,12 @@
'''

EXAMPLES = r'''
# rewrite Packer files in /path/to/packer_dir to canonical format
- name: Rewrite packer files in /path/to/packer_dir to canonical format
# rewrite Packer files in /path/to/packer_dir to canonical format, display diffs, and do not write to source files
- name: Rewrite packer files in /path/to/packer_dir to canonical format, display diffs, and do not write to source files
mschuchard.general.packer_fmt:
config_dir: /path/to/packer_dir
diff: true
write: false
# verify canonical formatting of Packer files in /path/to/packer_dir
- name: Verify canonical formatting of packer files in /path/to/packer_dir
Expand Down Expand Up @@ -76,8 +88,11 @@ def main() -> None:
argument_spec={
'check': {'type': 'bool', 'required': False},
'config_dir': {'type': 'path', 'required': False, 'default': Path.cwd()},
'recursive': {'type': 'bool', 'required': False}
'diff': {'type': 'bool', 'required': False},
'recursive': {'type': 'bool', 'required': False},
'write': {'type': 'bool', 'required': False, 'default': True},
},
mutually_exclusive=[('check', 'write')],
supports_check_mode=True
)

Expand All @@ -91,11 +106,24 @@ def main() -> None:
if check:
flags.append('check')
changed = False
if module.params.get('diff'):
flags.append('diff')
if module.params.get('recursive'):
flags.append('recursive')

# check args
args: dict = {}
# reminder: the flag that must be argued instead
# ruff complains so default should protect against falsey with None
if not module.params.get('write'):
args.update({'write': 'false'})
changed = False

# convert ansible params to terraform args
args = packer.ansible_to_packer(args)

# determine packer command
command: list[str] = packer.cmd(action='fmt', flags=flags, target_dir=config_dir)
command: list[str] = packer.cmd(action='fmt', flags=flags, args=args, target_dir=config_dir)

# exit early for check mode
if module.check_mode:
Expand Down
10 changes: 6 additions & 4 deletions tests/unit/plugins/modules/test_packer_fmt.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ def test_packer_fmt_defaults(capfd):
assert not info['stdout']


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

Expand All @@ -35,7 +35,9 @@ def test_packer_fmt_config(capfd):
info = json.loads(stdout)
assert not info['changed']
assert str(utils.fixtures_dir()) == info['command'][-1]
assert not info['stdout']
assert '-diff' in info['command']
assert '-write=false' in info['command']
assert '' == info['stdout']


def test_packer_fmt_recursive_check(capfd):
Expand Down

0 comments on commit 12072a8

Please sign in to comment.