From e2ff5263abc77186375ed10bd7551194dfd36d3a Mon Sep 17 00:00:00 2001 From: rdb Date: Sun, 21 Jul 2024 11:50:40 +0200 Subject: [PATCH] Add login and logout commands --- toggl2moneybird/cli.py | 19 +++++++++++++++++++ toggl2moneybird/commands.py | 5 +++++ toggl2moneybird/moneybird.py | 4 ++++ 3 files changed, 28 insertions(+) diff --git a/toggl2moneybird/cli.py b/toggl2moneybird/cli.py index 3bc4188..57da570 100644 --- a/toggl2moneybird/cli.py +++ b/toggl2moneybird/cli.py @@ -19,6 +19,8 @@ def main(): console = Console() cmd_choices = [name[4:] for name in dir(commands) if name.startswith('cmd_')] + cmd_choices.append('login') + cmd_choices.append('logout') cmd_choices.append('help') parser = ArgumentParser(prog='toggl2moneybird', description='Synchronizes Toggl Track time entries with a Moneybird administration', formatter_class=RichHelpFormatter) @@ -38,6 +40,19 @@ def main(): return logname = getpass.getuser() + if args.command == 'logout': + try: + commands.tt_logout(console) + console.print("Removed [plum3][bold]toggl[/bold]track[/plum3] credentials from keyring.") + except: + console.print("No [plum3][bold]toggl[/bold]track[/plum3] credentials found in keyring.") + try: + mb.Credentials.erase_keyring(keyring, logname) + console.print("Removed [deep_sky_blue1 bold]moneybird[/deep_sky_blue1 bold] credentials from keyring.") + except: + console.print("No [deep_sky_blue1 bold]moneybird[/deep_sky_blue1 bold] credentials found in keyring.") + return + mb_creds = mb.Credentials.from_keyring(keyring, logname) if not mb_creds: mb_creds = mb.authenticate() @@ -52,5 +67,9 @@ def main(): console.print("Logged into [deep_sky_blue1 bold]moneybird[/deep_sky_blue1 bold] administration", mb_admin) + if args.command == 'login': + commands.tt_login(console) + return + cmd_func = getattr(commands, 'cmd_' + args.command) return cmd_func(console, args, mb_admin) diff --git a/toggl2moneybird/commands.py b/toggl2moneybird/commands.py index 61a41cd..72215c3 100644 --- a/toggl2moneybird/commands.py +++ b/toggl2moneybird/commands.py @@ -122,6 +122,11 @@ def tt_login(console): return tt +def tt_logout(console): + logname = getpass.getuser() + keyring.delete_password('name.rdb.toggl2moneybird.toggl-api-token', logname) + + def mb_entry_data_table(entries_data, mb_admin, **kwargs): table = Table(**kwargs) table.add_column("date", justify="right", style="green", no_wrap=True) diff --git a/toggl2moneybird/moneybird.py b/toggl2moneybird/moneybird.py index 152d3c6..9ecac80 100644 --- a/toggl2moneybird/moneybird.py +++ b/toggl2moneybird/moneybird.py @@ -588,6 +588,10 @@ def store_keyring(self, keyring, username): data += '|' + self.refresh_token keyring.set_password('name.rdb.toggl2moneybird.moneybird-credentials', username, data) + @staticmethod + def erase_keyring(keyring, username): + keyring.delete_password('name.rdb.toggl2moneybird.moneybird-credentials', username) + def authenticate(refresh_token=None): # Spin up a temporary web server to handle the redirect from Moneybird.