Skip to content

Commit e8561eb

Browse files
committed
Added Commandsystem
1 parent f52160b commit e8561eb

File tree

6 files changed

+84
-9
lines changed

6 files changed

+84
-9
lines changed

pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@
4747
<artifactId>web-utils</artifactId>
4848
<version>1.0-SNAPSHOT</version>
4949
</dependency>
50+
<dependency>
51+
<groupId>info.picocli</groupId>
52+
<artifactId>picocli</artifactId>
53+
<version>4.6.1</version>
54+
</dependency>
5055
</dependencies>
5156

5257
<build>

src/main/java/org/javawebstack/framework/WebApplication.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.javawebstack.abstractdata.AbstractElement;
55
import org.javawebstack.framework.bind.ModelBindParamTransformer;
66
import org.javawebstack.framework.bind.ModelBindTransformer;
7+
import org.javawebstack.framework.commands.HelpCommand;
8+
import org.javawebstack.framework.commands.StartWebServerCommand;
79
import org.javawebstack.framework.config.Config;
810
import org.javawebstack.framework.module.Module;
911
import org.javawebstack.framework.seed.AllSeeder;
@@ -16,9 +18,13 @@
1618
import org.javawebstack.orm.wrapper.SQLDriverNotFoundException;
1719
import org.javawebstack.webutils.*;
1820
import org.javawebstack.webutils.crypt.Crypt;
21+
import picocli.CommandLine;
1922

2023
import java.io.IOException;
24+
import java.io.PrintWriter;
25+
import java.io.Reader;
2126
import java.util.*;
27+
import java.util.concurrent.Callable;
2228
import java.util.logging.Logger;
2329

2430
public abstract class WebApplication {
@@ -36,11 +42,9 @@ public abstract class WebApplication {
3642
private SQLDriverFactory sqlDriverFactory;
3743

3844
public WebApplication() {
39-
4045
setupModules();
4146
modules.forEach(m -> m.beforeSetupConfig(this, config));
4247
setupConfig(config);
43-
4448
crypt = new Crypt(config.has("crypt.key") ? config.get("crypt.key") : Crypt.generateKey());
4549

4650
modules.forEach(m -> m.setupConfig(this, config));
@@ -165,18 +169,29 @@ public I18N getTranslation() {
165169
return translation;
166170
}
167171

168-
protected void setupModules() {
169-
}
172+
protected void setupModules() { }
170173

171174
protected abstract void setupConfig(Config config);
172175

173-
protected void setupSeeding() {
174-
}
176+
protected void setupSeeding() { }
177+
178+
void setupCommands(CommandLine commandLine) { }
175179

176180
protected abstract void setupModels(SQL sql) throws ORMConfigurationException;
177181

178182
protected abstract void setupServer(HTTPServer server);
179183

184+
public void run(String[] args){
185+
HelpCommand help = new HelpCommand();
186+
187+
CommandLine commandLine = new CommandLine(help);
188+
help.setCommandLine(commandLine);
189+
commandLine.addSubcommand(new StartWebServerCommand(this));
190+
setupCommands(commandLine);
191+
192+
commandLine.execute(args);
193+
}
194+
180195
public void start() {
181196
server.start();
182197
server.join();
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.javawebstack.framework.commands;
2+
3+
import picocli.CommandLine;
4+
import picocli.CommandLine.Command;
5+
6+
import java.util.Collections;
7+
import java.util.concurrent.Callable;
8+
9+
@Command(mixinStandardHelpOptions = true)
10+
public class HelpCommand implements Callable<Integer> {
11+
12+
private CommandLine commandLine;
13+
14+
public void setCommandLine(CommandLine commandLine) {
15+
this.commandLine = commandLine;
16+
}
17+
18+
public HelpCommand(){
19+
}
20+
21+
private void printCommands(CommandLine.Help help, int indent){
22+
System.out.println(String.join("",Collections.nCopies(indent, " ")) +help.commandList());
23+
help.subcommands().forEach((n, h) -> printCommands(h, indent+4));
24+
}
25+
26+
public Integer call() {
27+
printCommands(commandLine.getHelp(), 0);
28+
return null;
29+
}
30+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.javawebstack.framework.commands;
2+
3+
import org.javawebstack.framework.WebApplication;
4+
import picocli.CommandLine.*;
5+
6+
import java.util.concurrent.Callable;
7+
8+
@Command(name = "start", description = "Starts the WebServer")
9+
public class StartWebServerCommand implements Callable<Integer> {
10+
private WebApplication webApplication;
11+
12+
/*@Option(names = {"--port", "-p"}, description = "Port")
13+
private int port;*/
14+
15+
public StartWebServerCommand(WebApplication webApplication){
16+
this.webApplication = webApplication;
17+
}
18+
19+
@Command(name = "test")
20+
public void asd(){
21+
22+
}
23+
24+
public Integer call() {
25+
webApplication.start();
26+
return 0;
27+
}
28+
}

src/main/java/org/javawebstack/framework/module/Module.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import org.javawebstack.framework.WebApplication;
44
import org.javawebstack.framework.config.Config;
55
import org.javawebstack.httpserver.HTTPServer;
6-
import org.javawebstack.injector.Injector;
76
import org.javawebstack.orm.exception.ORMConfigurationException;
87
import org.javawebstack.orm.wrapper.SQL;
98
import org.javawebstack.orm.wrapper.SQLDriverFactory;

src/main/java/org/javawebstack/framework/seed/AllSeeder.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
package org.javawebstack.framework.seed;
22

33
import org.javawebstack.framework.WebApplication;
4-
import org.javawebstack.injector.Inject;
54

65
public class AllSeeder implements Seeder {
76

8-
@Inject
97
private WebApplication app;
108

119
public void seed() {

0 commit comments

Comments
 (0)