Skip to content

Commit af15d20

Browse files
committed
jooby-cli: make the create command the default command #3537
1 parent 1431ce2 commit af15d20

File tree

3 files changed

+37
-2
lines changed

3 files changed

+37
-2
lines changed

modules/jooby-cli/src/main/java/io/jooby/cli/Cli.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.util.List;
1010
import java.util.Objects;
1111
import java.util.stream.Collectors;
12+
import java.util.stream.Stream;
1213

1314
import org.jline.reader.EndOfFileException;
1415
import org.jline.reader.LineReader;
@@ -124,7 +125,7 @@ public static void main(String[] args) throws IOException {
124125
try {
125126
String line = reader.readLine(prompt);
126127
ParsedLine pl = reader.getParser().parse(line, 0);
127-
String[] arguments = pl.words().toArray(new String[0]);
128+
String[] arguments = rewrite(cmd, pl.words().toArray(new String[0]));
128129
cmd.execute(arguments);
129130
} catch (UserInterruptException e) {
130131
System.exit(0);
@@ -134,4 +135,13 @@ public static void main(String[] args) throws IOException {
134135
}
135136
}
136137
}
138+
139+
static String[] rewrite(CommandLine cmd, String... command) {
140+
if (command.length > 1) {
141+
if (!cmd.getSubcommands().containsKey(command[0])) {
142+
return Stream.concat(Stream.of("create"), Stream.of(command)).toArray(String[]::new);
143+
}
144+
}
145+
return command;
146+
}
137147
}

modules/jooby-cli/src/main/java/io/jooby/cli/CreateCmd.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@
4444
*
4545
* @since 2.0.6
4646
*/
47-
@CommandLine.Command(name = "create", description = "Creates a new application")
47+
@CommandLine.Command(
48+
name = "create",
49+
description = "Creates a new application. This is the default command/action")
4850
public class CreateCmd extends Cmd {
4951
@CommandLine.Parameters(
5052
description = "Application name or coordinates (groupId:artifactId:version)")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Jooby https://jooby.io
3+
* Apache License Version 2.0 https://jooby.io/LICENSE.txt
4+
* Copyright 2014 Edgar Espina
5+
*/
6+
package io.jooby.cli;
7+
8+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
9+
10+
import org.junit.jupiter.api.Test;
11+
import org.mockito.Mockito;
12+
13+
import picocli.CommandLine;
14+
15+
public class CliTest {
16+
17+
@Test
18+
public void rewriteCommand() {
19+
var cmdLine = Mockito.mock(CommandLine.class);
20+
var command = Cli.rewrite(cmdLine, "jooby-code-gen", "--mvc");
21+
assertArrayEquals(new String[] {"create", "jooby-code-gen", "--mvc"}, command);
22+
}
23+
}

0 commit comments

Comments
 (0)