Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Simplifying interface so scanning happens automatically

  • Loading branch information...
commit 789275af2929868007c70bb169fbe70829111ea6 1 parent b6c8dd7
Steve Armstrong authored
Showing with 398 additions and 244 deletions.
  1. +12 −0 README.md
  2. +5 −0 pom.xml
  3. +13 −28 src/main/java/com/horsefire/syncaws/CommandLineArgs.java
  4. +9 −0 src/main/java/com/horsefire/syncaws/ConfigService.java
  5. +3 −5 src/main/java/com/horsefire/syncaws/Driver.java
  6. +0 −13 src/main/java/com/horsefire/syncaws/SyncAws.java
  7. +10 −15 src/main/java/com/horsefire/syncaws/tasks/CreateTask.java
  8. +24 −0 src/main/java/com/horsefire/syncaws/tasks/HelpTask.java
  9. +7 −2 src/main/java/com/horsefire/syncaws/tasks/InitTask.java
  10. +10 −4 src/main/java/com/horsefire/syncaws/tasks/{ViewTask.java → ListTask.java}
  11. +0 −40 src/main/java/com/horsefire/syncaws/tasks/ProjectTask.java
  12. +0 −49 src/main/java/com/horsefire/syncaws/tasks/ScanTask.java
  13. +160 −0 src/main/java/com/horsefire/syncaws/tasks/StatusTask.java
  14. +21 −26 src/main/java/com/horsefire/syncaws/tasks/TaskFactory.java
  15. +66 −36 src/main/java/com/horsefire/syncaws/tasks/UploadTask.java
  16. +8 −2 src/main/java/com/horsefire/syncaws/tasks/ValidateTask.java
  17. +28 −0 src/main/java/com/horsefire/syncaws/tasks/VersionTask.java
  18. +7 −14 src/test/java/com/horsefire/syncaws/CommandLineArgsBuilder.java
  19. +6 −6 src/test/java/com/horsefire/syncaws/tasks/CreateTaskTest.java
  20. +1 −1  src/test/java/com/horsefire/syncaws/tasks/InitTaskTest.java
  21. +8 −3 src/test/java/com/horsefire/syncaws/tasks/ValidateTaskTest.java
