Bug 10003 - koha-* scripts (packages) should provide tab-completion in bash
Summary: koha-* scripts (packages) should provide tab-completion in bash
Status: CLOSED FIXED
Alias: None
Product: Koha
Classification: Unclassified
Component: Packaging (show other bugs)
Version: master
Hardware: All All
: P5 - low enhancement (vote)
Assignee: Tomás Cohen Arazi
QA Contact:
URL:
Keywords:
Depends on: 10094 10622
Blocks:
  Show dependency treegraph
 
Reported: 2013-04-08 18:04 UTC by Tomás Cohen Arazi
Modified: 2014-12-07 20:07 UTC (History)
6 users (show)

See Also:
Change sponsored?: Sponsored
Patch complexity: Small patch
Documentation contact:
Documentation submission:
Text to go in the release notes:
Version(s) released in:


Attachments
Bug 10003: bash-completion for koha-list and some other script (7.35 KB, patch)
2013-08-30 14:43 UTC, Tomás Cohen Arazi
Details | Diff | Splinter Review
Bug 10003: bash-completion for koha-list and some other script (7.54 KB, patch)
2013-10-30 15:40 UTC, Tomás Cohen Arazi
Details | Diff | Splinter Review
Bug 10003: (followup) use sudo to get the instances list (1.56 KB, patch)
2013-12-19 18:22 UTC, Tomás Cohen Arazi
Details | Diff | Splinter Review
Bug 10003: bash-completion for koha-list and some other script (7.60 KB, patch)
2014-03-12 13:47 UTC, Dobrica Pavlinusic
Details | Diff | Splinter Review
Bug 10003: (followup) use sudo to get the instances list (1.62 KB, patch)
2014-03-12 13:48 UTC, Dobrica Pavlinusic
Details | Diff | Splinter Review
Bug 10003: bash-completion for koha-list and some other script (7.66 KB, patch)
2014-03-20 10:50 UTC, Jonathan Druart
Details | Diff | Splinter Review
Bug 10003: (followup) use sudo to get the instances list (1.68 KB, patch)
2014-03-20 10:50 UTC, Jonathan Druart
Details | Diff | Splinter Review

