Skip to content

Commit

Permalink
feat: show skipped and processed hosts in output
Browse files Browse the repository at this point in the history
A new feature has been introduced in the SSH config generator where hosts that are marked to be skipped are now recorded.
  • Loading branch information
pythoninja committed Mar 8, 2024
1 parent b41f5c5 commit a5fdcbe
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion sshgen/generators/sshconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def __init__(self, models: list[HostModel], output_file: Path | None = None):
self.template_path = FileUtils.as_package_file('templates/ssh_config.template')
self.ssh_template = self._open_template()
self.output_file = output_file
self._skipped: list[HostModel] = []

def generate(self) -> None:
filtered_models: list[HostModel] = self._filter_models()
Expand All @@ -26,13 +27,14 @@ def _filter_models(self) -> list[HostModel]:
models = copy.deepcopy(self.raw_models)
log.debug('Filtering hosts where _skip metafield was defined')

filtered_models = []
filtered_models: list[HostModel] = []

for model in models:
is_skipped = model.meta_fields.skip
log.debug('Host %s should be skipped: %s', model.host, is_skipped)

if is_skipped:
self._skipped.append(model)
continue

filtered_models.append(model)
Expand Down Expand Up @@ -75,10 +77,18 @@ def _save_config(self, templates: list[str]) -> None:
self._create_output_file()
self.output_file.write_text('\n'.join(templates))
log.info('Generated SSH config file was saved to %s', self.output_file)
log.debug('Skipped hosts list: %s', self._parse_skipped())
log.info('Total processed hosts: %d, total skipped hosts: %d', len(templates), len(self._skipped))

def _create_output_file(self) -> None:
self.output_file.touch(exist_ok=True)

def _parse_skipped(self) -> str:
if len(self._skipped) > 0:
return ', '.join([model.ansible_host for model in self._skipped])

return 'no hosts found'

@staticmethod
def _get_fallback_auth() -> list[str]:
parameter = 'IdentitiesOnly yes'
Expand Down

0 comments on commit a5fdcbe

Please sign in to comment.