add true/false fail checks to click commands

previously click would print stuff like "X done succesfully" after error
now it'll be more clear that a failure has occured
This commit is contained in:
Lili (Tlapka) 2023-06-26 13:40:26 +02:00
parent d1416d3706
commit ae42a052de
1 changed files with 27 additions and 20 deletions

View File

@ -35,6 +35,8 @@ def insert_counters(db):
db.commit() db.commit()
except sqlite3.Error as e: except sqlite3.Error as e:
print("Failed inserting counters to db:", e.args[0]) print("Failed inserting counters to db:", e.args[0])
return False
return True
def init_db(): def init_db():
@ -43,7 +45,8 @@ def init_db():
with current_app.open_resource('schema.sql') as f: with current_app.open_resource('schema.sql') as f:
db.executescript(f.read().decode('utf8')) db.executescript(f.read().decode('utf8'))
insert_counters(db) if insert_counters(db):
return True
def clear_redeem_queue(): def clear_redeem_queue():
@ -59,6 +62,8 @@ def clear_redeem_queue():
db.commit() db.commit()
except sqlite3.Error as e: except sqlite3.Error as e:
print("Error occured deleting redeem queue:", e.args[0]) print("Error occured deleting redeem queue:", e.args[0])
return False
return True
def refresh_counters(): def refresh_counters():
@ -69,7 +74,9 @@ def refresh_counters():
db.commit() db.commit()
except sqlite3.Error as e: except sqlite3.Error as e:
print("Error occured deleting old counters:", e.args[0]) print("Error occured deleting old counters:", e.args[0])
insert_counters(db) return False
if insert_counters(db):
return True
def refresh_milestones(): def refresh_milestones():
@ -90,6 +97,7 @@ def refresh_milestones():
db.commit() db.commit()
except sqlite3.Error as e: except sqlite3.Error as e:
print("Failed deleting old milestones from db:", e.args[0]) print("Failed deleting old milestones from db:", e.args[0])
return False
# add new milestones # add new milestones
try: try:
@ -114,12 +122,14 @@ def refresh_milestones():
db.commit() db.commit()
except sqlite3.Error as e: except sqlite3.Error as e:
print("Failed inserting milestones to db:", e.args[0]) print("Failed inserting milestones to db:", e.args[0])
return False
return True
def reset_milestone(milestone): def reset_milestone(milestone):
if not redeem_name in current_app.config['REDEEMS']: if not redeem_name in current_app.config['REDEEMS']:
print(f"Failed resetting milestone, {milestone} not in redeems file.") print(f"Failed resetting milestone, {milestone} not in redeems file.")
return None return False
try: try:
db.execute( db.execute(
"DELETE FROM milestones WHERE name = ?", "DELETE FROM milestones WHERE name = ?",
@ -133,7 +143,7 @@ def reset_milestone(milestone):
return True return True
except Error as e: except Error as e:
current_app.logger.error(f"Error occured adding a milestone: {e.args[0]}") current_app.logger.error(f"Error occured adding a milestone: {e.args[0]}")
return None return False
@ -141,7 +151,7 @@ def reset_milestone(milestone):
@with_appcontext @with_appcontext
def init_db_command(): def init_db_command():
"""Clear the existing data and create new tables.""" """Clear the existing data and create new tables."""
init_db() if init_db():
click.echo('Initialized the database.') click.echo('Initialized the database.')
@ -149,7 +159,7 @@ def init_db_command():
@with_appcontext @with_appcontext
def clear_queue_command(): def clear_queue_command():
"""Remove all redeems from the redeem queue.""" """Remove all redeems from the redeem queue."""
clear_redeem_queue() if clear_redeem_queue():
click.echo('Cleared redeem queue.') click.echo('Cleared redeem queue.')
@ -158,7 +168,7 @@ def clear_queue_command():
def refresh_counters_command(): def refresh_counters_command():
"""Refresh counters from current config file. """Refresh counters from current config file.
(Remove old ones, add new ones.)""" (Remove old ones, add new ones.)"""
refresh_counters() if refresh_counters():
click.echo('Counters refreshed.') click.echo('Counters refreshed.')
@ -166,8 +176,7 @@ def refresh_counters_command():
@with_appcontext @with_appcontext
def refresh_and_clear_command(): def refresh_and_clear_command():
"""Refresh counters and clear queue.""" """Refresh counters and clear queue."""
refresh_counters() if refresh_counters() and clear_redeem_queue():
clear_redeem_queue()
click.echo('Counters refreshed and queue cleared.') click.echo('Counters refreshed and queue cleared.')
@ -176,7 +185,7 @@ def refresh_and_clear_command():
def refresh_milestones_command(): def refresh_milestones_command():
"""Initialize all milestones from the redeems file, """Initialize all milestones from the redeems file,
delete milestones not in redeem file.""" delete milestones not in redeem file."""
refresh_milestones() if refresh_milestones():
click.echo('Refreshed milestones.') click.echo('Refreshed milestones.')
@ -187,20 +196,18 @@ def reset_milestone_command(milestone):
if milestone_complete(milestone): if milestone_complete(milestone):
if reset_milestone(milestone): if reset_milestone(milestone):
click.echo(f"Reset milestone {milestone}.") click.echo(f"Reset milestone {milestone}.")
else:
click.echo(f"Resetting milestone {milestone} failed.")
else: else:
click.echo(f"Could not reset milestone {milestone}, milestone not completed.") click.echo(f"Could not reset milestone {milestone}, milestone not completed.")
click.echo("(You can hard-reset-milestone if you really want to reset it.)") click.echo("(You can hard-reset-milestone if you really want to reset it.)")
@click.command('hard-reset-milestone') @click.command('hard-reset-milestone')
@click.argument('milestone') @click.argument('milestone')
def hard_reset_milestone_command(milestone): def hard_reset_milestone_command(milestone):
"""Resets any milestone back to zero.""" """Resets any milestone back to zero."""
if reset_milestone(milestone): if reset_milestone(milestone):
click.echo(f"Hard reset milestone {milestone}.") click.echo(f"Hard reset milestone {milestone}.")
else:
click.echo(f"Hard resetting milestone {milestone} failed.")
def init_app(app): def init_app(app):
app.teardown_appcontext(close_db) app.teardown_appcontext(close_db)