From 00d9694c690b29773522ac751354c778a487eb13 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Thu, 29 Feb 2024 15:10:18 +0100 Subject: [PATCH] Add CLI Related to #6 Co-authored-by: Jakub Kadlcik Signed-off-by: Matej Focko --- fedora-distro-aliases.spec | 1 + fedora_distro_aliases/cli.py | 66 ++++++++++++++++++++++++++++++++++++ setup.py | 6 ++++ 3 files changed, 73 insertions(+) create mode 100644 fedora_distro_aliases/cli.py diff --git a/fedora-distro-aliases.spec b/fedora-distro-aliases.spec index 5b2c12b..c262d04 100644 --- a/fedora-distro-aliases.spec +++ b/fedora-distro-aliases.spec @@ -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 diff --git a/fedora_distro_aliases/cli.py b/fedora_distro_aliases/cli.py new file mode 100644 index 0000000..f554a76 --- /dev/null +++ b/fedora_distro_aliases/cli.py @@ -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() diff --git a/setup.py b/setup.py index 72e53fc..c7b94cf 100644 --- a/setup.py +++ b/setup.py @@ -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' + ], + }, )