Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logs as table for console output and optional possible for export logs to csv #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
implement logs in the form of a table
  • Loading branch information
nickolay-github committed Mar 14, 2021
commit bcf4725859d2704b9bf702c975c0aeb9cbf52f14
15 changes: 8 additions & 7 deletions SSHKeyDistribut0r/command_line.py
Original file line number Diff line number Diff line change
@@ -16,16 +16,17 @@ def main():
print('Welcome to the world of key distribution!')
print()

parser = argparse.ArgumentParser(
description='A tool to automate key distribution with user authorization.')
parser = argparse.ArgumentParser(description='A tool to automate key distribution with user authorization.')
parser.add_argument('--dry-run', '-n', action='store_true',
help='show pending changes without applying them')
help='show pending changes without applying them')
parser.add_argument('--keys', '-k',
default='%s/%s/keys.yml' % (appdirs.user_config_dir(), prog),
help="path to keys file\n(default: '%(default)s')")
default='%s/%s/keys.yml' % (appdirs.user_config_dir(), prog),
help="path to keys file\n(default: '%(default)s')")
parser.add_argument('--server', '-s',
default='%s/%s/servers.yml' % (appdirs.user_config_dir(), prog),
help="path to server file (default: '%(default)s')")
default='%s/%s/servers.yml' % (appdirs.user_config_dir(), prog),
help="path to server file (default: '%(default)s')")
parser.add_argument('--export-csv-path', '-f',
help="path to csv export file (example: 'keys_distributor_result.csv')")
args = parser.parse_args()

try:
39 changes: 34 additions & 5 deletions SSHKeyDistribut0r/key_distribut0r.py
Original file line number Diff line number Diff line change
@@ -27,8 +27,8 @@
YAML_EXT = re.compile("^\\.ya?ml$")
JSON_EXT = re.compile("^\\.json$")

ERROR_STATUS = 'Error'
SUCCESS_STATUS = 'Success'
ERROR_STATUS = 'Error'
SUCCESS_STATUS = 'Success'


def remove_special_chars(original_string):
@@ -67,10 +67,37 @@ def read_config(config_file):
sys.exit(1)


def create_result_csv_table(messages):
def get_maximum_column_lengths(messages):
column_count = len(messages[0])
column_max_lens = {i: max(len(message[i]) for message in messages) for i in range(column_count)}
return column_max_lens


def print_table_log(messages):

messages.sort(key=lambda m: m[0] == ERROR_STATUS)

max_column_lens = get_maximum_column_lengths(messages)

def print_borderline():
column_count = len(messages[0])
empty_columns = ('' for _ in range(column_count))
print("+{:-^{lens[0]}}+{:-^{lens[1]}}+{:-^{lens[2]}}+{:-^{lens[3]}}+".format(*empty_columns,
lens=max_column_lens))
print()
print_borderline()
for message in messages[1:]:
color_on = COLOR_RED if message[0] == ERROR_STATUS else COLOR_GREEN
clear_message = (re.sub(r'\s+', ' ', col) for col in message)
print("|{color_on}{:^{lens[0]}}{color_off}"
"|{:^{lens[1]}}|{:^{lens[2]}}"
"|{:^{lens[3]}}|".format(*clear_message, color_on=color_on, color_off=COLOR_END, lens=max_column_lens))
print_borderline()


def export_to_csv(path, messages):
try:
with open('ssh_keys_distributor_result.csv', 'w', encoding='utf-8') as file:
with open(path, 'w', encoding='utf-8') as file:
writer = csv.writer(file, delimiter='|')
writer.writerows(messages)
except OSError as e:
@@ -151,5 +178,7 @@ def main(args):
msg = server['ip'], server['comment'], 'No user mentioned in configuration file!'
server_error_log(*msg)
messages.append((ERROR_STATUS, *msg))
create_result_csv_table(messages)
print_table_log(messages)
if args.export_csv_path:
export_to_csv(args.export_csv_path, messages)