diff --git a/autorepo/commands/auth.py b/autorepo/commands/auth.py index 3033bfa..a20c7e8 100644 --- a/autorepo/commands/auth.py +++ b/autorepo/commands/auth.py @@ -5,7 +5,7 @@ from autorepo.utils import (add_user, delete_auth_token, get_current_user, remove_user, set_auth_token, set_current_user, - token_exists) + set_default_current_user, token_exists) @click.command( @@ -40,6 +40,7 @@ def login_cmd(): ) def logout_cmd(): current_user = get_current_user() + if not current_user: click.echo("You are not logged in to any GitHub account", err=True) @@ -47,6 +48,7 @@ def logout_cmd(): delete_auth_token(current_user) remove_user(current_user) + set_default_current_user() click.echo(f"Logged out of {current_user}") diff --git a/autorepo/commands/list.py b/autorepo/commands/list.py index 4f73af5..6cc6f89 100644 --- a/autorepo/commands/list.py +++ b/autorepo/commands/list.py @@ -21,15 +21,16 @@ def list_group(): def users_cmd(): users = list_users() - if not users: + if len(users) == 0: click.echo("No users have logged in to autorepo") return - click.echo("Users:\n") + click.echo(f"{len(users)} user(s) have logged in to autorepo\n") + click.echo("Users:") - for user in users: - click.echo(f"{user}\n") + for i, user in enumerate(users): + click.echo(f"{i+1}: {user}") @click.command( diff --git a/autorepo/utils/__init__.py b/autorepo/utils/__init__.py index 812456b..8646c6a 100644 --- a/autorepo/utils/__init__.py +++ b/autorepo/utils/__init__.py @@ -1,7 +1,7 @@ from autorepo.utils.auth import (add_user, delete_auth_token, get_auth_token, get_current_user, list_users, remove_user, set_auth_token, set_current_user, - token_exists) + set_default_current_user, token_exists) from autorepo.utils.list import (list_gitignore_templates, list_licenses, list_repositories) from autorepo.utils.repo import (add_remote, clone_repo, create_repo, diff --git a/autorepo/utils/auth.py b/autorepo/utils/auth.py index 255e2ab..69773b6 100644 --- a/autorepo/utils/auth.py +++ b/autorepo/utils/auth.py @@ -1,53 +1,126 @@ import keyring -def get_auth_token(username): - return keyring.get_password("autorepo", f"gh_token_{username}") +def get_auth_token(username: str) -> str | None: + try: + token = keyring.get_password("autorepo", f"gh_token_{username}") + except Exception: + return None + return token -def set_auth_token(username, token): - keyring.set_password("autorepo", f"gh_token_{username}", token) +def token_exists(username: str) -> bool: + return get_auth_token(username) is not None -def delete_auth_token(username): - keyring.delete_password("autorepo", f"gh_token_{username}") +def set_auth_token(username: str, token: str) -> bool: + try: + keyring.set_password("autorepo", f"gh_token_{username}", token) -def set_current_user(username): - keyring.set_password("autorepo", "current_user", username) + return True + except Exception: + return False -def get_current_user(): - return keyring.get_password("autorepo", "current_user") +def delete_auth_token(username: str) -> bool: + if not token_exists(username): + return False + try: + keyring.delete_password("autorepo", f"gh_token_{username}") -def token_exists(username): - return get_auth_token(username) is not None + return True + except Exception: + return False -def list_users(): - users = keyring.get_password("autorepo", "all_users") - if users is None: - return [] +def set_current_user(username: str) -> bool: + if not token_exists(username): + return False + + try: + keyring.set_password("autorepo", "current_user", username) + + return True + except Exception: + return False + + +def get_current_user() -> str | None: + try: + username = keyring.get_password("autorepo", "current_user") + except Exception: + return None + + return username + + +def delete_current_user() -> bool: + try: + keyring.delete_password("autorepo", "current_user") + + return True + except Exception: + return False + - return users.split(";") +def list_users() -> list: + try: + users = keyring.get_password("autorepo", "all_users") + if users == "" or users == " " or users is None: + return [] + users = users.split(";") -def add_user(username): + return users + except Exception: + return [] + + +def add_user(username: str) -> bool: users = list_users() if username in users: - return + return False users.append(username) - keyring.set_password("autorepo", "all_users", ";".join(users)) + try: + keyring.set_password("autorepo", "all_users", ";".join(users)) + return True + except Exception: + return False -def remove_user(username): + +def remove_user(username: str) -> bool: users = list_users() if username not in users: - return + return False + + users.pop(users.index(username)) + + try: + keyring.set_password("autorepo", "all_users", ";".join(users)) + + return True + except Exception: + return False + + +def set_default_current_user() -> bool: + users = list_users() + if len(users) == 0: + delete_current_user() + + return False + + return set_current_user(users[0]) - users.remove(username) - keyring.set_password("autorepo", "all_users", ";".join(users)) +if __name__ == "__main__": + print(list_users()) + print(len(list_users())) + print(get_current_user()) + print(delete_current_user()) + print(get_current_user()) diff --git a/pyproject.toml b/pyproject.toml index 4e4cd60..0b53581 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "autorepo" -version = "0.1.7" +version = "0.1.8" description = "AutoRepo is a simple, cli-based tool for creating github repositories, without leaving your terminal!" authors = ["XanderWatson "] readme = "README.md"