Skip to content

Commit

Permalink
Add CLI
Browse files Browse the repository at this point in the history
Related to rpm-software-management#6

Co-authored-by: Jakub Kadlcik <frostyx@email.cz>
Signed-off-by: Matej Focko <mfocko@redhat.com>
  • Loading branch information
mfocko and FrostyX committed Sep 24, 2024
1 parent cd6dbf0 commit 1c622f2
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
1 change: 1 addition & 0 deletions fedora-distro-aliases.spec
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ Summary: %{summary}
%files -n python3-fedora-distro-aliases -f %{pyproject_files}
%license LICENSES/GPL-2.0-or-later.txt
%doc README.md
%{_bindir}/resolve-fedora-aliases


%changelog
Expand Down
66 changes: 66 additions & 0 deletions fedora_distro_aliases/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import argparse
import sys

from fedora_distro_aliases import get_distro_aliases

GETTERS = {
"namever": lambda x: x.namever,
"branch": lambda x: x.branch,
"version": lambda x: x.version,
}


def run(output_type: str, aliases: list[str]):
getter = GETTERS[output_type]
distro_aliases = get_distro_aliases()

resolved_aliases: set[str] = set()
for alias in aliases:
resolved_aliases.update(getter(x) for x in distro_aliases[alias])

print(" ".join(resolved_aliases))


def setup_parser() -> argparse.ArgumentParser:
parser = argparse.ArgumentParser(
description="""Resolve the requested aliases.
Resolve each of the ALIASES and print them out.
This script prints out only unique aliases i.e., it is not
possible to obtain the same alias twice in the output.
"""
)

# Handle the output type
parser.add_argument(
"-o", "--output-type",
choices=["namever", "branch", "version"],
default="namever",
help="""Choose the output format of the script.
Available options are namever (e.g., ‹fedora-40›), branch (e.g., ‹f40›),
or version (e.g., ‹40›).
"""
)

# Handle the aliases themselves
parser.add_argument(
"aliases",
nargs="+"
)

return parser


def main(argv=sys.argv[1:]):
argv = sys.argv[1:]

parser = setup_parser()
args = parser.parse_args(argv)

run(args.output_type, args.aliases)


if __name__ == '__main__':
main()
6 changes: 6 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,10 @@
packages=find_packages(),
include_package_data=True,
zip_safe=False,
py_modules=["cli"],
entry_points={
"console_scripts": [
'resolve-fedora-aliases = fedora_distro_aliases.cli:main'
],
},
)

0 comments on commit 1c622f2

Please sign in to comment.