Skip to content

Commit

Permalink
Adds a -debug flag to the push and pull commands, to help with debugg…
Browse files Browse the repository at this point in the history
…ing why requests fail.
  • Loading branch information
simonbrowndotje committed Jan 16, 2025
1 parent 8b014b9 commit 709e1c2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
25 changes: 25 additions & 0 deletions src/main/java/com/structurizr/cli/AbstractCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@
import org.apache.hc.client5.http.impl.classic.CloseableHttpResponse;
import org.apache.hc.client5.http.impl.classic.HttpClients;
import org.apache.hc.core5.http.io.entity.EntityUtils;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.builder.impl.BuiltConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;

import java.io.File;
import java.net.URL;
Expand Down Expand Up @@ -140,4 +146,23 @@ protected Class loadClass(String fqn, File workspaceFile) throws Exception {
return childClassLoader.loadClass(fqn);
}

protected void configureDebugLogging() {
ConfigurationBuilder<BuiltConfiguration> builder = ConfigurationBuilderFactory.newConfigurationBuilder();

builder.add(
builder.newAppender("stdout", "Console")
.add(
builder.newLayout(PatternLayout.class.getSimpleName())
.addAttribute(
"pattern",
"%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"
)
)
);

builder.add(builder.newLogger("com.structurizr", Level.DEBUG));

Configurator.reconfigure(builder.build());
}

}
10 changes: 10 additions & 0 deletions src/main/java/com/structurizr/cli/PullCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ public void run(String... args) throws Exception {
option.setRequired(false);
options.addOption(option);

option = new Option("debug", "debug", false, "Enable debug logging");
option.setRequired(false);
options.addOption(option);

CommandLineParser commandLineParser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();

Expand All @@ -54,6 +58,7 @@ public void run(String... args) throws Exception {
String apiSecret = "";
String branch = "";
String passphrase = "";
boolean debug = false;

try {
CommandLine cmd = commandLineParser.parse(options, args);
Expand All @@ -64,13 +69,18 @@ public void run(String... args) throws Exception {
apiSecret = cmd.getOptionValue("apiSecret");
branch = cmd.getOptionValue("branch");
passphrase = cmd.getOptionValue("passphrase");
debug = cmd.hasOption("debug");
} catch (ParseException e) {
log.error(e.getMessage());
formatter.printHelp("pull", options);

System.exit(1);
}

if (debug) {
configureDebugLogging();
}

File file;
if (StringUtils.isNullOrEmpty(branch)) {
log.info("Pulling workspace " + workspaceId + " from " + apiUrl);
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/structurizr/cli/PushCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@ public void run(String... args) throws Exception {
option.setRequired(false);
options.addOption(option);

option = new Option("debug", "debug", false, "Enable debug logging");
option.setRequired(false);
options.addOption(option);

CommandLineParser commandLineParser = new DefaultParser();
HelpFormatter formatter = new HelpFormatter();

Expand All @@ -68,6 +72,7 @@ public void run(String... args) throws Exception {
String passphrase = "";
boolean mergeFromRemote = true;
boolean archive = true;
boolean debug = false;

try {
CommandLine cmd = commandLineParser.parse(options, args);
Expand All @@ -81,6 +86,7 @@ public void run(String... args) throws Exception {
passphrase = cmd.getOptionValue("passphrase");
mergeFromRemote = Boolean.parseBoolean(cmd.getOptionValue("merge", "true"));
archive = Boolean.parseBoolean(cmd.getOptionValue("archive", "true"));
debug = cmd.hasOption("debug");

if (StringUtils.isNullOrEmpty(workspacePath)) {
log.error("-workspace must be specified");
Expand All @@ -93,6 +99,10 @@ public void run(String... args) throws Exception {
System.exit(1);
}

if (debug) {
configureDebugLogging();
}

if (StringUtils.isNullOrEmpty(branch)) {
log.info("Pushing workspace " + workspaceId + " to " + apiUrl);
} else {
Expand Down

0 comments on commit 709e1c2

Please sign in to comment.