Skip to content

Commit

Permalink
Add login and logout commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rdb committed Jul 21, 2024
1 parent 4a481d9 commit e2ff526
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
19 changes: 19 additions & 0 deletions toggl2moneybird/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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)
5 changes: 5 additions & 0 deletions toggl2moneybird/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions toggl2moneybird/moneybird.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e2ff526

Please sign in to comment.