Factor error printing into one thing

This commit is contained in:
Juhani Krekelä 2018-08-31 20:25:56 +03:00
parent 3ef3321137
commit c1c8890319
1 changed files with 14 additions and 20 deletions

View File

@ -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()