From e667a83369b018b741f38068db7989af75ea296d Mon Sep 17 00:00:00 2001 From: GenZmeY Date: Thu, 13 Aug 2020 05:44:02 +0300 Subject: [PATCH] feat: bash completion --- SOURCES/Makefile | 6 ++ SOURCES/main/bash_completion/kf2-srv | 98 ++++++++++++++++++++++++++++ SOURCES/main/kf2-srv | 11 ++++ SPECS/kf2-srv.spec | 9 ++- 4 files changed, 122 insertions(+), 2 deletions(-) create mode 100644 SOURCES/main/bash_completion/kf2-srv diff --git a/SOURCES/Makefile b/SOURCES/Makefile index 5dbad2a..897b1d8 100644 --- a/SOURCES/Makefile +++ b/SOURCES/Makefile @@ -27,6 +27,7 @@ PREFIX = /usr/local MAINLOGDIR = $(DESTDIR)/var/log/$(NAME) BETALOGDIR = $(DESTDIR)/var/log/$(NAME)-beta CONFDIR = $(DESTDIR)/etc/$(NAME) +BASHCOMPDIR = $(DESTDIR)/etc/bash_completion.d INSTMAINDIR = $(CONFDIR)/instances INSTBETADIR = $(CONFDIR)/instances-beta MAPCYCLEDIR = $(CONFDIR)/mapcycles @@ -88,6 +89,7 @@ filesystem: test -d '$(RSYSLOGDIR)' || install -m 755 -d '$(RSYSLOGDIR)' test -d '$(SCRIPTGRPDIR)' || install -m 755 -d '$(SCRIPTGRPDIR)' test -d '$(SCRIPTLIBDIR)' || install -m 755 -d '$(SCRIPTLIBDIR)' + test -d '$(BASHCOMPDIR)' || install -m 755 -d '$(BASHCOMPDIR)' install: filesystem build install -m 755 $(RELEASEDIR)/main/$(NAME) $(BINDIR) @@ -128,6 +130,8 @@ install: filesystem build install -m 644 $(RELEASEDIR)/config/$(NAME).conf $(CONFDIR) install -m 644 $(SOURCEDIR)/COPYING $(LICENSEDIR) + + install -m 644 $(RELEASEDIR)/main/bash_completion/$(NAME) $(BASHCOMPDIR) uninstall: rm -f $(BINDIR)/$(NAME) @@ -201,6 +205,8 @@ test: fake-systemd-build $(BASHCHECK) $(RELEASEDIR)/main/lib/playerids.lib $(BASHCHECK) $(RELEASEDIR)/main/lib/webadmin.lib $(BASHCHECK) $(RELEASEDIR)/main/lib/workshop.lib + + $(BASHCHECK) $(RELEASEDIR)/main/bash_completion/$(NAME) clean: rm -rf $(RELEASEDIR) diff --git a/SOURCES/main/bash_completion/kf2-srv b/SOURCES/main/bash_completion/kf2-srv new file mode 100644 index 0000000..15f88bd --- /dev/null +++ b/SOURCES/main/bash_completion/kf2-srv @@ -0,0 +1,98 @@ +#!/bin/bash + +# kf2-srv is a command line tool for managing a set of Killing Floor 2 servers. +# Copyright (C) 2019, 2020 GenZmeY +# mailto: genzmey@gmail.com +# +# This file is part of kf2-srv. +# +# kf2-srv is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +function _kf2_srv_completions () # $1: BetaPostfix +{ + local GrpDir=":DEFINE_PREFIX:/share/kf2-srv/cmdgrp" + local InsDir="/etc/kf2-srv/instances${BetaPostfix}" + local KF2Srv=":DEFINE_PREFIX:/bin/kf2-srv${BetaPostfix}" + + function groups_list () + { + find "$GrpDir" \ + -mindepth 1 \ + -maxdepth 1 \ + -type d \ + -printf "%f\n" + } + + function commands_list () # $1: Command group + { + find "$GrpDir/$1" \ + -mindepth 1 \ + -maxdepth 1 \ + -type f \ + -printf "%f\n" + } + + function instances_list () + { + find "$InsDir" \ + -mindepth 1 \ + -maxdepth 1 \ + -type d \ + -printf "%f\n" + } + + function command_usage_processing () # $1: Command group, $2: Command, $3: Current argument position + { + local CmdParams=$("$KF2Srv" "$1" "$2" "usage") + local LocalPosition; ((LocalPosition = $3 - 2)) + + local ParamIndex=0 + local LastParam + for Param in $CmdParams + do + ((ParamIndex++)) + if [[ "$ParamIndex" -eq "$LocalPosition" ]]; then + if echo "$Param" | grep -Fq ''; then + COMPREPLY=($(compgen -W "$(instances_list)" -- "${CURARG}")) + fi + return 0 + fi + LastParam="$Param" + done + + if echo "$LastParam" | grep -Fq '...'; then + COMPREPLY=($(compgen -W "$(instances_list)" -- "${CURARG}")) + fi + } + + COMPREPLY=() + CURARG="${COMP_WORDS[COMP_CWORD]}" + + case "${COMP_CWORD}" in + 1 ) COMPREPLY=($(compgen -W "$(groups_list)" -- "${CURARG}")) ;; + 2 ) COMPREPLY=($(compgen -W "$(commands_list ${COMP_WORDS[1]})" -- "${CURARG}")) ;; + * ) command_usage_processing "${COMP_WORDS[1]}" "${COMP_WORDS[2]}" "${COMP_CWORD}";; + esac + + return 0 +} + +function _kf2_srv_beta_completions () +{ + _kf2_srv_completions "-beta" +} + +complete -F _kf2_srv_completions kf2-srv +complete -F _kf2_srv_beta_completions kf2-srv-beta + diff --git a/SOURCES/main/kf2-srv b/SOURCES/main/kf2-srv index 3783c15..019c104 100755 --- a/SOURCES/main/kf2-srv +++ b/SOURCES/main/kf2-srv @@ -90,6 +90,11 @@ function is_help () # $1: Arg echo "$1" | grep -Piqo '^(-h|--help|help)$' } +function is_usage () # $1: Arg +{ + echo "$1" | grep -Piqo '^usage$' +} + function is_version () # $1: Arg { echo "$1" | grep -Piqo '^(-v|--version|version)$' @@ -197,6 +202,12 @@ elif [[ -d "$GroupPathname" ]]; then else echo "No help page for this command." fi + elif is_usage "$1"; then + if function_exists "cmd_usage"; then + cmd_usage + else + echo "No usage information for this command." + fi else if function_exists "cmd_main"; then if function_exists "cmd_need_installed_server" && cmd_need_installed_server && ! server_exists; then diff --git a/SPECS/kf2-srv.spec b/SPECS/kf2-srv.spec index a00e5df..f46d91f 100644 --- a/SPECS/kf2-srv.spec +++ b/SPECS/kf2-srv.spec @@ -1,8 +1,8 @@ %global steamuser steam Name: kf2-srv -Version: 0.14.1 -Release: 2%{dist} +Version: 0.15.0 +Release: 1%{dist} Summary: Killing Floor 2 server Group: Amusements/Games License: GNU GPLv3 @@ -31,6 +31,7 @@ Requires: multini >= 0.2.3 Requires: rsyslog >= 8.25.0 Requires: logrotate Requires: inotify-tools +Requires: bash-completion >= 2.7 Provides: %{name} @@ -80,6 +81,7 @@ rm -rf $RPM_BUILD_ROOT %attr(0644,root,root) %{_sysconfdir}/logrotate.d/%{name} %attr(0644,root,root) %{_datadir}/%{name}/cmdgrp/*/* %attr(0644,root,root) %{_datadir}/%{name}/lib/* +%attr(0644,root,root) %{_sysconfdir}/bash_completion.d/%{name} %preun if [[ $1 -eq 0 ]] ; then # Uninstall @@ -99,6 +101,9 @@ if [[ $1 == 1 ]]; then # Install fi %changelog +* Thu Aug 13 2020 GenZmeY - 0.15.0-1 +- bash completion support. + * Mon Aug 10 2020 GenZmeY - 0.14.1-2 - github-actions build.