Skip to content

Commit

Permalink
Updates structurizr-java to v3.0.0 - adds support for workspace branc…
Browse files Browse the repository at this point in the history
…hes to the push/pull commands.
  • Loading branch information
simonbrowndotje committed Sep 19, 2024
1 parent 301ab80 commit af7166e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 10 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ build
out
*.puml
*.wsd
*.sh
*.sh

*.json
11 changes: 6 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,16 @@ targetCompatibility = 17

repositories {
mavenCentral()
// mavenLocal()
}

dependencies {
def structurizrVersion = '3.0.0'

implementation "com.structurizr:structurizr-dsl:${structurizrVersion}"
implementation "com.structurizr:structurizr-export:${structurizrVersion}"
implementation "com.structurizr:structurizr-autolayout:${structurizrVersion}"
implementation "com.structurizr:structurizr-inspection:${structurizrVersion}"

implementation 'com.structurizr:structurizr-dsl:2.2.0'
implementation 'com.structurizr:structurizr-export:2.2.0'
implementation 'com.structurizr:structurizr-autolayout:2.2.0'
implementation 'com.structurizr:structurizr-inspection:2.2.0'
implementation 'io.github.goto1134:structurizr-d2-exporter:1.5.3'

implementation 'commons-cli:commons-cli:1.5.0'
Expand Down
19 changes: 17 additions & 2 deletions src/main/java/com/structurizr/cli/PullCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.structurizr.Workspace;
import com.structurizr.api.WorkspaceApiClient;
import com.structurizr.util.StringUtils;
import com.structurizr.util.WorkspaceUtils;
import org.apache.commons.cli.*;
import org.apache.commons.logging.Log;
Expand Down Expand Up @@ -35,13 +36,18 @@ public void run(String... args) throws Exception {
option.setRequired(true);
options.addOption(option);

option = new Option("branch", "branch", true, "Branch name");
option.setRequired(false);
options.addOption(option);

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

String apiUrl = "";
long workspaceId = 1;
String apiKey = "";
String apiSecret = "";
String branch = "";

try {
CommandLine cmd = commandLineParser.parse(options, args);
Expand All @@ -50,19 +56,28 @@ public void run(String... args) throws Exception {
workspaceId = Long.parseLong(cmd.getOptionValue("workspaceId"));
apiKey = cmd.getOptionValue("apiKey");
apiSecret = cmd.getOptionValue("apiSecret");
branch = cmd.getOptionValue("branch");
} catch (ParseException e) {
log.error(e.getMessage());
formatter.printHelp("pull", options);

System.exit(1);
}

log.info("Pulling workspace " + workspaceId + " from " + apiUrl);
File file;
if (StringUtils.isNullOrEmpty(branch)) {
log.info("Pulling workspace " + workspaceId + " from " + apiUrl);
file = new File("structurizr-" + workspaceId + "-workspace.json");
} else {
log.info("Pulling workspace " + workspaceId + " from " + apiUrl + " (branch=" + branch + ")");
file = new File("structurizr-" + workspaceId + "-" + branch + "-workspace.json");
}

WorkspaceApiClient client = new WorkspaceApiClient(apiUrl, apiKey, apiSecret);
client.setBranch(branch);
client.setAgent(getAgent());
Workspace workspace = client.getWorkspace(workspaceId);

File file = new File("structurizr-" + workspaceId + "-workspace.json");
WorkspaceUtils.saveWorkspaceToJson(workspace, file);
log.info(" - workspace saved as " + file.getCanonicalPath());
log.info(" - finished");
Expand Down
14 changes: 12 additions & 2 deletions src/main/java/com/structurizr/cli/PushCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ public void run(String... args) throws Exception {
option.setRequired(true);
options.addOption(option);

option = new Option("branch", "branch", true, "Branch name");
option.setRequired(false);
options.addOption(option);

option = new Option("w", "workspace", true, "Path or URL to the workspace JSON/DSL file");
option.setRequired(false);
options.addOption(option);
Expand All @@ -59,6 +63,7 @@ public void run(String... args) throws Exception {
long workspaceId = 1;
String apiKey = "";
String apiSecret = "";
String branch = "";
String workspacePath = "";
String passphrase = "";
boolean mergeFromRemote = true;
Expand All @@ -71,6 +76,7 @@ public void run(String... args) throws Exception {
workspaceId = Long.parseLong(cmd.getOptionValue("workspaceId"));
apiKey = cmd.getOptionValue("apiKey");
apiSecret = cmd.getOptionValue("apiSecret");
branch = cmd.getOptionValue("branch");
workspacePath = cmd.getOptionValue("workspace");
passphrase = cmd.getOptionValue("passphrase");
mergeFromRemote = Boolean.parseBoolean(cmd.getOptionValue("merge", "true"));
Expand All @@ -87,9 +93,14 @@ public void run(String... args) throws Exception {
System.exit(1);
}

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

WorkspaceApiClient client = new WorkspaceApiClient(apiUrl, apiKey, apiSecret);
client.setBranch(branch);
client.setAgent(getAgent());
client.setWorkspaceArchiveLocation(null);

Expand All @@ -111,7 +122,6 @@ public void run(String... args) throws Exception {
log.info(" - parsing model and views from " + path.getCanonicalPath());

Workspace workspace = loadWorkspace(workspacePath);
workspace.setRevision(null);

log.info(" - merge layout from remote: " + mergeFromRemote);
client.setMergeFromRemote(mergeFromRemote);
Expand Down

0 comments on commit af7166e

Please sign in to comment.