Skip to content

Commit

Permalink
Switches to poetry for package management, adds user management, adds…
Browse files Browse the repository at this point in the history
… command to delete a repository, updates version to 0.1.5
  • Loading branch information
SaahilNotSahil committed Jan 8, 2024
1 parent f23da89 commit 842812b
Show file tree
Hide file tree
Showing 24 changed files with 968 additions and 616 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ jobs:
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install build
python -m pip install pipx
pipx install poetry
- name: Build package
run: python -m build
run: poetry build --format wheel
- name: Check package
run: poetry check
- name: Publish package
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
with:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ dmypy.json

# Pyre type checker
.pyre/

.vscode/
15 changes: 0 additions & 15 deletions Pipfile

This file was deleted.

479 changes: 0 additions & 479 deletions Pipfile.lock

This file was deleted.

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AutoRepo
# AutoRepo (v0.1.5)

### AutoRepo is a simple, cli-based tool for creating github repositories, without leaving your terminal!
### AutoRepo is a simple, cli-based tool for creating GitHub repositories, without leaving your terminal!

# Installation

Expand All @@ -24,7 +24,7 @@ $ pipx install autorepo
$ repo --help
```

- Using any command (displayed in the help message) requires you to first be logged in to your github account using a Personal Access Token (this is a one-time thing). You can generate a PAT by following the instructions <a href="https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/">here</a>:
- Using any command (displayed in the help message) requires you to first be logged in to your GitHub account using a Personal Access Token (this is a one-time thing). You can generate a PAT by following the instructions <a href="https://help.github.com/articles/creating-a-personal-access-token-for-the-command-line/">here</a>:

```
$ repo login
Expand All @@ -36,14 +36,14 @@ $ repo login
$ repo create [OPTIONS] NAME
```

- To convert an existing project directory into a github repository, run the following command (the `-e` flag is used to specify that the directory contains an existing project):
- To convert an existing project directory into a GitHub repository, run the following command (the `-e` flag is used to specify that the directory contains an existing project):

```
$ cd <project_directory>
$ repo create -e [OPTIONS] NAME
```

- To logout of your github account, run the following command:
- To logout of your GitHub account, run the following command:

```
$ repo logout
Expand Down
1 change: 1 addition & 0 deletions autorepo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from autorepo.commands import autorepo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import click

from .auth import login_cmd, logout_cmd
from .list import list_group
from .repo import clone_cmd, create_cmd
from autorepo.commands.auth import login_cmd, logout_cmd
from autorepo.commands.list import list_group
from autorepo.commands.repo import clone_cmd, create_cmd, delete_cmd


@click.group(
Expand All @@ -17,3 +17,4 @@ def autorepo():
autorepo.add_command(list_group)
autorepo.add_command(clone_cmd)
autorepo.add_command(create_cmd)
autorepo.add_command(delete_cmd)
95 changes: 95 additions & 0 deletions autorepo/commands/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import getpass

import click
from github import BadCredentialsException, Github

from autorepo.utils import (add_user, delete_auth_token, get_current_user,
remove_user, set_auth_token, set_current_user,
token_exists)


@click.command(
name="login",
help="Log in to GitHub using a Personal Access Token"
)
def login_cmd():
click.echo("Please enter your GitHub Personal Access Token")
token = getpass.getpass(prompt="Token: ")

try:
gh = Github(token)
username = gh.get_user().login
except BadCredentialsException:
click.echo("Invalid token", err=True)

return

set_auth_token(username, token)

click.echo(f"Logged in as {username}")

add_user(username)
set_current_user(username)

click.echo(f"{username} is now the current user")


@click.command(
name="logout",
help="Log the current user out of GitHub"
)
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)

return

delete_auth_token(current_user)
remove_user(current_user)

click.echo(f"Logged out of {current_user}")


@click.command(
name="current-user",
help="Show the current GitHub user"
)
def current_user_cmd():
current_user = get_current_user()
if not current_user:
click.echo("You are not logged in to any GitHub account", err=True)

return

click.echo(current_user)


@click.command(
name="switch-user",
help="Switch the current GitHub user"
)
@click.argument("username", required=True)
def switch_user_cmd(username):
if not token_exists(username):
click.echo(
f"Please enter your GitHub Personal Access Token for {username}"
)
token = getpass.getpass(prompt="Token: ")

try:
gh = Github(token)
username = gh.get_user().login
except BadCredentialsException:
click.echo("Invalid token", err=True)

return

set_auth_token(username, token)
add_user(username)

