From c1c889031953baa25f6d624f691beabe66befadc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Juhani=20Krekel=C3=A4?= Date: Fri, 31 Aug 2018 20:25:56 +0300 Subject: [PATCH] Factor error printing into one thing --- src/main-export-known-hosts.py | 34 ++++++++++++++-------------------- 1 file changed, 14 insertions(+), 20 deletions(-) diff --git a/src/main-export-known-hosts.py b/src/main-export-known-hosts.py index c5a3273..45d7e99 100644 --- a/src/main-export-known-hosts.py +++ b/src/main-export-known-hosts.py @@ -51,33 +51,27 @@ def main(): # returns, we have correct arguments args = parser.parse_args() - try: - with open(args.infile, 'r') as f: - known_host_entries = process_known_hosts.process_file(f, args.ignore_ips) - except Exception as err: - print('Error: %s' % err, file=sys.stderr) - sys.exit(1) + with open(args.infile, 'r') as f: + known_host_entries = process_known_hosts.process_file(f, args.ignore_ips) # Convert to the entry format for .sshwot files entries = [] for known_hosts_entry in known_host_entries: entries.append(process_known_hosts.known_hosts_to_entry(known_hosts_entry)) + if args.outfile is None: + # Write to stdout by default + # We use sys.stdout.buffer instead of just sys.stdout because we + # are writing binary data + write_file.write(sys.stdout.buffer, entries) + + else: + with open(args.outfile, 'wb') as outf: + write_file.write(outf, entries) + +if __name__ == '__main__': try: - if args.outfile is None: - # Write to stdout by default - # We use sys.stdout.buffer instead of just sys.stdout because we - # are writing binary data - write_file.write(sys.stdout.buffer, entries) - - else: - with open(args.outfile, 'wb') as outf: - write_file.write(outf, entries) - + main() except Exception as err: print('Error: %s' % err, file=sys.stderr) sys.exit(1) - -if __name__ == '__main__': - main() -