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 4d42206
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 21 deletions.
19 changes: 15 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,17 @@ 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}/zsh/site
register-python-argcomplete --shell zsh ramalama > $(DESTDIR)${SHAREDIR}/zsh/site/_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

.PHONY:
install-program:
install ${SELINUXOPT} -d -m 755 $(DESTDIR)$(BINDIR)
Expand All @@ -40,12 +51,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
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
4 changes: 4 additions & 0 deletions rpm/python-ramalama.spec
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,17 @@ 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
%{_datadir}/zsh/site/_%{pypi_name}

%changelog
%autochangelog

0 comments on commit 4d42206

Please sign in to comment.