|
1 | 1 | package tv.codely.apps; |
2 | 2 |
|
| 3 | +import java.util.Arrays; |
| 4 | +import java.util.HashMap; |
| 5 | + |
3 | 6 | import org.springframework.boot.SpringApplication; |
4 | 7 | import org.springframework.boot.WebApplicationType; |
5 | 8 | import org.springframework.context.ConfigurableApplicationContext; |
| 9 | + |
6 | 10 | import tv.codely.apps.backoffice.backend.BackofficeBackendApplication; |
7 | 11 | import tv.codely.apps.backoffice.frontend.BackofficeFrontendApplication; |
8 | 12 | import tv.codely.apps.mooc.backend.MoocBackendApplication; |
9 | 13 | import tv.codely.shared.infrastructure.cli.ConsoleCommand; |
10 | 14 |
|
11 | | -import java.util.Arrays; |
12 | | -import java.util.HashMap; |
13 | | - |
14 | 15 | public class Starter { |
15 | | - public static void main(String[] args) { |
16 | | - if (args.length < 2) { |
17 | | - throw new RuntimeException("There are not enough arguments"); |
18 | | - } |
19 | | - |
20 | | - String applicationName = args[0]; |
21 | | - String commandName = args[1]; |
22 | | - boolean isServerCommand = commandName.equals("server"); |
23 | | - |
24 | | - ensureApplicationExist(applicationName); |
25 | | - ensureCommandExist(applicationName, commandName); |
26 | | - |
27 | | - Class<?> applicationClass = applications().get(applicationName); |
28 | | - |
29 | | - SpringApplication app = new SpringApplication(applicationClass); |
30 | | - |
31 | | - if (!isServerCommand) { |
32 | | - app.setWebApplicationType(WebApplicationType.NONE); |
33 | | - } |
34 | | - |
35 | | - ConfigurableApplicationContext context = app.run(args); |
36 | | - |
37 | | - if (!isServerCommand) { |
38 | | - ConsoleCommand command = (ConsoleCommand) context.getBean( |
39 | | - commands().get(applicationName).get(commandName) |
40 | | - ); |
41 | | - |
42 | | - command.execute(Arrays.copyOfRange(args, 2, args.length)); |
43 | | - } |
44 | | - } |
45 | | - |
46 | | - private static void ensureApplicationExist(String applicationName) { |
47 | | - if (!applications().containsKey(applicationName)) { |
48 | | - throw new RuntimeException(String.format( |
49 | | - "The application <%s> doesn't exist. Valids:\n- %s", |
50 | | - applicationName, |
51 | | - String.join("\n- ", applications().keySet()) |
52 | | - )); |
53 | | - } |
54 | | - } |
55 | | - |
56 | | - private static void ensureCommandExist(String applicationName, String commandName) { |
57 | | - if (!"server".equals(commandName) && !existCommand(applicationName, commandName)) { |
58 | | - throw new RuntimeException(String.format( |
59 | | - "The command <%s> for application <%s> doesn't exist. Valids (application.command):\n- api\n- %s", |
60 | | - commandName, |
61 | | - applicationName, |
62 | | - String.join("\n- ", commands().get(applicationName).keySet()) |
63 | | - )); |
64 | | - } |
65 | | - } |
66 | | - |
67 | | - private static HashMap<String, Class<?>> applications() { |
68 | | - HashMap<String, Class<?>> applications = new HashMap<>(); |
69 | | - |
70 | | - applications.put("mooc_backend", MoocBackendApplication.class); |
71 | | - applications.put("backoffice_backend", BackofficeBackendApplication.class); |
72 | | - applications.put("backoffice_frontend", BackofficeFrontendApplication.class); |
73 | | - |
74 | | - return applications; |
75 | | - } |
76 | | - |
77 | | - private static HashMap<String, HashMap<String, Class<?>>> commands() { |
78 | | - HashMap<String, HashMap<String, Class<?>>> commands = new HashMap<>(); |
79 | | - |
80 | | - commands.put("mooc_backend", MoocBackendApplication.commands()); |
81 | | - commands.put("backoffice_backend", BackofficeBackendApplication.commands()); |
82 | | - commands.put("backoffice_frontend", BackofficeFrontendApplication.commands()); |
83 | | - |
84 | | - return commands; |
85 | | - } |
86 | | - |
87 | | - private static Boolean existCommand(String applicationName, String commandName) { |
88 | | - HashMap<String, HashMap<String, Class<?>>> commands = commands(); |
89 | | - |
90 | | - return commands.containsKey(applicationName) && commands.get(applicationName).containsKey(commandName); |
91 | | - } |
| 16 | + |
| 17 | + public static void main(String[] args) { |
| 18 | + if (args.length < 2) { |
| 19 | + throw new RuntimeException("There are not enough arguments"); |
| 20 | + } |
| 21 | + |
| 22 | + String applicationName = args[0]; |
| 23 | + String commandName = args[1]; |
| 24 | + boolean isServerCommand = commandName.equals("server"); |
| 25 | + |
| 26 | + ensureApplicationExist(applicationName); |
| 27 | + ensureCommandExist(applicationName, commandName); |
| 28 | + |
| 29 | + Class<?> applicationClass = applications().get(applicationName); |
| 30 | + |
| 31 | + SpringApplication app = new SpringApplication(applicationClass); |
| 32 | + |
| 33 | + if (!isServerCommand) { |
| 34 | + app.setWebApplicationType(WebApplicationType.NONE); |
| 35 | + } |
| 36 | + |
| 37 | + ConfigurableApplicationContext context = app.run(args); |
| 38 | + |
| 39 | + if (!isServerCommand) { |
| 40 | + ConsoleCommand command = (ConsoleCommand) context.getBean(commands().get(applicationName).get(commandName)); |
| 41 | + |
| 42 | + command.execute(Arrays.copyOfRange(args, 2, args.length)); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + private static void ensureApplicationExist(String applicationName) { |
| 47 | + if (!applications().containsKey(applicationName)) { |
| 48 | + throw new RuntimeException( |
| 49 | + String.format( |
| 50 | + "The application <%s> doesn't exist. Valids:\n- %s", |
| 51 | + applicationName, |
| 52 | + String.join("\n- ", applications().keySet()) |
| 53 | + ) |
| 54 | + ); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + private static void ensureCommandExist(String applicationName, String commandName) { |
| 59 | + if (!"server".equals(commandName) && !existCommand(applicationName, commandName)) { |
| 60 | + throw new RuntimeException( |
| 61 | + String.format( |
| 62 | + "The command <%s> for application <%s> doesn't exist. Valids (application.command):\n- api\n- %s", |
| 63 | + commandName, |
| 64 | + applicationName, |
| 65 | + String.join("\n- ", commands().get(applicationName).keySet()) |
| 66 | + ) |
| 67 | + ); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static HashMap<String, Class<?>> applications() { |
| 72 | + HashMap<String, Class<?>> applications = new HashMap<>(); |
| 73 | + |
| 74 | + applications.put("mooc_backend", MoocBackendApplication.class); |
| 75 | + applications.put("backoffice_backend", BackofficeBackendApplication.class); |
| 76 | + applications.put("backoffice_frontend", BackofficeFrontendApplication.class); |
| 77 | + |
| 78 | + return applications; |
| 79 | + } |
| 80 | + |
| 81 | + private static HashMap<String, HashMap<String, Class<?>>> commands() { |
| 82 | + HashMap<String, HashMap<String, Class<?>>> commands = new HashMap<>(); |
| 83 | + |
| 84 | + commands.put("mooc_backend", MoocBackendApplication.commands()); |
| 85 | + commands.put("backoffice_backend", BackofficeBackendApplication.commands()); |
| 86 | + commands.put("backoffice_frontend", BackofficeFrontendApplication.commands()); |
| 87 | + |
| 88 | + return commands; |
| 89 | + } |
| 90 | + |
| 91 | + private static Boolean existCommand(String applicationName, String commandName) { |
| 92 | + HashMap<String, HashMap<String, Class<?>>> commands = commands(); |
| 93 | + |
| 94 | + return commands.containsKey(applicationName) && commands.get(applicationName).containsKey(commandName); |
| 95 | + } |
92 | 96 | } |
0 commit comments