Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
pythoninja committed Nov 1, 2023
1 parent 4e81264 commit 4744b70
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 11 deletions.
10 changes: 10 additions & 0 deletions hosts.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
test_group: # host_group
hosts:
host99: # host
ansible_host: 127.0.0.1
ansible_user: root
_meta:
_auth_type: IdentityAgent
_auth_path: ~/.1password/agent.sock
_aliases: [ "base", "another-alias" ]
_skip: false
2 changes: 1 addition & 1 deletion sshgen/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def generate_hosts_file(hosts_file: Annotated[str, typer.Option('--hosts-file',
Example usage: sshconf generate -o my_ssh_config
"""

hosts_file = FileUtils.as_file(file_path=hosts_file)
hosts_file = FileUtils.as_yaml(file_path=hosts_file)
output_file = FileUtils.as_file(file_path=output)

AppUtils(hosts_file, output_file).generate_ssh_config()
Expand Down
2 changes: 1 addition & 1 deletion sshgen/parsers/ansible.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class AnsibleHostsParser:
def __init__(self, hosts_file_path: Path | None = None):
self._yaml = YAML()
self._default_hosts_file = 'examples/hosts.yml'
self._file_path: Path = hosts_file_path or FileUtils.as_file(self._default_hosts_file)
self._file_path: Path = hosts_file_path or FileUtils.as_yaml(self._default_hosts_file)

def as_map(self) -> CommentedMap:
log.debug('Loading ansible hosts file: %s', self._file_path)
Expand Down
2 changes: 1 addition & 1 deletion sshgen/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


def run() -> None:
hosts_file = FileUtils.as_file(file_path='../examples/hosts.yml')
hosts_file = FileUtils.as_file(file_path='../examples/hosts.txt')
output_file = FileUtils.as_file(file_path='../config')

AppUtils(hosts_file, output_file).generate_ssh_config()
Expand Down
26 changes: 18 additions & 8 deletions sshgen/utils/file.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,28 @@ class FileUtils:
def as_file(file_path: str) -> Path:
resolved_path = FileUtils._get_base_path() / Path(file_path)

if FileUtils._is_yaml_file(resolved_path):
if resolved_path.exists() and FileUtils._is_empty(resolved_path):
log.error('Ansible hosts file %s is empty. Exiting...', resolved_path)
sys.exit(1)
elif not resolved_path.exists():
log.error('Ansible hosts file does not exists at %s. Exiting...', resolved_path)
sys.exit(1)

FileUtils._create_file(resolved_path)

return resolved_path

@staticmethod
def as_yaml(file_path: str) -> Path:
resolved_path = FileUtils._get_base_path() / Path(file_path)

if not FileUtils._is_yaml_file(resolved_path):
log.error('Ansible hosts file is not a yaml file. Valid extensions are: yaml or yml. Exiting...')
sys.exit(1)

if resolved_path.exists() and FileUtils._is_empty(resolved_path):
log.error('Ansible hosts file %s is empty. Exiting...', resolved_path)
sys.exit(1)

if not resolved_path.exists():
log.error('Ansible hosts file does not exists at %s. Exiting...', resolved_path)
sys.exit(1)

return resolved_path

@staticmethod
def as_package_file(file_path: str) -> Path:
return Path(__file__).resolve().parent.parent / Path(file_path)
Expand Down

0 comments on commit 4744b70

Please sign in to comment.