Skip to content

Commit 3e99d4c

Browse files
committed
Added crypt and migrate commands
1 parent 8847bf4 commit 3e99d4c

File tree

5 files changed

+128
-2
lines changed

5 files changed

+128
-2
lines changed

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
import com.github.javafaker.Faker;
44
import org.javawebstack.command.CommandResult;
55
import org.javawebstack.command.CommandSystem;
6+
import org.javawebstack.command.MultiCommand;
67
import org.javawebstack.framework.bind.ModelBindParamTransformer;
78
import org.javawebstack.framework.bind.ModelBindTransformer;
8-
import org.javawebstack.framework.command.ShellCommand;
9-
import org.javawebstack.framework.command.StartCommand;
9+
import org.javawebstack.framework.command.*;
1010
import org.javawebstack.framework.config.Config;
1111
import org.javawebstack.framework.module.Module;
1212
import org.javawebstack.framework.util.CORSPolicy;
@@ -105,6 +105,12 @@ public WebApplication(){
105105
modules.forEach(m -> m.setupCommands(this, commandSystem));
106106
commandSystem.addCommand("start", new StartCommand());
107107
commandSystem.addCommand("sh", new ShellCommand());
108+
commandSystem.addCommand("migrate", new MigrateCommand());
109+
commandSystem.addCommand("crypt", new MultiCommand()
110+
.add("generate", new CryptGenerateCommand())
111+
.add("encrypt", new CryptEncryptCommand())
112+
.add("hash", new CryptHashCommand())
113+
);
108114
}
109115

110116
public WebApplication addModule(Module module){
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javawebstack.framework.command;
2+
3+
import org.javawebstack.command.Command;
4+
import org.javawebstack.command.CommandResult;
5+
import org.javawebstack.command.CommandSystem;
6+
import org.javawebstack.framework.util.Crypt;
7+
import org.javawebstack.injector.Inject;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class CryptEncryptCommand implements Command {
13+
14+
@Inject
15+
Crypt crypt;
16+
17+
public CommandResult execute(CommandSystem commandSystem, List<String> args, Map<String, List<String>> params) {
18+
System.out.println(crypt.hash(args.size() > 0 ? args.get(0) : ""));
19+
return CommandResult.success();
20+
}
21+
22+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.javawebstack.framework.command;
2+
3+
import org.javawebstack.command.Command;
4+
import org.javawebstack.command.CommandResult;
5+
import org.javawebstack.command.CommandSystem;
6+
import org.javawebstack.framework.config.Config;
7+
import org.javawebstack.framework.util.Crypt;
8+
import org.javawebstack.framework.util.IO;
9+
import org.javawebstack.injector.Inject;
10+
11+
import java.io.IOException;
12+
import java.util.List;
13+
import java.util.Map;
14+
15+
public class CryptGenerateCommand implements Command {
16+
17+
public CommandResult execute(CommandSystem commandSystem, List<String> args, Map<String, List<String>> params) {
18+
String[] lines;
19+
try {
20+
lines = IO.readTextFile(".env").replace("\r", "").split("\\n");
21+
} catch (IOException e) {
22+
lines = new String[0];
23+
}
24+
boolean found = false;
25+
for(int i=0; i<lines.length; i++){
26+
if(lines[i].startsWith("CRYPT_KEY=")){
27+
if(lines[i].length() > 10 && !params.containsKey("f"))
28+
return CommandResult.error("You already have a key. You might loose all your encrypted data if you regenerate it! Use -f to do it anyway.");
29+
lines[i] = "CRYPT_KEY=" + Crypt.generateKey();
30+
found = true;
31+
break;
32+
}
33+
}
34+
if(!found){
35+
String[] newLines = new String[lines.length + 3];
36+
System.arraycopy(lines, 0, newLines, 0, lines.length);
37+
newLines[newLines.length-3] = "";
38+
newLines[newLines.length-2] = "# Encryption Key";
39+
newLines[newLines.length-1] = "CRYPT_KEY=" + Crypt.generateKey();
40+
lines = newLines;
41+
}
42+
try {
43+
IO.writeFile(".env", String.join("\n", lines));
44+
System.out.println("Generated a new key!");
45+
return CommandResult.success();
46+
} catch (IOException e) {
47+
return CommandResult.error(e);
48+
}
49+
}
50+
51+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package org.javawebstack.framework.command;
2+
3+
import org.javawebstack.command.Command;
4+
import org.javawebstack.command.CommandResult;
5+
import org.javawebstack.command.CommandSystem;
6+
import org.javawebstack.framework.util.Crypt;
7+
import org.javawebstack.injector.Inject;
8+
9+
import java.util.List;
10+
import java.util.Map;
11+
12+
public class CryptHashCommand implements Command {
13+
14+
@Inject
15+
Crypt crypt;
16+
17+
public CommandResult execute(CommandSystem commandSystem, List<String> args, Map<String, List<String>> params) {
18+
System.out.println(crypt.hash(args.size() > 0 ? args.get(0) : ""));
19+
return CommandResult.success();
20+
}
21+
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package org.javawebstack.framework.command;
2+
3+
import org.javawebstack.command.Command;
4+
import org.javawebstack.command.CommandResult;
5+
import org.javawebstack.command.CommandSystem;
6+
import org.javawebstack.orm.ORM;
7+
8+
import java.util.List;
9+
import java.util.Map;
10+
11+
public class MigrateCommand implements Command {
12+
13+
public CommandResult execute(CommandSystem commandSystem, List<String> args, Map<String, List<String>> params) {
14+
if(params.containsKey("f")){
15+
// DROP TABLES
16+
}
17+
if(params.containsKey("a")){
18+
ORM.autoMigrate();
19+
System.out.println("Auto-Migration successful!");
20+
return CommandResult.success();
21+
}
22+
return CommandResult.error("Manual migration is not yet fully implemented");
23+
}
24+
25+
}

0 commit comments

Comments
 (0)