click.echo(f"Logged in as {username}")

set_current_user(username)

click.echo(f"Switched to {username} as the current user")
23 changes: 21 additions & 2 deletions src/autorepo/commands/list.py → autorepo/commands/list.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import click
from github import Github

from ..utils import (get_auth_token, list_gitignore_templates, list_licenses,
list_repositories)
from autorepo.utils import (get_auth_token, list_gitignore_templates,
list_licenses, list_repositories, list_users)


@click.group(
Expand All @@ -13,6 +13,24 @@ def list_group():
pass


@click.command(
name="users",
help="List the users that have logged in to autorepo"
)
def users_cmd():
users = list_users()

if not users:
click.echo("No users have logged in to autorepo")

return

click.echo("Users:\n")

for user in users:
click.echo(f"{user}\n")


@click.command(
name="licenses",
help="List the licenses available on GitHub"
Expand Down Expand Up @@ -55,6 +73,7 @@ def repositories_cmd(user):
list_repositories(gh, user)


list_group.add_command(users_cmd)
list_group.add_command(licenses_cmd)
list_group.add_command(gitignore_cmd)
list_group.add_command(repositories_cmd)
28 changes: 27 additions & 1 deletion src/autorepo/commands/repo.py → autorepo/commands/repo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import click

from ..utils import add_remote, clone_repo, create_repo, init_repo
from autorepo.utils import (add_remote, clone_repo, create_repo, delete_repo,
init_repo)


@click.command(
Expand Down Expand Up @@ -142,3 +143,28 @@ def create_cmd(
return

click.echo("Repository created and cloned successfully")


@click.command(
name="delete",
help="Delete a repository"
)
@click.argument(
"name",
required=True
)
@click.option(
"--organization",
"-o",
default=None,
help="Delete the repository from an organization"
)
def delete_cmd(name, organization):
retcode = delete_repo(name, organization)

if retcode is None:
click.echo("Failed to delete the repository", err=True)

return

click.echo("Repository deleted successfully")
8 changes: 8 additions & 0 deletions autorepo/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
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)
from autorepo.utils.list import (list_gitignore_templates, list_licenses,
list_repositories)
from autorepo.utils.repo import (add_remote, clone_repo, create_repo,
delete_repo, init_repo)
53 changes: 53 additions & 0 deletions autorepo/utils/auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import keyring


def get_auth_token(username):
return keyring.get_password("autorepo", f"gh_token_{username}")


def set_auth_token(username, token):
keyring.set_password("autorepo", f"gh_token_{username}", token)


def delete_auth_token(username):
keyring.delete_password("autorepo", f"gh_token_{username}")


def set_current_user(username):
keyring.set_password("autorepo", "current_user", username)


def get_current_user():
return keyring.get_password("autorepo", "current_user")


def token_exists(username):
return get_auth_token(username) is not None


def list_users():
users = keyring.get_password("autorepo", "all_users")
if users is None:
return []

return users.split(";")


def add_user(username):
users = list_users()
if username in users:
return

users.append(username)

keyring.set_password("autorepo", "all_users", ";".join(users))


def remove_user(username):
users = list_users()
if username not in users:
return

users.remove(username)

keyring.set_password("autorepo", "all_users", ";".join(users))
2 changes: 1 addition & 1 deletion src/autorepo/utils/list.py → autorepo/utils/list.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import click
from github import Github

from .auth import get_auth_token
from autorepo.utils.auth import get_auth_token


def pretty_print_licenses(item_dict):
Expand Down
43 changes: 42 additions & 1 deletion src/autorepo/utils/repo.py → autorepo/utils/repo.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import click
from github import Github

from .auth import get_auth_token
from autorepo.utils.auth import get_auth_token


def clone_repo(url=None, user=None, repo=None):
Expand Down Expand Up @@ -122,3 +122,44 @@ def add_remote(url, name="origin"):
click.echo("Remote added successfully")

return 0


def delete_repo(name, organization=None):
token = get_auth_token()

if not token:
click.echo(
"You must be logged in to delete a repository",
err=True
)

return None

gh = Github(token)

if organization:
try:
user = gh.get_organization(organization)
except Exception:
click.echo(
"The organization provided does not exist",
err=True
)

return None
else:
user = gh.get_user()

try:
repo = gh.get_repo(f"{user.login}/{name}")
except Exception:
click.echo(
"The repository provided does not exist",
err=True
)

return None

repo.delete()

return 0
Loading

0 comments on commit 842812b

Please sign in to comment.