Skip to content

Commit

Permalink
dcache-core: fix admin command completion
Browse files Browse the repository at this point in the history
Motivation:
In the admin interface, command autocompletion strips arguments and parameters before showing them in the overview.
When one such argument contains a hyphen, everything after the hyphen is stripped and not shown.

Especially zookeeper-related ha-commands such as
`\s PnfsManager chimera-maintenance ha show participants`
were shortened, not auto-completed and thus only usable if they were already known.

Modification:
Make sure that we only shorten the string if the hyphen follows a whitespace.
Add tests for completion shortening.

Result:
Admin commands with hyphen-containing arguments are no longer trunkated prematurely, they are now properly shown and autocompleted.

Target: master, 9.2, 9.1
Requires-notes: no
Requires-book: no
Patch: https://rb.dcache.org/r/14176/
Acked-by: Albert Rossi
  • Loading branch information
lemora committed Dec 5, 2023
1 parent ffc5eff commit bb19a48
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import jline.console.completer.Completer;

/**
* Simple completor for JLine that uses the dCache help output to suggest command completions.
* Simple completer for JLine that uses the dCache help output to suggest command completions.
*/
public class HelpCompleter implements Completer {

Expand All @@ -21,8 +21,10 @@ public HelpCompleter(String help) {
}

protected String scan(String line) {
int i = CharMatcher.anyOf("#[]<>|-").indexIn(line);
return (i == -1) ? line : line.substring(0, i);
int i = CharMatcher.anyOf("#[]<>|").indexIn(line);
int j = line.indexOf(" -");
i = i == -1 || j == -1 ? Math.max(i, j) : Math.min(i, j);
return (i == -1) ? line : line.substring(0, i).trim();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* dCache - http://www.dcache.org/
*
* Copyright (C) 2023 Deutsches Elektronen-Synchrotron
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package diskCacheV111.admin;

import static org.junit.Assert.assertEquals;

import org.junit.Test;

public class HelpCompleterTest {

HelpCompleter helpCompleter = new HelpCompleter("");

@Test
public void testScanShouldStripArgs() {
String line = "log set <appender> [<logger>] OFF|ERROR|WARN|INFO|DEBUG|TRACE|ALL";
String processed = helpCompleter.scan(line);
assertEquals("log set", processed);
}

@Test
public void testScanShouldStripWhitespaceSeparatedHyphen() {
String line = "set max diskspace -";
String processed = helpCompleter.scan(line);
assertEquals("set max diskspace", processed);
}

@Test
public void testScanShouldAcceptHyphenContainingCommand() {
String line = "chimera-maintenance ha show participants";
String processed = helpCompleter.scan(line);
assertEquals("chimera-maintenance ha show participants", processed);
}
}

0 comments on commit bb19a48

Please sign in to comment.