aboutsummaryrefslogtreecommitdiff
path: root/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/FileChange.java
diff options
context:
space:
mode:
Diffstat (limited to 'agent/src/main/java/moe/yuuta/dn42peering/agent/provision/FileChange.java')
-rw-r--r--agent/src/main/java/moe/yuuta/dn42peering/agent/provision/FileChange.java66
1 files changed, 66 insertions, 0 deletions
diff --git a/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/FileChange.java b/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/FileChange.java
new file mode 100644
index 0000000..146e41c
--- /dev/null
+++ b/agent/src/main/java/moe/yuuta/dn42peering/agent/provision/FileChange.java
@@ -0,0 +1,66 @@
+package moe.yuuta.dn42peering.agent.provision;
+
+import io.vertx.core.Future;
+import io.vertx.core.Vertx;
+import io.vertx.core.buffer.Buffer;
+import io.vertx.core.file.OpenOptions;
+import io.vertx.core.impl.logging.Logger;
+import io.vertx.core.impl.logging.LoggerFactory;
+
+import javax.annotation.Nonnull;
+import javax.annotation.Nullable;
+
+public class FileChange extends Change {
+ private final Logger logger = LoggerFactory.getLogger(getClass().getSimpleName());
+
+ public enum Action {
+ CREATE_AND_WRITE,
+ OVERWRITE,
+ DELETE
+ }
+
+ public FileChange(@Nonnull String path,
+ @Nullable String contents,
+ @Nonnull String action) {
+ super(path, contents, action);
+ Action.valueOf(action); // Verify
+ }
+
+ @Nonnull
+ @Override
+ public Future<Void> execute(@Nonnull Vertx vertx) {
+ switch (Action.valueOf(action)) {
+ case CREATE_AND_WRITE:
+ logger.info("Writing " + id + " with:\n" + to);
+ return vertx.fileSystem().open(id, new OpenOptions()
+ .setCreateNew(true)
+ .setTruncateExisting(true)
+ .setWrite(true))
+ .compose(asyncFile -> {
+ return asyncFile.write(Buffer.buffer(to == null ? "" : to), 0)
+ .compose(_v -> Future.succeededFuture(asyncFile));
+ })
+ .compose(asyncFile -> {
+ return asyncFile.close();
+ });
+ case OVERWRITE:
+ logger.info("Overwriting " + id + " with:\n" + to);
+ return vertx.fileSystem().open(id, new OpenOptions()
+ .setCreateNew(false)
+ .setTruncateExisting(true)
+ .setWrite(true))
+ .compose(asyncFile -> {
+ return asyncFile.write(Buffer.buffer(to == null ? "" : to), 0)
+ .compose(_v -> Future.succeededFuture(asyncFile));
+ })
+ .compose(asyncFile -> {
+ return asyncFile.close();
+ });
+ case DELETE:
+ logger.info("Deleting " + id);
+ return vertx.fileSystem().delete(id);
+ default:
+ throw new UnsupportedOperationException("Unknown file change action " + action);
+ }
+ }
+}