Skip to content

Commit 9a8967c

Browse files
Renamed the "backup" command to "cloud-to-onpremises" - added functionality related to users and branches.
1 parent 355adaa commit 9a8967c

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

src/main/java/com/structurizr/cli/BackupCommand.java renamed to src/main/java/com/structurizr/cli/CloudToOnPremisesCommand.java

Lines changed: 36 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,22 @@
1414
import java.util.List;
1515
import java.util.Properties;
1616

17-
class BackupCommand extends AbstractCommand {
17+
class CloudToOnPremisesCommand extends AbstractCommand {
1818

19-
private static final Log log = LogFactory.getLog(BackupCommand.class);
19+
private static final Log log = LogFactory.getLog(CloudToOnPremisesCommand.class);
20+
private static final String CLOUD_SERVICE_API_URL = "https://api.structurizr.com";
2021

21-
BackupCommand() {
22+
CloudToOnPremisesCommand() {
2223
}
2324

2425
public void run(String... args) throws Exception {
2526
Options options = new Options();
2627

27-
Option option = new Option("url", "structurizrApiUrl", true, "Structurizr API URL (default: https://api.structurizr.com)");
28-
option.setRequired(false);
29-
options.addOption(option);
30-
31-
option = new Option("key", "apiKey", true, "API key");
28+
Option option = new Option("key", "apiKey", true, "API key");
3229
option.setRequired(true);
3330
options.addOption(option);
3431

35-
option = new Option("user", "username", true, "Username (only required for cloud service)");
32+
option = new Option("user", "username", true, "Username");
3633
option.setRequired(false);
3734
options.addOption(option);
3835

@@ -47,7 +44,6 @@ public void run(String... args) throws Exception {
4744
CommandLineParser commandLineParser = new DefaultParser();
4845
HelpFormatter formatter = new HelpFormatter();
4946

50-
String apiUrl = "";
5147
String username = "";
5248
String apiKey = "";
5349
boolean debug = false;
@@ -56,14 +52,13 @@ public void run(String... args) throws Exception {
5652
try {
5753
CommandLine cmd = commandLineParser.parse(options, args);
5854

59-
apiUrl = cmd.getOptionValue("structurizrApiUrl", "https://api.structurizr.com");
6055
apiKey = cmd.getOptionValue("apiKey");
6156
username = cmd.getOptionValue("username");
6257
outputPath = cmd.getOptionValue("output");
6358
debug = cmd.hasOption("debug");
6459
} catch (ParseException e) {
6560
log.error(e.getMessage());
66-
formatter.printHelp("backup", options);
61+
formatter.printHelp("cloud-to-onpremises", options);
6762

6863
System.exit(1);
6964
}
@@ -77,28 +72,50 @@ public void run(String... args) throws Exception {
7772
throw new RuntimeException("Output directory already exists: " + outputDir.getAbsolutePath());
7873
}
7974

80-
AdminApiClient adminApiClient = new AdminApiClient(apiUrl, username, apiKey);
75+
AdminApiClient adminApiClient = new AdminApiClient(CLOUD_SERVICE_API_URL, username, apiKey);
8176
List<WorkspaceMetadata> workspaces = adminApiClient.getWorkspaces();
8277

8378
if (!workspaces.isEmpty()) {
8479
outputDir.mkdirs();
8580

8681
File structurizrPropertiesFile = new File(outputDir, "structurizr.properties");
87-
Files.writeString(structurizrPropertiesFile.toPath(), "structurizr.workspaces=*");
82+
Files.writeString(structurizrPropertiesFile.toPath(), "structurizr.feature.workspace.branches=true");
8883
log.info(" - " + structurizrPropertiesFile.getCanonicalPath());
8984

85+
File structurizrUsersFile = new File(outputDir, "structurizr.users");
86+
Files.writeString(structurizrUsersFile.toPath(), username + "=$2a$06$uM5wM.eJwrPq1RM/gBXRr.d0bfyu9ABxdE56qYbRLSCZzqfR7xHcC");
87+
log.info(" - " + structurizrUsersFile.getCanonicalPath());
88+
9089
for (WorkspaceMetadata workspaceMetadata : workspaces) {
9190
File workspaceDirectory = new File(outputDir, "" + workspaceMetadata.getId());
9291
workspaceDirectory.mkdirs();
9392

94-
WorkspaceApiClient client = new WorkspaceApiClient(apiUrl, workspaceMetadata.getApiKey(), workspaceMetadata.getApiSecret());
93+
WorkspaceApiClient client = new WorkspaceApiClient(CLOUD_SERVICE_API_URL, workspaceMetadata.getApiKey(), workspaceMetadata.getApiSecret());
9594
client.setAgent(getAgent());
9695

96+
// main branch
9797
String json = client.getWorkspaceAsJson(workspaceMetadata.getId());
9898
File workspaceJsonFile = new File(workspaceDirectory, "workspace.json");
9999
Files.writeString(workspaceJsonFile.toPath(), json);
100100
log.info(" - " + workspaceJsonFile.getCanonicalPath());
101101

102+
// branches
103+
if (workspaceMetadata.getBranches() != null) {
104+
File branchesDirectory = new File(workspaceDirectory, "branches");
105+
branchesDirectory.mkdirs();
106+
107+
for (String branch : workspaceMetadata.getBranches()) {
108+
File branchDirectory = new File(branchesDirectory, branch);
109+
branchDirectory.mkdir();
110+
111+
client.setBranch(branch);
112+
json = client.getWorkspaceAsJson(workspaceMetadata.getId());
113+
File branchJsonFile = new File(branchDirectory, "workspace.json");
114+
Files.writeString(branchJsonFile.toPath(), json);
115+
log.info(" - " + branchJsonFile.getCanonicalPath());
116+
}
117+
}
118+
102119
Properties properties = new Properties();
103120
properties.setProperty("name", workspaceMetadata.getName() != null ? workspaceMetadata.getName() : "");
104121
properties.setProperty("description", workspaceMetadata.getDescription() != null ? workspaceMetadata.getDescription() : "");
@@ -110,6 +127,10 @@ public void run(String... args) throws Exception {
110127
}
111128
properties.setProperty("clientSideEncrypted", "" + (json.contains("\"encryptionStrategy\"") && json.contains("\"ciphertext\"")));
112129

130+
properties.setProperty("owner", workspaceMetadata.getUsers().getOwner());
131+
properties.setProperty("writeUsers", workspaceMetadata.getUsers().getOwner() + ", " + String.join(",", workspaceMetadata.getUsers().getWrite()));
132+
properties.setProperty("readUsers", String.join(",", workspaceMetadata.getUsers().getWrite()));
133+
113134
File workspacePropertiesFile = new File(workspaceDirectory, "workspace.properties");
114135
FileWriter fileWriter = new FileWriter(workspacePropertiesFile);
115136
properties.store(fileWriter, null);

src/main/java/com/structurizr/cli/StructurizrCliApplication.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class StructurizrCliApplication {
1919

2020
private static final String PUSH_COMMAND = "push";
2121
private static final String PULL_COMMAND = "pull";
22-
private static final String BACKUP_COMMAND = "backup";
22+
private static final String CLOUD_TO_ONPREMISES_COMMAND = "cloud-to-onpremises";
2323
private static final String LOCK_COMMAND = "lock";
2424
private static final String UNLOCK_COMMAND = "unlock";
2525
private static final String EXPORT_COMMAND = "export";
@@ -61,7 +61,7 @@ public class StructurizrCliApplication {
6161

6262
COMMANDS.put(PUSH_COMMAND, new PushCommand());
6363
COMMANDS.put(PULL_COMMAND, new PullCommand());
64-
COMMANDS.put(BACKUP_COMMAND, new BackupCommand());
64+
COMMANDS.put(CLOUD_TO_ONPREMISES_COMMAND, new CloudToOnPremisesCommand());
6565
COMMANDS.put(LOCK_COMMAND, new LockCommand());
6666
COMMANDS.put(UNLOCK_COMMAND, new UnlockCommand());
6767
COMMANDS.put(EXPORT_COMMAND, new ExportCommand());

0 commit comments

Comments
 (0)