Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ dependencies = [
"requests>=2.31.0",
"rich>=14.0.0",
"dynaconf>=3.2.11",
"prettytable>=3.16.0",
]
readme = "README.md"
requires-python = ">= 3.11.5"
Expand Down
19 changes: 11 additions & 8 deletions src/github_rest_cli/api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import requests
from github_rest_cli.globals import GITHUB_URL, get_headers
from github_rest_cli.utils import rich_output, rprint
from github_rest_cli.utils import rich_output, rprint, CliOutput


def request_with_handling(
Expand Down Expand Up @@ -118,7 +118,7 @@ def delete_repository(name: str, org: str = None):
)


def list_repositories(page: int, property: str, role: str):
def list_repositories(page: int, property: str, role: str, output_format: str):
headers = get_headers()
url = build_url("user", "repos")

Expand All @@ -132,12 +132,15 @@ def list_repositories(page: int, property: str, role: str):
error_msg={401: "Unauthorized access. Please check your token or credentials."},
)

if response:
data = response.json()
repo_full_name = [repo["full_name"] for repo in data]
for repos in repo_full_name:
rich_output(f"- {repos}")
rich_output(f"\nTotal repositories: {len(repo_full_name)}")
if not response:
return None

output = CliOutput(response.json())

if output_format == "json":
return output.json_format()

return output.default_format()


def dependabot_security(name: str, enabled: bool, org: str = None):
Expand Down
13 changes: 11 additions & 2 deletions src/github_rest_cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ def cli():
"-p",
"--page",
required=False,
default=50,
default=20,
type=int,
dest="page",
help="The number of results",
Expand All @@ -74,6 +74,14 @@ def cli():
dest="sort",
help="List repositories sorted by",
)
list_repo_parser.add_argument(
"-f",
"--format",
required=False,
default="table",
dest="format",
help="Format to display the list of repositories in",
)
list_repo_parser.set_defaults(func=list_repositories)

# Subparser for "create-repository" function
Expand Down Expand Up @@ -203,7 +211,8 @@ def cli():
if command == "get-repo":
args.func(args.name, args.org)
elif command == "list-repo":
args.func(args.page, args.sort, args.role)
repos = args.func(args.page, args.sort, args.role, args.format)
print(repos) # noqa: T201
elif command == "create-repo":
args.func(args.name, args.visibility, args.org, args.empty)
elif command == "delete-repo":
Expand Down
53 changes: 52 additions & 1 deletion src/github_rest_cli/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,56 @@
from rich import print as rprint
import json


class CliOutput:
# What's the role of `data` here?
# `data` is a parameter of the __init__ method, provided when creating an instance.
# When you create an instance of the CliOutput class,
# `self.data` stores the value of the `data` parameter as an instance attribute.
def __init__(self, data):
self.data = data

def json_format(self):
output = {
# "total_repositories": len([repo["full_name"] for repo in self.data]),
"repositories": [
{
"name": f.get("name"),
"owner": f.get("owner", {}).get("login"),
"url": f.get("html_url"),
"visibility": f.get("visibility"),
}
for f in self.data
],
}

return json.dumps(output, indent=2)

def table_format(self):
from prettytable import PrettyTable

table = PrettyTable()
table.title = "GitHub Repositories"
table.header_style = "upper"

if isinstance(self.data, list):
table.field_names = ["name", "owner", "url", "visibility"]

for repo in self.data:
table.add_row(
[
repo.get("name"),
repo.get("owner", {}).get("login"),
repo.get("html_url"),
repo.get("visibility"),
]
)

return table

def default_format(self):
return self.table_format()


def rich_output(message: str, format_str: str = "bold green"):
rprint(f"[{format_str}]{message}[/{format_str}]")
return rprint(f"[{format_str}]{message}[/{format_str}]")
23 changes: 23 additions & 0 deletions uv.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.