12 README.md
View
@@ -36,3 +36,15 @@ Assuming syncaws is a script to run `java -jar syncaws.jar --configDir <configDi
3. Upload the new index
4. Upload all new files
5. Update the display files to add the new index as the newest
+
+`syncaws list`
+
+1. List all project names
+
+`syncaws version`
+
+1. Display the version
+
+`syncaws help`
+
+1. Display help
5 pom.xml
View
@@ -128,6 +128,11 @@
<artifactId>jcommander</artifactId>
<version>1.18</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.commons</groupId>
+ <artifactId>commons-lang3</artifactId>
+ <version>3.1</version>
+ </dependency>
<dependency>
<groupId>com.horsefire</groupId>
41 src/main/java/com/horsefire/syncaws/CommandLineArgs.java
View
@@ -11,55 +11,40 @@
@Parameter(names = { "-c", "--configDir" }, description = "SyncAws config directory")
private String configDir;
- @Parameter(names = { "-p", "--project" }, description = "Project name")
- private String project = null;
-
- @Parameter(names = { "--dir" }, description = "Project directory")
- private String dir = null;
-
@Parameter(names = { "--debug" }, description = "Enable debug logging")
private boolean debug = false;
- @Parameter(names = { "--dry-run" }, description = "Dry run for upload")
- private boolean dryrun = false;
-
@Parameter
- private List<String> tasks = Lists.newArrayList();
+ private List<String> args = Lists.newArrayList();
public CommandLineArgs() {
}
- CommandLineArgs(String configDir, String project, boolean debug,
- List<String> tasks, String dir, boolean dryRun) {
+ CommandLineArgs(String configDir, boolean debug, List<String> args) {
this.configDir = configDir;
- this.project = project;
this.debug = debug;
- this.tasks = tasks;
- this.dryrun = dryRun;
- this.dir = dir;
+ this.args = args;
}
public String getConfigDir() {
return configDir;
}
- public String getProject() {
- return project;
- }
-
- public String getDir() {
- return dir;
- }
-
public boolean isDebug() {
return debug;
}
- public boolean isDryrun() {
- return dryrun;
+ public String getTask() {
+ if (args.size() > 0) {
+ return args.get(0);
+ }
+ return null;
}
- public List<String> getTasks() {
- return Collections.unmodifiableList(tasks);
+ public List<String> getArgs() {
+ if (args.size() > 1) {
+ return Collections.unmodifiableList(args.subList(1, args.size()));
+ }
+ return Collections.emptyList();
}
}
9 src/main/java/com/horsefire/syncaws/ConfigService.java
View
@@ -87,6 +87,15 @@ public String getBaseUrl() {
return Collections.unmodifiableCollection(Arrays.asList(projects));
}
+ public Project getProject(String name) {
+ for (Project project : getConfig().projects) {
+ if (project.getName().equals(name)) {
+ return project;
+ }
+ }
+ return null;
+ }
+
public void addProject(Project project) {
LOG.debug("Adding new project: {}", project);
Project[] projects = new Project[getConfig().projects.length + 1];
8 src/main/java/com/horsefire/syncaws/Driver.java
View
@@ -35,17 +35,15 @@ public void run() throws Exception {
return;
}
- Task[] tasks = null;
+ Task task = null;
try {
- tasks = m_taskFactory.parseTasks();
+ task = m_taskFactory.getTask();
} catch (RuntimeException e) {
LOG.error("Error parsing tasks: {}", e.getMessage(), e);
return;
}
try {
- for (Task task : tasks) {
- task.run();
- }
+ task.run();
} catch (RuntimeException e) {
LOG.error("Error running task: {}", e.getMessage(), e);
}
13 src/main/java/com/horsefire/syncaws/SyncAws.java
View
@@ -1,8 +1,5 @@
package com.horsefire.syncaws;
-import java.io.InputStream;
-import java.util.Properties;
-
import com.beust.jcommander.JCommander;
import com.beust.jcommander.ParameterException;
@@ -29,16 +26,6 @@ public static void main(String[] args) throws Exception {
System.setProperty("log-level", "DEBUG");
}
- if (options.getTasks().size() == 1
- && options.getTasks().get(0).equals("version")) {
- String path = "/META-INF/maven/com.horsefire/syncaws/pom.properties";
- InputStream stream = SyncAws.class.getResourceAsStream(path);
- Properties props = new Properties();
- props.load(stream);
- System.out.println("SyncAws " + props.get("version"));
- return;
- }
-
Driver.start(options);
}
}
25 src/main/java/com/horsefire/syncaws/tasks/CreateTask.java
View
@@ -2,6 +2,7 @@
import java.io.File;
import java.io.Reader;
+import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -20,7 +21,7 @@
private static final Logger LOG = LoggerFactory.getLogger(CreateTask.class);
- private final CommandLineArgs m_options;
+ private final CommandLineArgs m_args;
private final ConfigService m_configService;
private final UuidGenerator m_generator;
private final AwsClient m_client;
@@ -28,10 +29,10 @@
private final FileDelegate m_fileDelegate;
@Inject
- public CreateTask(CommandLineArgs options, ConfigService configService,
+ public CreateTask(CommandLineArgs args, ConfigService configService,
UuidGenerator generator, AwsClient client, UrlService urlService,
FileDelegate fileDelegate) {
- m_options = options;
+ m_args = args;
m_configService = configService;
m_generator = generator;
m_client = client;
@@ -40,21 +41,16 @@ public CreateTask(CommandLineArgs options, ConfigService configService,
}
public void validate() {
- String projectName = m_options.getProject();
- if (projectName == null || projectName.isEmpty()) {
+ if (m_args.getArgs().size() != 2) {
throw new RuntimeException(
- "Must specify project name when creating a project");
- }
- String dir = m_options.getDir();
- if (dir == null || dir.isEmpty()) {
- throw new RuntimeException(
- "Must specify dir when creating a project");
+ "Task expects project name and directory");
}
}
public void run() throws Exception {
- String projectName = m_options.getProject();
- File dir = new File(m_options.getDir());
+ List<String> args = m_args.getArgs();
+ String projectName = args.get(0);
+ File dir = new File(args.get(1));
if (!m_fileDelegate.isDirectory(dir)) {
throw new RuntimeException("Directory must exist");
}
@@ -82,7 +78,6 @@ public void run() throws Exception {
m_client.putHtml(htmlFile.toString(), m_urlService.getHtmlFile(project));
m_client.putJson("[]", m_urlService.getIndexList(project));
m_configService.addProject(project);
- LOG.info("Added project {} and uploaded index files",
- m_options.getProject());
+ LOG.info("Added project {} and uploaded index files", projectName);
}
}
24 src/main/java/com/horsefire/syncaws/tasks/HelpTask.java
View
@@ -0,0 +1,24 @@
+package com.horsefire.syncaws.tasks;
+
+import com.google.inject.Inject;
+import com.horsefire.syncaws.CommandLineArgs;
+
+public class HelpTask implements Task {
+
+ private final CommandLineArgs m_args;
+
+ @Inject
+ public HelpTask(CommandLineArgs args) {
+ m_args = args;
+ }
+
+ public void validate() {
+ if (!m_args.getArgs().isEmpty()) {
+ throw new RuntimeException("Task does not have arguments");
+ }
+ }
+
+ public void run() throws Exception {
+ throw new UnsupportedOperationException("Help not implemented");
+ }
+}
9 src/main/java/com/horsefire/syncaws/tasks/InitTask.java
View
@@ -1,19 +1,24 @@
package com.horsefire.syncaws.tasks;
import com.google.inject.Inject;
+import com.horsefire.syncaws.CommandLineArgs;
import com.horsefire.syncaws.ConfigService;
public class InitTask implements Task {
+ private final CommandLineArgs m_args;
private final ConfigService m_configService;
@Inject
- public InitTask(ConfigService configService) {
+ public InitTask(CommandLineArgs args, ConfigService configService) {
+ m_args = args;
m_configService = configService;
}
public void validate() {
- // Do nothing
+ if (!m_args.getArgs().isEmpty()) {
+ throw new RuntimeException("Task does not have arguments");
+ }
}
public void run() throws Exception {
14 ...in/java/com/horsefire/syncaws/tasks/ViewTask.java → ...in/java/com/horsefire/syncaws/tasks/ListTask.java
View
@@ -4,25 +4,31 @@
import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
+import com.horsefire.syncaws.CommandLineArgs;
import com.horsefire.syncaws.ConfigService;
import com.horsefire.syncaws.Project;
import com.horsefire.syncaws.aws.UrlService;
-public class ViewTask implements Task {
+public class ListTask implements Task {
- private static final Logger LOG = LoggerFactory.getLogger(ViewTask.class);
+ private static final Logger LOG = LoggerFactory.getLogger(ListTask.class);
+ private final CommandLineArgs m_args;
private final ConfigService m_configService;
private final UrlService m_urlService;
@Inject
- public ViewTask(ConfigService configService, UrlService urlService) {
+ public ListTask(CommandLineArgs args, ConfigService configService,
+ UrlService urlService) {
+ m_args = args;
m_configService = configService;
m_urlService = urlService;
}
public void validate() {
- // Do nothing
+ if (!m_args.getArgs().isEmpty()) {
+ throw new RuntimeException("Task does not have arguments");
+ }
}
public void run() throws Exception {
40 src/main/java/com/horsefire/syncaws/tasks/ProjectTask.java
View
@@ -1,40 +0,0 @@
-package com.horsefire.syncaws.tasks;
-
-import com.horsefire.syncaws.CommandLineArgs;
-import com.horsefire.syncaws.ConfigService;
-import com.horsefire.syncaws.Project;
-
-public abstract class ProjectTask implements Task {
-
- private final CommandLineArgs m_options;
- private final ConfigService m_configService;
- private Project m_selectedProject;
-
- public ProjectTask(CommandLineArgs options, ConfigService configService) {
- m_options = options;
- m_configService = configService;
- }
-
- protected CommandLineArgs getOptions() {
- return m_options;
- }
-
- protected ConfigService getConfig() {
- return m_configService;
- }
-
- protected Project getSelectedProject() {
- return m_selectedProject;
- }
-
- public void validate() {
- for (Project project : getConfig().getProjects()) {
- if (project.getName().equals(getOptions().getProject())) {
- m_selectedProject = project;
- return;
- }
- }
- throw new RuntimeException("No project found for: "
- + m_options.getProject());
- }
-}
49 src/main/java/com/horsefire/syncaws/tasks/ScanTask.java
View
@@ -1,49 +0,0 @@
-package com.horsefire.syncaws.tasks;
-
-import java.io.File;
-import java.io.FileWriter;
-import java.io.IOException;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-import com.google.inject.Inject;
-import com.horsefire.syncaws.CommandLineArgs;
-import com.horsefire.syncaws.ConfigService;
-import com.horsefire.syncaws.Project;
-import com.horsefire.syncaws.fingerprint.Fingerprint;
-import com.horsefire.syncaws.fingerprint.FingerprintSerializer;
-import com.horsefire.syncaws.fingerprint.InkStamp;
-
-public class ScanTask extends ProjectTask {
-
- private static final Logger LOG = LoggerFactory.getLogger(ScanTask.class);
-
- private final InkStamp m_inkStamp;
- private final FingerprintSerializer m_serializer;
-
- @Inject
- public ScanTask(CommandLineArgs options, ConfigService config,
- InkStamp inkStamp, FingerprintSerializer fingerprintSerializer) {
- super(options, config);
- m_inkStamp = inkStamp;
- m_serializer = fingerprintSerializer;
- }
-
- public void run() {
- Project selectedProject = getSelectedProject();
-
- Fingerprint print = m_inkStamp.createNew(new File(selectedProject
- .getBaseDir()));
- String json = m_serializer.save(print);
- try {
- FileWriter writer = new FileWriter(getSelectedProject()
- .getFingerprintFile(getOptions()));
- writer.write(json);
- writer.close();
- LOG.info("Local fingerprint updated");
- } catch (IOException e) {
- throw new RuntimeException("Error serializing out fingerprint", e);
- }
- }
-}
160 src/main/java/com/horsefire/syncaws/tasks/StatusTask.java
View
@@ -0,0 +1,160 @@
+package com.horsefire.syncaws.tasks;
+
+import java.io.File;
+import java.io.FileReader;
+import java.io.FileWriter;
+import java.io.IOException;
+import java.io.Reader;
+import java.security.NoSuchAlgorithmException;
+import java.util.List;
+
+import org.jets3t.service.S3ServiceException;
+import org.jets3t.service.ServiceException;
+import org.json.simple.JSONArray;
+import org.json.simple.parser.JSONParser;
+import org.json.simple.parser.ParseException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.inject.Inject;
+import com.horsefire.syncaws.CommandLineArgs;
+import com.horsefire.syncaws.ConfigService;
+import com.horsefire.syncaws.Project;
+import com.horsefire.syncaws.UuidGenerator;
+import com.horsefire.syncaws.aws.AwsClient;
+import com.horsefire.syncaws.aws.UrlService;
+import com.horsefire.syncaws.backup.Delta;
+import com.horsefire.syncaws.backup.DeltaGenerator;
+import com.horsefire.syncaws.backup.Index;
+import com.horsefire.syncaws.backup.IndexSerializer;
+import com.horsefire.syncaws.backup.UploadedFile;
+import com.horsefire.syncaws.fingerprint.Fingerprint;
+import com.horsefire.syncaws.fingerprint.FingerprintSerializer;
+import com.horsefire.syncaws.fingerprint.InkStamp;
+
+public class StatusTask implements Task {
+
+ private static final Logger LOG = LoggerFactory.getLogger(StatusTask.class);
+
+ private final CommandLineArgs m_args;
+ private final ConfigService m_config;
+ private final AwsClient m_awsClient;
+ private final UrlService m_urlService;
+ private final InkStamp m_inkStamp;
+ private final FingerprintSerializer m_serializer;
+
+ @Inject
+ public StatusTask(CommandLineArgs options, ConfigService config,
+ AwsClient awsClient, UrlService urlService, InkStamp inkStamp,
+ FingerprintSerializer serializer) {
+ m_args = options;
+ m_config = config;
+ m_awsClient = awsClient;
+ m_urlService = urlService;
+ m_inkStamp = inkStamp;
+ m_serializer = serializer;
+ }
+
+ public void validate() {
+ if (m_args.getArgs().size() != 1) {
+ throw new RuntimeException("Task expects project name");
+ }
+ }
+
+ public void updateFingerprint(Project selectedProject) {
+ Fingerprint print = m_inkStamp.createNew(new File(selectedProject
+ .getBaseDir()));
+ String json = m_serializer.save(print);
+ try {
+ FileWriter writer = new FileWriter(
+ selectedProject.getFingerprintFile(m_args));
+ writer.write(json);
+ writer.close();
+ LOG.info("Local fingerprint updated");
+ } catch (IOException e) {
+ throw new RuntimeException("Error serializing out fingerprint", e);
+ }
+ }
+
+ public void run() throws NoSuchAlgorithmException, S3ServiceException {
+ Project selectedProject = m_config.getProject(m_args.getArgs().get(0));
+ updateFingerprint(selectedProject);
+
+ Delta delta = getDelta(selectedProject);
+ List<UploadedFile> files = delta.getFilesToUpload();
+ if (files.isEmpty()) {
+ LOG.info("No new files. Backup is up-to-date");
+ return;
+ }
+ for (UploadedFile file : files) {
+ LOG.info(file.getPath());
+ }
+ LOG.info("{} new files to upload (total size {} bytes)", files.size(),
+ delta.getTotalBytes());
+ }
+
+ private Delta getDelta(Project selectedProject) {
+ Fingerprint print = getFingerprint(selectedProject);
+ Index index = getMostRecentIndex(selectedProject);
+ return new DeltaGenerator(new UuidGenerator()).create(index, print);
+ }
+
+ private Fingerprint getFingerprint(Project selectedProject) {
+ File file = selectedProject.getFingerprintFile(m_args);
+ if (!file.isFile()) {
+ throw new RuntimeException("Index does not exist at " + file);
+ }
+ try {
+ FileReader reader = new FileReader(file);
+ FingerprintSerializer serializer = new FingerprintSerializer();
+ Fingerprint print = serializer.load(reader);
+ reader.close();
+ return print;
+ } catch (IOException e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private JSONArray getIndexList(Project project) {
+ try {
+ Reader reader = m_awsClient.getReader(m_urlService
+ .getIndexList(project));
+ Object object = new JSONParser().parse(reader);
+ reader.close();
+ if (!(object instanceof JSONArray)) {
+ throw new RuntimeException("Error deserializing index list");
+ }
+ return (JSONArray) object;
+ } catch (ServiceException e) {
+ throw new RuntimeException("Error reading index list from S3", e);
+ } catch (IOException e) {
+ throw new RuntimeException("Error reading index list from S3", e);
+ } catch (ParseException e) {
+ throw new RuntimeException("Error reading index list from S3", e);
+ }
+ }
+
+ private Index getMostRecentIndex(Project project) {
+ try {
+ JSONArray indexes = getIndexList(project);
+ if (indexes.isEmpty()) {
+ return null;
+ }
+ Object object = indexes.get(indexes.size() - 1);
+ if (!(object instanceof String)) {
+ throw new RuntimeException("Index must be a string");
+ }
+ Reader reader = m_awsClient.getReader(m_urlService.getIndex(
+ project, (String) object));
+ Index index = new IndexSerializer().load(reader);
+ reader.close();
+ return index;
+ } catch (S3ServiceException e) {
+ throw new RuntimeException("Error loading index", e);
+ } catch (ServiceException e) {
+ throw new RuntimeException("Error loading index", e);
+ } catch (IOException e) {
+ throw new RuntimeException("Error loading index", e);
+ }
+ }
+}
47 src/main/java/com/horsefire/syncaws/tasks/TaskFactory.java
View
@@ -1,9 +1,10 @@
package com.horsefire.syncaws.tasks;
import java.util.HashMap;
-import java.util.List;
import java.util.Map;
+import org.apache.commons.lang3.StringUtils;
+
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.horsefire.syncaws.CommandLineArgs;
@@ -15,43 +16,37 @@
@Inject
public TaskFactory(CommandLineArgs args,
+ Provider<VersionTask> versionTaskProvider,
Provider<InitTask> initTaskProvider,
Provider<ValidateTask> validateTaskProvider,
Provider<CreateTask> createTaskProvider,
- Provider<ScanTask> scanTaskProvider,
+ Provider<StatusTask> statusTaskProvider,
Provider<UploadTask> uploadTaskProvider,
- Provider<ViewTask> viewTaskProvider) {
+ Provider<HelpTask> helpTaskProvider,
+ Provider<ListTask> listTaskProvider) {
m_args = args;
m_taskMapping.put("init", initTaskProvider);
m_taskMapping.put("validate", validateTaskProvider);
m_taskMapping.put("create", createTaskProvider);
- m_taskMapping.put("scan", scanTaskProvider);
+ m_taskMapping.put("status", statusTaskProvider);
m_taskMapping.put("upload", uploadTaskProvider);
- m_taskMapping.put("view", viewTaskProvider);
+ m_taskMapping.put("list", listTaskProvider);
+ m_taskMapping.put("version", versionTaskProvider);
+ m_taskMapping.put("help", helpTaskProvider);
}
- public Task[] parseTasks() {
- List<String> taskNames = m_args.getTasks();
- if (taskNames.size() == 0) {
- StringBuilder message = new StringBuilder(
- "Must specify atleast one of the following tasks: ");
- String spacer = ", ";
- for (String task : m_taskMapping.keySet()) {
- message.append(task).append(spacer);
- }
- message.setLength(message.length() - spacer.length());
- throw new RuntimeException(message.toString());
+ public Task getTask() {
+ String taskName = m_args.getTask();
+ if (taskName == null) {
+ throw new RuntimeException("Must specify a task: "
+ + StringUtils.join(m_taskMapping.keySet(), ", "));
}
- final Task[] tasks = new Task[taskNames.size()];
- for (int i = 0; i < tasks.length; i++) {
- Provider<? extends Task> provider = m_taskMapping.get(taskNames
- .get(i));
- if (provider == null) {
- throw new RuntimeException("Unknown task: " + taskNames.get(i));
- }
- tasks[i] = provider.get();
- tasks[i].validate();
+ Provider<? extends Task> provider = m_taskMapping.get(taskName);
+ if (provider == null) {
+ throw new RuntimeException("Unknown task: " + taskName);
}
- return tasks;
+ Task task = provider.get();
+ task.validate();
+ return task;
}
}
102 src/main/java/com/horsefire/syncaws/tasks/UploadTask.java
View
@@ -2,6 +2,7 @@
import java.io.File;
import java.io.FileReader;
+import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
@@ -34,23 +35,78 @@
import com.horsefire.syncaws.backup.UploadedFile;
import com.horsefire.syncaws.fingerprint.Fingerprint;
import com.horsefire.syncaws.fingerprint.FingerprintSerializer;
+import com.horsefire.syncaws.fingerprint.InkStamp;
-public class UploadTask extends ProjectTask {
+public class UploadTask implements Task {
private static final Logger LOG = LoggerFactory.getLogger(UploadTask.class);
+
+ private final CommandLineArgs m_args;
+ private final ConfigService m_config;
private final AwsClient m_awsClient;
private final UrlService m_urlService;
+ private final InkStamp m_inkStamp;
+ private final FingerprintSerializer m_serializer;
@Inject
public UploadTask(CommandLineArgs options, ConfigService config,
- AwsClient awsClient, UrlService urlService) {
- super(options, config);
+ AwsClient awsClient, UrlService urlService, InkStamp inkStamp,
+ FingerprintSerializer serializer) {
+ m_args = options;
+ m_config = config;
m_awsClient = awsClient;
m_urlService = urlService;
+ m_inkStamp = inkStamp;
+ m_serializer = serializer;
+ }
+
+ public void validate() {
+ if (m_args.getArgs().size() != 1) {
+ throw new RuntimeException("Task expects project name");
+ }
+ }
+
+ public void updateFingerprint(Project selectedProject) {
+ Fingerprint print = m_inkStamp.createNew(new File(selectedProject
+ .getBaseDir()));
+ String json = m_serializer.save(print);
+ try {
+ FileWriter writer = new FileWriter(
+ selectedProject.getFingerprintFile(m_args));
+ writer.write(json);
+ writer.close();
+ LOG.info("Local fingerprint updated");
+ } catch (IOException e) {
+ throw new RuntimeException("Error serializing out fingerprint", e);
+ }
+ }
+
+ public void run() throws NoSuchAlgorithmException, S3ServiceException {
+ Project selectedProject = m_config.getProject(m_args.getArgs().get(0));
+ updateFingerprint(selectedProject);
+
+ Delta delta = getDelta(selectedProject);
+ List<UploadedFile> files = delta.getFilesToUpload();
+ if (files.isEmpty()) {
+ LOG.info("No new files. Backup is up-to-date");
+ return;
+ }
+ LOG.info("{} new files to upload (total size {} bytes)", files.size(),
+ delta.getTotalBytes());
+ String indexName = uploadIndex(selectedProject, delta.getNewIndex());
+ LOG.info("Uploaded new index: {}", indexName);
+ uploadFiles(selectedProject, files);
+ updateIndexList(selectedProject, indexName);
}
- private Fingerprint getFingerprint() {
- File file = getSelectedProject().getFingerprintFile(getOptions());
+ private Delta getDelta(Project selectedProject) {
+ Fingerprint print = getFingerprint(selectedProject);
+ Index index = getMostRecentIndex(selectedProject);
+ return new DeltaGenerator(new UuidGenerator()).create(index, print);
+ }
+
+ private Fingerprint getFingerprint(Project selectedProject) {
+ File file = selectedProject.getFingerprintFile(m_args);
if (!file.isFile()) {
throw new RuntimeException("Index does not exist at " + file);
}
@@ -108,20 +164,14 @@ private Index getMostRecentIndex(Project project) {
}
}
- private Delta getDelta() {
- Fingerprint print = getFingerprint();
- Index index = getMostRecentIndex(getSelectedProject());
- return new DeltaGenerator(new UuidGenerator()).create(index, print);
- }
-
- private String uploadIndex(Index index) {
+ private String uploadIndex(Project selectedProject, Index index) {
String indexJson = new IndexSerializer().save(index);
DateTimeFormatter pattern = DateTimeFormat.forPattern("yyyyMMddHHmm");
String indexName = pattern.print(new DateTime(DateTimeZone.UTC))
+ ".js";
try {
m_awsClient.putJson(indexJson,
- m_urlService.getIndex(getSelectedProject(), indexName));
+ m_urlService.getIndex(selectedProject, indexName));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Error uploading index", e);
} catch (UnsupportedEncodingException e) {
@@ -134,15 +184,14 @@ private String uploadIndex(Index index) {
return indexName;
}
- private void uploadFiles(List<UploadedFile> files) {
+ private void uploadFiles(Project project, List<UploadedFile> files) {
try {
- Project project = getSelectedProject();
for (UploadedFile file : files) {
File localFile = new File(project.getBaseDir(), file.getPath());
LOG.info("Uploading {} ({} bytes)",
localFile.getAbsolutePath(), file.getBytes());
m_awsClient.putFile(localFile,
- m_urlService.getFile(getSelectedProject(), file));
+ m_urlService.getFile(project, file));
}
} catch (S3ServiceException e) {
throw new RuntimeException("Error uploading file", e);
@@ -154,8 +203,7 @@ private void uploadFiles(List<UploadedFile> files) {
}
@SuppressWarnings("unchecked")
- private void updateIndexList(String indexName) {
- Project project = getSelectedProject();
+ private void updateIndexList(Project project, String indexName) {
JSONArray indexList = getIndexList(project);
indexList.add(indexName);
try {
@@ -171,22 +219,4 @@ private void updateIndexList(String indexName) {
throw new RuntimeException("Error updating index list", e);
}
}
-
- public void run() throws NoSuchAlgorithmException, S3ServiceException {
- Delta delta = getDelta();
- List<UploadedFile> files = delta.getFilesToUpload();
- if (files.isEmpty()) {
- LOG.info("No new files. Backup is up-to-date");
- return;
- }
- LOG.info("{} new files to upload (total size {} bytes)", files.size(),
- delta.getTotalBytes());
- if (getOptions().isDryrun()) {
- return;
- }
- String indexName = uploadIndex(delta.getNewIndex());
- LOG.info("Uploaded new index: {}", indexName);
- uploadFiles(files);
- updateIndexList(indexName);
- }
}
10 src/main/java/com/horsefire/syncaws/tasks/ValidateTask.java
View
@@ -4,6 +4,7 @@
import org.slf4j.LoggerFactory;
import com.google.inject.Inject;
+import com.horsefire.syncaws.CommandLineArgs;
import com.horsefire.syncaws.ConfigService;
import com.horsefire.syncaws.aws.AwsClient;
@@ -12,17 +13,22 @@
private static final Logger LOG = LoggerFactory
.getLogger(ValidateTask.class);
+ private final CommandLineArgs m_args;
private final ConfigService m_configService;
private final AwsClient m_awsClient;
@Inject
- public ValidateTask(ConfigService configService, AwsClient awsClient) {
+ public ValidateTask(CommandLineArgs args, ConfigService configService,
+ AwsClient awsClient) {
+ m_args = args;
m_configService = configService;
m_awsClient = awsClient;
}
public void validate() {
- // Do nothing
+ if (!m_args.getArgs().isEmpty()) {
+ throw new RuntimeException("Task does not have arguments");
+ }
}
public void run() throws Exception {
28 src/main/java/com/horsefire/syncaws/tasks/VersionTask.java
View
@@ -0,0 +1,28 @@
+package com.horsefire.syncaws.tasks;
+
+import java.io.InputStream;
+import java.util.Properties;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class VersionTask implements Task {
+
+ private static final Logger LOG = LoggerFactory
+ .getLogger(VersionTask.class);
+
+ public VersionTask() {
+ }
+
+ public void validate() {
+ // Do nothing
+ }
+
+ public void run() throws Exception {
+ String path = "/META-INF/maven/com.horsefire/syncaws/pom.properties";
+ InputStream stream = getClass().getResourceAsStream(path);
+ Properties props = new Properties();
+ props.load(stream);
+ LOG.info("SyncAws {}", props.get("version"));
+ }
+}
21 src/test/java/com/horsefire/syncaws/CommandLineArgsBuilder.java
View
@@ -6,11 +6,8 @@
public class CommandLineArgsBuilder {
private String m_configDir = null;
- private String m_project = null;
- private String m_dir = null;
private boolean m_debug = false;
- private boolean m_dryRun = false;
- private final List<String> m_tasks = new ArrayList<String>();
+ private final List<String> m_args = new ArrayList<String>();
public CommandLineArgsBuilder configDir(String configDir) {
m_configDir = configDir;
@@ -18,13 +15,11 @@ public CommandLineArgsBuilder configDir(String configDir) {
}
public CommandLineArgsBuilder project(String project) {
- m_project = project;
- return this;
+ throw new UnsupportedOperationException();
}
public CommandLineArgsBuilder dir(String dir) {
- m_dir = dir;
- return this;
+ throw new UnsupportedOperationException();
}
public CommandLineArgsBuilder debug() {
@@ -33,17 +28,15 @@ public CommandLineArgsBuilder debug() {
}
public CommandLineArgsBuilder dryRun() {
- m_dryRun = true;
- return this;
+ throw new UnsupportedOperationException();
}
- public CommandLineArgsBuilder addTask(String task) {
- m_tasks.add(task);
+ public CommandLineArgsBuilder addArgs(String args) {
+ m_args.add(args);
return this;
}
public CommandLineArgs build() {
- return new CommandLineArgs(m_configDir, m_project, m_debug, m_tasks,
- m_dir, m_dryRun);
+ return new CommandLineArgs(m_configDir, m_debug, m_args);
}
}
12 src/test/java/com/horsefire/syncaws/tasks/CreateTaskTest.java
View
@@ -29,8 +29,9 @@ public void testCreateProjectWithWindowsPath() throws Exception {
final String dirPath = "C:\\Users\\steve";
final String properDirPath = "C:/Users/steve";
final String projectName = "myProject";
- CommandLineArgs options = new CommandLineArgsBuilder().dir(dirPath)
- .project(projectName).build();
+ CommandLineArgs options = new CommandLineArgsBuilder()
+ .addArgs("create").addArgs(projectName).addArgs(dirPath)
+ .build();
ConfigService configService = createMock(ConfigService.class);
UuidGenerator generator = createMock(UuidGenerator.class);
@@ -38,10 +39,9 @@ public void testCreateProjectWithWindowsPath() throws Exception {
UrlService urlService = createMock(UrlService.class);
FileDelegate fileDelegate = createMock(FileDelegate.class);
- expect(fileDelegate.isDirectory(eq(new File(options.getDir()))))
- .andReturn(true);
- expect(fileDelegate.getAbsolutePath(eq(new File(options.getDir()))))
- .andReturn(dirPath);
+ expect(fileDelegate.isDirectory(eq(new File(dirPath)))).andReturn(true);
+ expect(fileDelegate.getAbsolutePath(eq(new File(dirPath)))).andReturn(
+ dirPath);
Collection<Project> projects = Collections.emptyList();
expect(configService.getProjects()).andReturn(projects);
2  src/test/java/com/horsefire/syncaws/tasks/InitTaskTest.java
View
@@ -15,7 +15,7 @@ private void runInit() throws Exception {
CommandLineArgs args = new CommandLineArgsBuilder().configDir(
getSandboxPath()).build();
ConfigService configService = new ConfigService(args);
- InitTask task = new InitTask(configService);
+ InitTask task = new InitTask(args, configService);
task.validate();
task.run();
11 src/test/java/com/horsefire/syncaws/tasks/ValidateTaskTest.java
View
@@ -10,6 +10,8 @@
import org.jets3t.service.S3ServiceException;
import org.junit.Test;
+import com.horsefire.syncaws.CommandLineArgs;
+import com.horsefire.syncaws.CommandLineArgsBuilder;
import com.horsefire.syncaws.ConfigService;
import com.horsefire.syncaws.aws.AwsClient;
@@ -23,7 +25,8 @@ public void testHappyPath() throws Exception {
awsClient.testConnection();
expectLastCall().once();
replay(configService, awsClient);
- ValidateTask task = new ValidateTask(configService, awsClient);
+ CommandLineArgs args = new CommandLineArgsBuilder().build();
+ ValidateTask task = new ValidateTask(args, configService, awsClient);
task.validate();
task.run();
verify(configService, awsClient);
@@ -36,7 +39,8 @@ public void testConfigFailure() throws Exception {
new RuntimeException("config"));
AwsClient awsClient = createMock(AwsClient.class);
replay(configService, awsClient);
- ValidateTask task = new ValidateTask(configService, awsClient);
+ CommandLineArgs args = new CommandLineArgsBuilder().build();
+ ValidateTask task = new ValidateTask(args, configService, awsClient);
task.validate();
try {
task.run();
@@ -56,7 +60,8 @@ public void testS3Failure() throws Exception {
awsClient.testConnection();
expectLastCall().andThrow(new S3ServiceException()).once();
replay(configService, awsClient);
- ValidateTask task = new ValidateTask(configService, awsClient);
+ CommandLineArgs args = new CommandLineArgsBuilder().build();
+ ValidateTask task = new ValidateTask(args, configService, awsClient);
task.validate();
try {
task.run();
Please sign in to comment.
Something went wrong with that request. Please try again.