From 6add76b842fec92dbf85354a313b67d941c98ee4 Mon Sep 17 00:00:00 2001 From: Daniel J Walsh Date: Sat, 28 Sep 2024 08:06:50 -0400 Subject: [PATCH] Add command completion to Ramalama Signed-off-by: Daniel J Walsh --- .github/workflows/ci.yml | 4 ++-- Makefile | 20 +++++++++++++++---- .../ramalama/latest/Containerfile | 2 +- ramalama.py | 18 ++++++++++++++++- ramalama/__init__.py | 4 ++-- ramalama/cli.py | 17 +++------------- rpm/python-ramalama.spec | 7 +++++++ 7 files changed, 48 insertions(+), 24 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 039f8ba7..55ae3307 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -9,8 +9,8 @@ jobs: shell: bash run: | sudo apt-get update - sudo apt-get install bats bash - pip install omlmd + sudo apt-get install bats bash python3-argcomplete + pip install omlmd argcomplete - name: run test run: make test diff --git a/Makefile b/Makefile index 63ba20a8..490417fa 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ OS := $(shell uname;) SELINUXOPT ?= $(shell test -x /usr/sbin/selinuxenabled && selinuxenabled && echo -Z) PREFIX ?= /usr/local BINDIR ?= ${PREFIX}/bin -SHAREDIR ?= ${PREFIX}/share/ramalama +SHAREDIR ?= ${PREFIX}/share PYTHON ?= $(shell command -v python3 python|head -n1) DESTDIR ?= / PATH := $(PATH):$(HOME)/.local/bin @@ -32,6 +32,18 @@ help: @echo " - make clean" @echo +.PHONY: +install-completions: + install ${SELINUXOPT} -d -m 755 $(DESTDIR)${SHAREDIR}/bash-completion/completions + register-python-argcomplete --shell bash ramalamay > $(DESTDIR)${SHAREDIR}/bash-completion/completions/ramalama + + install ${SELINUXOPT} -d -m 755 $(DESTDIR)${SHAREDIR}/fish/vendor_completions.d + register-python-argcomplete --shell fish ramalama > $(DESTDIR)${SHAREDIR}/fish/vendor_completions.d/ramalama.fish + +# FIXME: not available on Centos 9 yet. +# install ${SELINUXOPT} -d -m 755 $(DESTDIR)${SHAREDIR}/zsh/site +# register-python-argcomplete --shell zsh ramalama > $(DESTDIR)${SHAREDIR}/zsh/site/_ramalama + .PHONY: install-program: install ${SELINUXOPT} -d -m 755 $(DESTDIR)$(BINDIR) @@ -40,12 +52,12 @@ install-program: .PHONY: install-shortnames: - install ${SELINUXOPT} -d -m 755 $(DESTDIR)$(SHAREDIR) + install ${SELINUXOPT} -d -m 755 $(DESTDIR)$(SHAREDIR)/ramalama install ${SELINUXOPT} -m 644 shortnames/shortnames.conf \ - $(DESTDIR)$(SHAREDIR) + $(DESTDIR)$(SHAREDIR)/ramalama .PHONY: -install: install-program install-shortnames install-docs +install: install-program install-shortnames install-docs install-completions RAMALAMA_VERSION=$(RAMALAMA_VERSION) \ pip install . --root $(DESTDIR) --prefix ${PREFIX} diff --git a/container-images/ramalama/latest/Containerfile b/container-images/ramalama/latest/Containerfile index eec953fa..bf5d3158 100644 --- a/container-images/ramalama/latest/Containerfile +++ b/container-images/ramalama/latest/Containerfile @@ -15,7 +15,7 @@ RUN dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.n dnf install -y epel-release && \ dnf --enablerepo=ubi-9-appstream-rpms install -y git procps-ng vim \ dnf-plugins-core python3-dnf-plugin-versionlock cmake gcc-c++ \ - python3-pip && \ + python3-pip python3-argcomplete && \ dnf clean all && \ rm -rf /var/cache/*dnf* diff --git a/ramalama.py b/ramalama.py index c07710a7..ce9b0f5f 100755 --- a/ramalama.py +++ b/ramalama.py @@ -1,5 +1,6 @@ #!/usr/bin/python3 +import argcomplete import os import errno import subprocess @@ -13,8 +14,23 @@ def main(args): import ramalama + parser, args = ramalama.init_cli() + argcomplete.autocomplete(parser) + + if args.version: + return ramalama.version(args) + + if ramalama.run_container(args): + return + + # Process CLI try: - ramalama.init_cli() + args.func(args) + except ramalama.HelpException: + parser.print_help() + except AttributeError: + parser.print_usage() + print("ramalama: requires a subcommand") except IndexError as e: ramalama.perror("Error: " + str(e).strip("'")) sys.exit(errno.EINVAL) diff --git a/ramalama/__init__.py b/ramalama/__init__.py index 82acb121..bd98974d 100644 --- a/ramalama/__init__.py +++ b/ramalama/__init__.py @@ -1,9 +1,9 @@ """ramalama client module.""" from ramalama.common import perror -from ramalama.cli import init_cli +from ramalama.cli import init_cli, run_container, version, HelpException import sys assert sys.version_info >= (3, 6), "Python 3.6 or greater is required." -__all__ = ["perror", "init_cli"] +__all__ = ["perror", "init_cli", "run_container", "version", "HelpException"] diff --git a/ramalama/cli.py b/ramalama/cli.py index c2b38fc6..f574d238 100644 --- a/ramalama/cli.py +++ b/ramalama/cli.py @@ -30,7 +30,6 @@ def use_container(): def init_cli(): - shortnames = Shortnames() parser = ArgumentParser( prog="ramalama", formatter_class=argparse.ArgumentDefaultsHelpFormatter, @@ -72,27 +71,17 @@ def init_cli(): version_parser(subparsers) # Parse CLI args = parser.parse_args() - if args.version: - return version(args) + # create stores directories mkdirs(args.store) if hasattr(args, "MODEL"): + shortnames = Shortnames() resolved_model = shortnames.resolve(args.MODEL) if resolved_model: args.UNRESOLVED_MODEL = args.MODEL args.MODEL = resolved_model - if run_container(args): - return - - # Process CLI - try: - args.func(args) - except HelpException: - parser.print_help() - except AttributeError: - parser.print_usage() - print("ramalama: requires a subcommand") + return parser, args def login_parser(subparsers): diff --git a/rpm/python-ramalama.spec b/rpm/python-ramalama.spec index c3ab87cc..abd27b56 100644 --- a/rpm/python-ramalama.spec +++ b/rpm/python-ramalama.spec @@ -39,6 +39,7 @@ BuildRequires: pyproject-rpm-macros BuildRequires: python%{python3_pkgversion}-pip BuildRequires: python%{python3_pkgversion}-setuptools BuildRequires: python%{python3_pkgversion}-wheel +BuildRequires: python%{python3_pkgversion}-argcomplete Summary: %{summary} Provides: %{pypi_name} = %{version}-%{release} %{?python_provide:%python_provide python%{python3_pkgversion}-%{pypi_name}} @@ -57,6 +58,7 @@ Provides: %{pypi_name} = %{version}-%{release} %pyproject_save_files %{pypi_name} %{__make} DESTDIR=%{buildroot} PREFIX=%{_prefix} install-shortnames %{__make} DESTDIR=%{buildroot} PREFIX=%{_prefix} install-docs +%{__make} DESTDIR=%{buildroot} PREFIX=%{_prefix} install-completions %files -n python%{python3_pkgversion}-%{pypi_name} -f %{pyproject_files} %license LICENSE @@ -64,6 +66,11 @@ Provides: %{pypi_name} = %{version}-%{release} %dir %{_datadir}/%{pypi_name} %{_datadir}/%{pypi_name}/shortnames.conf %{_mandir}/man1/ramalama*.1* +%{_datadir}/bash-completion/completions/%{pypi_name} +%{_datadir}/fish/vendor_completions.d//%{pypi_name}.fish +# +# FIXME: Not available on Centos/RHEL 9 yet +# %{_datadir}/zsh/site/_%{pypi_name} %changelog %autochangelog