Note You need to log in before you can comment on or make changes to this bug.
Description Tomás Cohen Arazi 2013-04-08 18:04:12 UTC
If implemented, there should be a section fro this in the coding guidelines, and every new command line switch should follow thsoe guidelines for inclusion in the bash-completion file.
Comment 1 Magnus Enger 2013-04-09 08:47:17 UTC
Not sure I understand - is this about tab completion on instance names or something else?
Comment 2 Tomás Cohen Arazi 2013-04-09 11:56:30 UTC
(In reply to comment #1)
> Not sure I understand - is this about tab completion on instance names or
> something else?

Its about tab completion of instances and all switches and parameters (providing a bash-completion script for the job).

For the job I'll need help on the possible combination of switches.
Comment 3 Tomás Cohen Arazi 2013-08-30 14:43:54 UTC Comment hidden (obsolete)
Comment 4 Tomás Cohen Arazi 2013-08-30 15:25:37 UTC
I have built a package for testing purposes. It's bug 10622 patches + this patch on top of master. It can be found here:

http://es.koha-community.org/kc_files/koha-common_3.13-1~git+20130830120021.1417289e_all.deb
Comment 5 I'm just a bot 2013-09-29 05:51:03 UTC
Patch applied cleanly, go forth and signoff
Comment 6 I'm just a bot 2013-10-24 19:38:26 UTC
Patch applied cleanly, go forth and signoff
Comment 7 Tomás Cohen Arazi 2013-10-30 15:40:08 UTC Comment hidden (obsolete)
Comment 8 Mark Tompsett 2013-11-01 19:01:58 UTC
After finding that filer vs. filter typo, I had hopes for it working.
However, this is some of my testing output:

mtompset@ubuntu:~$ koha-list
library
mtompset@ubuntu:~$ koha-list --enabled
grep: /etc/apache2/sites-available/library: Permission denied
library
mtompset@ubuntu:~$ koha-list --enabled
grep: /etc/apache2/sites-available/library: Permission denied
library
mtompset@ubuntu:~$ koha-list --disabled
grep: /etc/apache2/sites-available/library: Permission denied
mtompset@ubuntu:~$ koha-list --disabled
grep: /etc/apache2/sites-available/library: Permission denied
mtompset@ubuntu:~$ sudo koha-disable grep: /etc/apache2/sites-available/library: Permission denied
library grep: /etc/apache2/sites-available/library: Permission denied

 * Restarting web server apache2                                                                                                                             apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
 ... waiting apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1 for ServerName
                                                                                                                                                      [ OK ]
mtompset@ubuntu:~$ koha-list --disabled
grep: /etc/apache2/sites-available/library: Permission denied
mtompset@ubuntu:~$ koha-list --disabled
grep: /etc/apache2/sites-available/library: Permission denied
mtompset@ubuntu:~$ koha-list --enabled
grep: /etc/apache2/sites-available/library: Permission denied
library
mtompset@ubuntu:~$ koha-list --enabled
grep: /etc/apache2/sites-available/library: Permission denied
library
mtompset@ubuntu:~$ sudo koha-disable library
Instance library already disabled.
mtompset@ubuntu:~$ koha-list --enabled
grep: /etc/apache2/sites-available/library: Permission denied
library
mtompset@ubuntu:~$ koha-list --enabled
grep: /etc/apache2/sites-available/library: Permission denied
library
mtompset@ubuntu:~$ koha-list --disabled
grep: /etc/apache2/sites-available/library: Permission denied
mtompset@ubuntu:~$ koha-list --disabled
grep: /etc/apache2/sites-available/library: Permission denied
mtompset@ubuntu:~$
Comment 9 Mark Tompsett 2013-11-02 01:23:30 UTC
root@ubuntu:~# koha-enable --help
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             print version information and exit
      --help                display this help and exit
      --mmap                ignored for backwards compatibility

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print the file name for each match
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is `binary', `text', or `without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is `read', `recurse', or `skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is `read' or `skip'
  -R, -r, --recursive       equivalent to --directories=recurse
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
  -L, --files-without-match  print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is `always', `never', or `auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS)
  -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

`egrep' means `grep -E'.  `fgrep' means `grep -F'.
Direct invocation as either `egrep' or `fgrep' is deprecated.
With no FILE, or when FILE is -, read standard input.  If less than two FILEs
are given, assume -h.  Exit status is 0 if any line was selected, 1 otherwise;
if any error occurs and -q was not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU Grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>
Usage: grep [OPTION]... PATTERN [FILE]...
Search for PATTERN in each FILE or standard input.
PATTERN is, by default, a basic regular expression (BRE).
Example: grep -i 'hello world' menu.h main.c

Regexp selection and interpretation:
  -E, --extended-regexp     PATTERN is an extended regular expression (ERE)
  -F, --fixed-strings       PATTERN is a set of newline-separated fixed strings
  -G, --basic-regexp        PATTERN is a basic regular expression (BRE)
  -P, --perl-regexp         PATTERN is a Perl regular expression
  -e, --regexp=PATTERN      use PATTERN for matching
  -f, --file=FILE           obtain PATTERN from FILE
  -i, --ignore-case         ignore case distinctions
  -w, --word-regexp         force PATTERN to match only whole words
  -x, --line-regexp         force PATTERN to match only whole lines
  -z, --null-data           a data line ends in 0 byte, not newline

Miscellaneous:
  -s, --no-messages         suppress error messages
  -v, --invert-match        select non-matching lines
  -V, --version             print version information and exit
      --help                display this help and exit
      --mmap                ignored for backwards compatibility

Output control:
  -m, --max-count=NUM       stop after NUM matches
  -b, --byte-offset         print the byte offset with output lines
  -n, --line-number         print line number with output lines
      --line-buffered       flush output on every line
  -H, --with-filename       print the file name for each match
  -h, --no-filename         suppress the file name prefix on output
      --label=LABEL         use LABEL as the standard input file name prefix
  -o, --only-matching       show only the part of a line matching PATTERN
  -q, --quiet, --silent     suppress all normal output
      --binary-files=TYPE   assume that binary files are TYPE;
                            TYPE is `binary', `text', or `without-match'
  -a, --text                equivalent to --binary-files=text
  -I                        equivalent to --binary-files=without-match
  -d, --directories=ACTION  how to handle directories;
                            ACTION is `read', `recurse', or `skip'
  -D, --devices=ACTION      how to handle devices, FIFOs and sockets;
                            ACTION is `read' or `skip'
  -R, -r, --recursive       equivalent to --directories=recurse
      --include=FILE_PATTERN  search only files that match FILE_PATTERN
      --exclude=FILE_PATTERN  skip files and directories matching FILE_PATTERN
      --exclude-from=FILE   skip files matching any file pattern from FILE
      --exclude-dir=PATTERN  directories that match PATTERN will be skipped.
  -L, --files-without-match  print only names of FILEs containing no match
  -l, --files-with-matches  print only names of FILEs containing matches
  -c, --count               print only a count of matching lines per FILE
  -T, --initial-tab         make tabs line up (if needed)
  -Z, --null                print 0 byte after FILE name

Context control:
  -B, --before-context=NUM  print NUM lines of leading context
  -A, --after-context=NUM   print NUM lines of trailing context
  -C, --context=NUM         print NUM lines of output context
  -NUM                      same as --context=NUM
      --color[=WHEN],
      --colour[=WHEN]       use markers to highlight the matching strings;
                            WHEN is `always', `never', or `auto'
  -U, --binary              do not strip CR characters at EOL (MSDOS)
  -u, --unix-byte-offsets   report offsets as if CRs were not there (MSDOS)

`egrep' means `grep -E'.  `fgrep' means `grep -F'.
Direct invocation as either `egrep' or `fgrep' is deprecated.
With no FILE, or when FILE is -, read standard input.  If less than two FILEs
are given, assume -h.  Exit status is 0 if any line was selected, 1 otherwise;
if any error occurs and -q was not given, the exit status is 2.

Report bugs to: bug-grep@gnu.org
GNU Grep home page: <http://www.gnu.org/software/grep/>
General help using GNU software: <http://www.gnu.org/gethelp/>
grep: /etc/apache2/sites-available/--help: No such file or directory
Instance --help already enabled.

Not really a bug, but definitely a weird side-effect.
Comment 10 Mark Tompsett 2013-11-02 02:55:00 UTC
root@ubuntu:~# koha-stop-zebra instance2
Stopping Zebra server for instance2
root@ubuntu:~# koha-stop-zebra instance2
Zebra already stopped for instance instance2.
root@ubuntu:~# koha-stop-zebra instance2
Zebra already stopped for instance instance2.

The tab completion came up with instance2 even though it was stopped already.

Hmmm... my koha-restart-sip doesn't exist, I think that was bug 10622. Perhaps I built my .deb before including 10622.

Any other tests I had issues with were NOT tab completion issues, but rather behaviour issues (--help vs. no parameter, what does disable really mean, feedback vs. no feedback).
Comment 11 Tomás Cohen Arazi 2013-11-02 03:41:42 UTC
(In reply to M. Tompsett from comment #10)
> root@ubuntu:~# koha-stop-zebra instance2
> Stopping Zebra server for instance2
> root@ubuntu:~# koha-stop-zebra instance2
> Zebra already stopped for instance instance2.
> root@ubuntu:~# koha-stop-zebra instance2
> Zebra already stopped for instance instance2.
> 
> The tab completion came up with instance2 even though it was stopped already.
> 
> Hmmm... my koha-restart-sip doesn't exist, I think that was bug 10622.
> Perhaps I built my .deb before including 10622.
> 
> Any other tests I had issues with were NOT tab completion issues, but rather
> behaviour issues (--help vs. no parameter, what does disable really mean,
> feedback vs. no feedback).


All this is intended. tab completion just shows valid parameters for the command. You can call any of those koha-*-action commands with instances for such the task is not running and the result is that you got. Those scripts just check the instance is enabled. You shouldn't ask tab completion to provide things the commands themselves don't provide.

Please don't paste the output of the grep command help, it just makes this bug unreadable.
Comment 12 Mark Tompsett 2013-11-18 19:42:54 UTC
Have you fixed the issue with tab completion failing for non-root users?
Comment 13 Tomás Cohen Arazi 2013-12-19 18:22:02 UTC Comment hidden (obsolete)
Comment 14 Dobrica Pavlinusic 2014-03-12 13:47:36 UTC Comment hidden (obsolete)
Comment 15 Dobrica Pavlinusic 2014-03-12 13:48:47 UTC Comment hidden (obsolete)
Comment 16 Dobrica Pavlinusic 2014-03-12 13:49:26 UTC
Very useful and nice addition to our packages, IMHO :-)
Comment 17 Jonathan Druart 2014-03-20 10:50:15 UTC
Created attachment 26476 [details] [review]
Bug 10003: bash-completion for koha-list and some other script

This is an initial commit submitted for peer review. It implements bash-completion for the following commands:

 - koha-list
 - koha-enable
 - koha-disable
 - koha-email-enable
 - koha-email-disable
 - koha-enable-sip
 - koha-start-sip
 - koha-restart-sip
 - koha-stop-sip
 - koha-start-zebra
 - koha-stop-zebra
 - koha-restart-zebra

It is implemented in a way that it removes already used or mutually exclusive parameters (instance names, option switches).
I already have written completion for other (more complex) commands, But I belive a simpler patch is better to start with.

IMPORTANT: this patch relies on having the koha-list command available in the path. Also 10622 is needed for the SIP-related option switches.

To test:
 - Make sure you have bash-completion installed and enabled (IRC might help us if you encounter problems).
 - Apply the patch.
Option 1:
 - Pick the debian/koha-common.bash-completion file and do
   $ cp debian/koha-common.bash-completion /etc/bash_completion.d/koha-common
 - Open a new bash shell (I do it opening a new terminal on my Ubuntu box).
 - Type one of the listed commands...
  And repeatedly press <TAB>.
 - Enjoy, and signoff if you belive it is usable. Otherwise report back.
Option 2:
 - run:
   $ . debian/koha-common.bash-completion
 - Type one of the listed commands...
  And repeatedly press <TAB>.
 - Enjoy, and signoff if you belive it is usable. Otherwise report back.

Tests:
- Some koha-list option switches are mutually exclusive, -h should be available in any context
- koha-enable should only autocomplete disabled instances
- koha-disable should only autocomplete enabled instances
- koha-email-enable should only autocomplete email-disabled instances
- koha-email-disable should only autocomplete email-enabled instances
- koha-*-zebra scripts should only autocomplete enabled instances.
- koha-*-sip scripts should only autocomplete sip-enabled instances.

Regards
To+

Note: writing bash-completion routines is a bit hacky, I tried to make it the simplest way I could. Your comments are welcome.

Sponsored-by: Universidad Nacional de Cordoba
Signed-off-by: Dobrica Pavlinusic <dpavlin@rot13.org>

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Comment 18 Jonathan Druart 2014-03-20 10:50:31 UTC
Created attachment 26477 [details] [review]
Bug 10003: (followup) use sudo to get the instances list

Te only way of using the koha-* commands is using sudo. The only way
to get the list of enabled instances and other insight information of
the instances is through the use of sudo.
This followup makes the tab-completion bash script use sudo for calling
'koha-list' so it can read teh relevant data.

The drawback of this approach is that the user will be prompted for password
the first time he/she presses the tab key twice (triggering tab-completion).

I see no problem with that. And is the only option available, apart from the
setuid one. Which I don't like.

Regards
To+

Sponsored-by: Universidad Nacional de Cordoba
Signed-off-by: Dobrica Pavlinusic <dpavlin@rot13.org>

Signed-off-by: Jonathan Druart <jonathan.druart@biblibre.com>
Comment 19 Galen Charlton 2014-05-05 16:08:04 UTC
Pushed to master.  Thanks, Tomás.

However, I am not pushing the follow-up: I've never heard of any other package that prompts you for a sudo password just by hitting the tab key.  It's one thing to do 'sudo foo', then get prompted for a password -- that's what you expect.  But not for hitting the tab key.