Skip to content

Commit cfbc0ba

Browse files
idodeclareVladimir Kotal
authored andcommitted
Resolve #2638 : Add -h,--help repos
1 parent c49b2ba commit cfbc0ba

File tree

3 files changed

+120
-2
lines changed

3 files changed

+120
-2
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2020, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opengrok.indexer.history;
25+
26+
import java.util.List;
27+
import java.util.Locale;
28+
29+
/**
30+
* Represents a utility to show the user details of supported {@link Repository}
31+
* types for -h,--help.
32+
*/
33+
public class RepositoriesHelp {
34+
35+
public static String getText() {
36+
StringBuilder builder = new StringBuilder();
37+
builder.append("Enabled repositories:");
38+
builder.append(System.lineSeparator());
39+
builder.append(System.lineSeparator());
40+
41+
List<Class<? extends Repository>> clazzes = RepositoryFactory.getRepositoryClasses();
42+
clazzes.sort((o1, o2) -> o1.getSimpleName().compareToIgnoreCase(o2.getSimpleName()));
43+
for (Class<?> clazz : clazzes) {
44+
String simpleName = clazz.getSimpleName();
45+
if (toAka(builder, simpleName)) {
46+
builder.append(" (");
47+
builder.append(simpleName);
48+
builder.append(")");
49+
}
50+
builder.append(System.lineSeparator());
51+
}
52+
return builder.toString();
53+
}
54+
55+
private static boolean toAka(StringBuilder builder, String repoSimpleName) {
56+
final String REPOSITORY = "Repository";
57+
if (!repoSimpleName.endsWith(REPOSITORY)) {
58+
builder.append(repoSimpleName);
59+
return false;
60+
} else {
61+
String aka = repoSimpleName.substring(0,
62+
repoSimpleName.length() - REPOSITORY.length());
63+
builder.append(aka.toLowerCase(Locale.ROOT));
64+
return true;
65+
}
66+
}
67+
68+
/* private to enforce static */
69+
private RepositoriesHelp() {
70+
}
71+
}

opengrok-indexer/src/main/java/org/opengrok/indexer/index/Indexer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.opengrok.indexer.configuration.Project;
6262
import org.opengrok.indexer.configuration.RuntimeEnvironment;
6363
import org.opengrok.indexer.history.HistoryGuru;
64+
import org.opengrok.indexer.history.RepositoriesHelp;
6465
import org.opengrok.indexer.history.Repository;
6566
import org.opengrok.indexer.history.RepositoryFactory;
6667
import org.opengrok.indexer.history.RepositoryInfo;
@@ -168,6 +169,9 @@ public static void main(String[] argv) {
168169
case GURU:
169170
helpStream.println(AnalyzerGuruHelp.getUsage());
170171
break;
172+
case REPOS:
173+
helpStream.println(RepositoriesHelp.getText());
174+
break;
171175
default:
172176
helpStream.println(helpUsage);
173177
break;
@@ -448,7 +452,8 @@ public static String[] parseOptions(String[] argv) throws ParseException {
448452
"With no mode specified, display this usage summary. Or specify a mode:",
449453
" config - display configuration.xml examples.",
450454
" ctags - display ctags command-line.",
451-
" guru - display AnalyzerGuru details.").Do(v -> {
455+
" guru - display AnalyzerGuru details.",
456+
" repos - display enabled repositories.").Do(v -> {
452457
help = true;
453458
helpUsage = parser.getUsage();
454459
String mode = (String) v;
@@ -1148,7 +1153,7 @@ private static String getCtagsCommand() {
11481153
}
11491154

11501155
private enum HelpMode {
1151-
CONFIG, CTAGS, DEFAULT, GURU
1156+
CONFIG, CTAGS, DEFAULT, GURU, REPOS
11521157
}
11531158

11541159
private Indexer() {
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2020, Chris Fraire <[email protected]>.
22+
*/
23+
24+
package org.opengrok.indexer.history;
25+
26+
import org.junit.Test;
27+
28+
import static org.junit.Assert.assertFalse;
29+
import static org.junit.Assert.assertTrue;
30+
31+
/**
32+
* Represents a container for tests of {@link RepositoriesHelp}.
33+
*/
34+
public class RepositoriesHelpTest {
35+
@Test
36+
public void shouldCreateReadableUsage() {
37+
String helpText = RepositoriesHelp.getText();
38+
assertFalse("help text should not be empty", helpText.isEmpty());
39+
assertTrue("help text should contain 'git (GitRepository)'",
40+
helpText.contains("git (GitRepository)"));
41+
}
42+
}

0 commit comments

Comments
 (0)