Skip to content

Commit 2861e42

Browse files
committed
Add federatedcode cli
Signed-off-by: Keshav Priyadarshi <git@keshav.space>
1 parent 8e226fb commit 2861e42

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

aboutcode/federatedcode/cli.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/usr/bin/env python3
2+
#
3+
# Copyright (c) nexB Inc. and others. All rights reserved.
4+
# FederatedCode is a trademark of nexB Inc.
5+
# SPDX-License-Identifier: Apache-2.0
6+
# See http://www.apache.org/licenses/LICENSE-2.0 for the license text.
7+
# See https://github.com/nexB/federatedcode for support or download.
8+
# See https://aboutcode.org for more information about AboutCode.org OSS projects.
9+
#
10+
11+
import click
12+
from aboutcode.federatedcode.client import get_package_scan
13+
14+
@click.command()
15+
@click.argument('purl')
16+
def handler(purl):
17+
"""
18+
Get package scan for PURL from FederatedCode git repository.
19+
20+
PURL: PURL to fetch scan result
21+
"""
22+
click.echo(get_package_scan(purl=purl))
23+
24+
if __name__ == '__main__':
25+
handler()

aboutcode/federatedcode/client/__init__.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
load_dotenv()
2020

21-
FEDERATEDCODE_GITHUB_ACCOUNT_NAME = os.getenv("FEDERATEDCODE_GITHUB_ACCOUNT_NAME")
21+
FEDERATEDCODE_GIT_RAW_URL = os.getenv("FEDERATEDCODE_GIT_RAW_URL", "https://raw.githubusercontent.com/aboutcode-org/")
2222

2323

2424
class ScanNotAvailableError(Exception):
@@ -28,8 +28,8 @@ class ScanNotAvailableError(Exception):
2828
def get_package_scan(purl: Union[PackageURL, str]):
2929
"""Return the package scan result for a PURL from the FederatedCode Git repository."""
3030

31-
if not FEDERATEDCODE_GITHUB_ACCOUNT_NAME:
32-
raise ValueError("Provide ``FEDERATEDCODE_GITHUB_ACCOUNT_NAME`` in .env file.")
31+
if not FEDERATEDCODE_GIT_RAW_URL:
32+
raise ValueError("Provide ``FEDERATEDCODE_GIT_RAW_URL`` in .env file.")
3333

3434
if isinstance(purl, str):
3535
purl = PackageURL.from_string(purl)
@@ -45,9 +45,9 @@ def get_package_scan(purl: Union[PackageURL, str]):
4545
version = purl.version
4646
file_name = "scancodeio.json"
4747

48-
url_parts = [FEDERATEDCODE_GITHUB_ACCOUNT_NAME, repo_name, package_dir_path, version, file_name]
48+
url_parts = [repo_name, package_dir_path, version, file_name]
4949

50-
file_url = urljoin("https://raw.githubusercontent.com", "/".join(url_parts))
50+
file_url = urljoin(FEDERATEDCODE_GIT_RAW_URL, "/".join(url_parts))
5151

5252
try:
5353
response = requests.get(file_url)

pyproject-aboutcode.federatedcode.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ dependencies = [
4242

4343
urls = { Homepage = "https://github.com/aboutcode-org/federatedcode" }
4444

45+
[project.scripts]
46+
federatedcode = "aboutcode.federatedcode.cli:handler"
4547

4648
[tool.bumpversion]
4749
current_version = "0.1.0"

0 commit comments

Comments
 (0)