Skip to content

Commit 751aa89

Browse files
authored
Merge pull request #46 from aboutcode-org/federatedcode-cli
Add federatedcode CLI
2 parents 8e226fb + 1e37954 commit 751aa89

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

aboutcode/federatedcode/cli.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
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+
13+
from aboutcode.federatedcode.client import get_package_scan
14+
15+
16+
@click.command()
17+
@click.argument("purl")
18+
def handler(purl):
19+
"""
20+
Get package scan for PURL from FederatedCode git repository.
21+
22+
PURL: PURL to fetch scan result
23+
"""
24+
click.echo(get_package_scan(purl=purl))
25+
26+
27+
if __name__ == "__main__":
28+
handler()

aboutcode/federatedcode/client/__init__.py

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

1919
load_dotenv()
2020

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

2326

2427
class ScanNotAvailableError(Exception):
@@ -28,8 +31,8 @@ class ScanNotAvailableError(Exception):
2831
def get_package_scan(purl: Union[PackageURL, str]):
2932
"""Return the package scan result for a PURL from the FederatedCode Git repository."""
3033

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

3437
if isinstance(purl, str):
3538
purl = PackageURL.from_string(purl)
@@ -45,9 +48,9 @@ def get_package_scan(purl: Union[PackageURL, str]):
4548
version = purl.version
4649
file_name = "scancodeio.json"
4750

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

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

5255
try:
5356
response = requests.get(file_url)

pyproject-aboutcode.federatedcode.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,14 @@ dependencies = [
3838
"packageurl_python >= 0.15.6",
3939
"aboutcode.hashid>=0.1.0",
4040
"python-dotenv>=1.0.1",
41+
"click>=8.1.7",
42+
"requests>=2.32.3",
4143
]
4244

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

47+
[project.scripts]
48+
federatedcode = "aboutcode.federatedcode.cli:handler"
4549

4650
[tool.bumpversion]
4751
current_version = "0.1.0"

0 commit comments

Comments
 (0)