Skip to content

Commit

Permalink
Add command completion to Ramalama
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel J Walsh <dwalsh@redhat.com>
  • Loading branch information
rhatdan committed Sep 29, 2024
1 parent bfa109f commit 6add76b
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 24 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
20 changes: 16 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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}

Expand Down
2 changes: 1 addition & 1 deletion container-images/ramalama/latest/Containerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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*

Expand Down
18 changes: 17 additions & 1 deletion ramalama.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/python3

import argcomplete
import os
import errno
import subprocess
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions ramalama/__init__.py
Original file line number Diff line number Diff line change
@@ -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"]
17 changes: 3 additions & 14 deletions ramalama/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ def use_container():


def init_cli():
shortnames = Shortnames()
parser = ArgumentParser(
prog="ramalama",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions rpm/python-ramalama.spec
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
Expand All @@ -57,13 +58,19 @@ 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
%doc README.md
%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

0 comments on commit 6add76b

Please sign in to comment.