1 package emissary.command;
2
3 import emissary.client.EmissaryClient;
4 import emissary.client.response.ConfigsResponseEntity;
5 import emissary.server.api.Configs;
6
7 import org.apache.commons.lang3.StringUtils;
8 import org.apache.hc.client5.http.classic.methods.HttpGet;
9 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory;
11 import picocli.CommandLine;
12 import picocli.CommandLine.Command;
13 import picocli.CommandLine.Model.CommandSpec;
14 import picocli.CommandLine.Option;
15 import picocli.CommandLine.ParameterException;
16 import picocli.CommandLine.Spec;
17
18 import java.io.IOException;
19
20 @Command(description = "Test the configuration for a place", subcommands = {HelpCommand.class})
21 public class ConfigCommand extends HttpCommand {
22 @Spec
23 private CommandSpec spec;
24 private static final Logger logger = LoggerFactory.getLogger(ConfigCommand.class);
25 public static final int DEFAULT_PORT = 8001;
26 public static final String COMMAND_NAME = "config";
27
28 @Option(names = {"--place"}, description = "fully-qualified place", arity = "1", required = true)
29 private String place;
30
31 @Option(names = {"--detailed"}, description = "get verbose output when parsing the configs\nDefault: ${DEFAULT-VALUE}")
32 private boolean detailed = false;
33
34 @Option(names = {"--offline"}, description = "run the config command in offline mode (useful for local testing)\nDefault: ${DEFAULT-VALUE}")
35 private boolean offline = false;
36
37 @Override
38 public String getCommandName() {
39 return COMMAND_NAME;
40 }
41
42 @Override
43 public int getDefaultPort() {
44 return DEFAULT_PORT;
45 }
46
47 @Override
48 public void setupCommand() {
49 setupHttp();
50
51 if (!offline && StringUtils.isNotBlank(getFlavor())) {
52 throw new ParameterException(spec.commandLine(), "--flavor can only be specified in offline mode");
53 }
54
55 if (offline && StringUtils.isBlank(getFlavor())) {
56
57 overrideFlavor("STANDALONE");
58 }
59 }
60
61 @Override
62 public void run(CommandLine c) {
63 setup();
64 try {
65 ConfigsResponseEntity entity = offline ? getOfflineConfigs() : getConfigs();
66 entity.dumpToConsole();
67 } catch (Exception e) {
68 LOG.error("Problem getting configs: {}", e.getMessage());
69 }
70 }
71
72 public ConfigsResponseEntity getConfigs() {
73 String endpoint = getScheme() + "://" + getHost() + ":" + getPort() + "/api/configuration/" + (detailed ? "detailed/" : "") + this.place;
74 LOG.debug("Hitting {}", endpoint);
75 EmissaryClient client = new EmissaryClient();
76 return client.send(new HttpGet(endpoint)).getContent(ConfigsResponseEntity.class);
77 }
78
79 public ConfigsResponseEntity getOfflineConfigs() throws IOException {
80 logger.debug("Offline mode");
81 return Configs.getConfigsResponse(place, detailed);
82 }
83
84 }