Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

POC - Make search the default command #289

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 51 additions & 49 deletions src/main/java/it/mulders/mcs/cli/Cli.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,20 @@
import it.mulders.mcs.search.SearchQuery;
import it.mulders.mcs.search.printer.CoordinatePrinter;
import picocli.CommandLine;
import picocli.CommandLine.Model.CommandSpec;
import picocli.CommandLine.Spec;

import java.util.Objects;
import java.util.concurrent.Callable;

@CommandLine.Command(
name = "mcs",
subcommands = { Cli.SearchCommand.class, Cli.ClassSearchCommand.class },
description = "Search artifacts in Maven Central by coordinates",
subcommands = { Cli.ClassSearchCommand.class },
usageHelpAutoWidth = true,
versionProvider = ClasspathVersionProvider.class
)
public class Cli {
public class Cli implements Callable<Integer> {

@CommandLine.Option(
names = { "-V", "--version" },
Expand All @@ -31,68 +35,66 @@ public class Cli {
)
private boolean usageHelpRequested;

public SearchCommand createSearchCommand() {
return new SearchCommand();
}

public ClassSearchCommand createClassSearchCommand() {
return new ClassSearchCommand();
}

@CommandLine.Command(
name = "search",
description = "Search artifacts in Maven Central by coordinates",
usageHelpAutoWidth = true
@CommandLine.Parameters(
arity = "0..n",
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Needed to change from "1..n" to "0..n" to get ClassSearch subcommand to work.

description = {
"What to search for.",
"If the search term contains a colon ( : ), it is considered a literal groupId and artifactId",
"Otherwise, the search term is considered a wildcard search"
}
)
public class SearchCommand implements Callable<Integer> {
@CommandLine.Parameters(
arity = "1..n",
description = {
"What to search for.",
"If the search term contains a colon ( : ), it is considered a literal groupId and artifactId",
"Otherwise, the search term is considered a wildcard search"
}
)
private String[] query;
private String[] query;

@CommandLine.Option(
names = { "-l", "--limit" },
description = "Show <count> results",
paramLabel = "<count>"
)
private Integer limit;
@CommandLine.Option(
names = { "-l", "--limit" },
description = "Show <count> results",
paramLabel = "<count>"
)
private Integer limit;

@CommandLine.Option(
names = { "-f", "--format" },
description = """
@CommandLine.Option(
names = { "-f", "--format" },
description = """
Show result in <type> format
Supported types are:
maven, gradle, gradle-short, gradle-kotlin, sbt, ivy, grape, leiningen, buildr
""",
paramLabel = "<type>"
)
private String responseFormat;
paramLabel = "<type>"
)
private String responseFormat;

@CommandLine.Option(
names = { "-s", "--show-vulnerabilities" },
description = "Show reported security vulnerabilities",
paramLabel = "<vulnerabilities>"
)
private boolean showVulnerabilities;
@CommandLine.Option(
names = { "-s", "--show-vulnerabilities" },
description = "Show reported security vulnerabilities",
paramLabel = "<vulnerabilities>"
)
private boolean showVulnerabilities;

@Override
public Integer call() {
var combinedQuery = String.join(" ", query);
System.out.printf("Searching for %s...%n", combinedQuery);
var searchQuery = SearchQuery.search(combinedQuery)
.withLimit(this.limit)
.build();

CoordinatePrinter coordinatePrinter = FormatType.providePrinter(responseFormat);
var searchCommandHandler = new SearchCommandHandler(coordinatePrinter, showVulnerabilities);
searchCommandHandler.search(searchQuery);
return 0;
@Spec
CommandSpec spec;

@Override
public Integer call() {
if (Objects.isNull(query)) {
spec.commandLine().usage(System.err);
System.exit(1);
}
Comment on lines +83 to 86
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added because of this change. This explicit validation means the commands mcs and previously mcs search (missing the query) both do the same thing e.g. print usage and exit.


var combinedQuery = String.join(" ", query);
System.out.printf("Searching for %s...%n", combinedQuery);
var searchQuery = SearchQuery.search(combinedQuery)
.withLimit(this.limit)
.build();

CoordinatePrinter coordinatePrinter = FormatType.providePrinter(responseFormat);
var searchCommandHandler = new SearchCommandHandler(coordinatePrinter, showVulnerabilities);
searchCommandHandler.search(searchQuery);
return 0;
}

@CommandLine.Command(
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/it/mulders/mcs/cli/CommandClassFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ public CommandClassFactory(final Cli cli) {
@Override
@SuppressWarnings("unchecked")
public <K> K create(Class<K> cls) throws Exception {
if (cls == Cli.SearchCommand.class) {
return (K) cli.createSearchCommand();
} else if (cls == Cli.ClassSearchCommand.class) {
if (cls == Cli.ClassSearchCommand.class) {
return (K) cli.createClassSearchCommand();
}

Expand Down
10 changes: 5 additions & 5 deletions src/test/java/it/mulders/mcs/cli/CliTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,35 +26,35 @@ class SearchCommandTest {
void delegates_to_handler() {
var query = SearchQuery.search("test").build();

verifySearchExecution(query, "search", "test");
verifySearchExecution(query, "test");
}

@Test
void accepts_space_separated_terms() {
SearchQuery query = SearchQuery.search("jakarta rs").build();

verifySearchExecution(query, "search", "jakarta", "rs");
verifySearchExecution(query, "jakarta", "rs");
}

@Test
void accepts_limit_results_parameter() {
var query = SearchQuery.search("test").withLimit(3).build();

verifySearchExecution(query, "search", "--limit", "3", "test");
verifySearchExecution(query, "--limit", "3", "test");
}

@Test
void accepts_output_type_parameter() {
var query = SearchQuery.search("test").build();

verifySearchExecution(query, "search", "--format", "gradle-short", "test");
verifySearchExecution(query, "--format", "gradle-short", "test");
}

@Test
void accepts_show_vulnerabilities_parameter() {
var query = SearchQuery.search("test").build();

verifySearchExecution(query, "search", "--show-vulnerabilities", "test");
verifySearchExecution(query, "--show-vulnerabilities", "test");
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/test/java/it/mulders/mcs/cli/CommandClassFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ class CommandClassFactoryTest implements WithAssertions {
private final CommandClassFactory factory = new CommandClassFactory(cli);

@Test
void can_construct_search_command_instance() throws Exception {
assertThat(factory.create(Cli.SearchCommand.class)).isNotNull();
void can_construct_class_search_command_instance() throws Exception {
assertThat(factory.create(Cli.ClassSearchCommand.class)).isNotNull();
}

@Test
Expand Down
Loading