Compare commits
10 commits
96f5ddfca6
...
2fd37ab057
Author | SHA1 | Date | |
---|---|---|---|
2fd37ab057 | |||
56cd0714ac | |||
a8f57197a7 | |||
b491449295 | |||
dcf157f4d6 | |||
ea0f079473 | |||
d9398c0843 | |||
1e2691683a | |||
f04e851a66 | |||
771d7d7b4c |
3 changed files with 99 additions and 35 deletions
|
@ -47,6 +47,8 @@ Create a configuration file in either JSON, ZON, or YAML format. Example (in JSO
|
|||
- `watchers`: Array of event watchers with the following properties:
|
||||
- `folder`: Syncthing folder ID to watch
|
||||
- `path_pattern`: Regular expression to match file paths
|
||||
- `action`: Action to match on (deleted, updated, modified, etc). Defaults to '*', which is all actions
|
||||
- `event_type`: [Event type](https://docs.syncthing.net/dev/events.html#event-types) to match on. Defaults to ItemFinished
|
||||
- `command`: Command to execute when a match is found. Supports variables:
|
||||
- `${path}`: Full path to the changed file
|
||||
- `${folder}`: Folder ID where the change occurred
|
||||
|
@ -81,4 +83,4 @@ zig build test
|
|||
|
||||
## License
|
||||
|
||||
MIT License
|
||||
MIT License
|
||||
|
|
31
src/main.zig
31
src/main.zig
|
@ -6,6 +6,11 @@ const EventPoller = lib.EventPoller;
|
|||
const Args = struct {
|
||||
config_path: []const u8 = "config.json",
|
||||
syncthing_url: ?[]const u8 = null,
|
||||
|
||||
pub fn deinit(self: Args, allocator: std.mem.Allocator) void {
|
||||
allocator.free(self.config_path);
|
||||
if (self.syncthing_url) |url| allocator.free(url);
|
||||
}
|
||||
};
|
||||
|
||||
pub const std_options: std.Options = .{
|
||||
|
@ -32,6 +37,7 @@ pub fn main() !u8 {
|
|||
const allocator = gpa.allocator();
|
||||
|
||||
const args = try parseArgs(allocator);
|
||||
defer args.deinit(allocator);
|
||||
|
||||
const file = try std.fs.cwd().openFile(args.config_path, .{});
|
||||
defer file.close();
|
||||
|
@ -61,15 +67,24 @@ pub fn main() !u8 {
|
|||
}
|
||||
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("Monitoring Syncthing events at {s}\n", .{config.syncthing_url});
|
||||
|
||||
var last_id: ?i64 = null;
|
||||
const connection_pool = std.http.Client.ConnectionPool{};
|
||||
while (true) {
|
||||
var arena_alloc = std.heap.ArenaAllocator.init(allocator);
|
||||
// Most processing is fairly small
|
||||
var stack_fallback_allocator = std.heap.stackFallback(1024 * 2, allocator);
|
||||
var arena_alloc = std.heap.ArenaAllocator.init(stack_fallback_allocator.get());
|
||||
defer arena_alloc.deinit();
|
||||
const arena = arena_alloc.allocator();
|
||||
|
||||
var poller = try EventPoller.init(arena, api_key, config);
|
||||
var poller = try EventPoller.init(
|
||||
arena,
|
||||
api_key,
|
||||
config,
|
||||
connection_pool,
|
||||
);
|
||||
if (last_id == null) // first run
|
||||
try stdout.print("Monitoring Syncthing events at {s}\n", .{try poller.url()});
|
||||
defer last_id = poller.last_id;
|
||||
poller.last_id = last_id;
|
||||
const events = poller.poll() catch |err| switch (err) {
|
||||
|
@ -93,9 +108,13 @@ pub fn main() !u8 {
|
|||
|
||||
for (events) |event| {
|
||||
for (config.watchers) |watcher| {
|
||||
if (watcher.matches(event.folder, event.path, event.action)) {
|
||||
try stdout.print("Match found for folder {s}, path {s}, executing command\n\t{s}\n", .{ event.folder, event.path, watcher.command });
|
||||
try lib.executeCommand(allocator, watcher.command, event);
|
||||
if (watcher.matches(event)) {
|
||||
try stdout.print(
|
||||
"Match - Folder: {s} Action: {s} Event type: {s} Path: {s}\n",
|
||||
.{ event.folder, event.action, event.event_type, event.path },
|
||||
);
|
||||
std.log.debug("Executing command \n\t{s}", .{watcher.command});
|
||||
try lib.executeCommand(arena, watcher.command, event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
99
src/root.zig
99
src/root.zig
|
@ -12,22 +12,29 @@ pub const Config = struct {
|
|||
pub const Watcher = struct {
|
||||
folder: []const u8,
|
||||
path_pattern: []const u8,
|
||||
action: []const u8,
|
||||
action: []const u8 = "*",
|
||||
event_type: []const u8 = "ItemFinished",
|
||||
command: []const u8,
|
||||
compiled_pattern: ?mvzr.Regex = null,
|
||||
|
||||
pub fn matches(self: *Watcher, folder: []const u8, path: []const u8, action: []const u8) bool {
|
||||
if (!std.mem.eql(u8, folder, self.folder)) {
|
||||
pub fn matches(self: *Watcher, event: SyncthingEvent) bool {
|
||||
if (!std.mem.eql(u8, event.folder, self.folder)) {
|
||||
return false;
|
||||
}
|
||||
if (!std.mem.eql(u8, event.event_type, self.event_type)) {
|
||||
return false;
|
||||
}
|
||||
std.log.debug(
|
||||
"Watcher match on folder {s}. Checking path {s} against pattern {s}",
|
||||
.{ folder, path, self.path_pattern },
|
||||
"Watcher match on folder {s}/event type {s}. Checking path {s} against pattern {s}",
|
||||
.{ event.folder, event.event_type, event.path, self.path_pattern },
|
||||
);
|
||||
if (!std.mem.eql(u8, action, self.action)) {
|
||||
const action_match =
|
||||
(self.action.len == 1 and self.action[0] == '*') or
|
||||
std.mem.eql(u8, event.action, self.action);
|
||||
if (!action_match) {
|
||||
std.log.debug(
|
||||
"Event action {s}, but watching for action {s}. Skipping command",
|
||||
.{ action, self.action },
|
||||
.{ event.action, self.action },
|
||||
);
|
||||
return false;
|
||||
}
|
||||
|
@ -36,28 +43,38 @@ pub const Watcher = struct {
|
|||
std.log.err("watcher path_pattern failed to compile and will never match: {s}", .{self.path_pattern});
|
||||
}
|
||||
if (self.compiled_pattern) |pattern|
|
||||
return pattern.isMatch(path);
|
||||
return pattern.isMatch(event.path);
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
pub const SyncthingEvent = struct {
|
||||
id: i64,
|
||||
event_type: []const u8,
|
||||
data_type: []const u8,
|
||||
folder: []const u8,
|
||||
path: []const u8,
|
||||
action: []const u8,
|
||||
time: []const u8,
|
||||
original_json: []const u8,
|
||||
|
||||
pub fn fromJson(allocator: std.mem.Allocator, value: std.json.Value) !SyncthingEvent {
|
||||
const data = value.object.get("data").?.object;
|
||||
// event values differ on this point...
|
||||
const path = data.get("item") orelse data.get("path");
|
||||
|
||||
var al = std.ArrayList(u8).init(allocator);
|
||||
defer al.deinit();
|
||||
try std.json.stringify(value, .{ .whitespace = .indent_2 }, al.writer());
|
||||
return SyncthingEvent{
|
||||
.id = value.object.get("id").?.integer,
|
||||
.time = try allocator.dupe(u8, value.object.get("time").?.string),
|
||||
.event_type = try allocator.dupe(u8, value.object.get("type").?.string),
|
||||
.data_type = try allocator.dupe(u8, data.get("type").?.string),
|
||||
.folder = try allocator.dupe(u8, data.get("folder").?.string),
|
||||
.action = try allocator.dupe(u8, data.get("action").?.string),
|
||||
.path = try allocator.dupe(u8, data.get("item").?.string),
|
||||
.path = try allocator.dupe(u8, path.?.string),
|
||||
.original_json = try al.toOwnedSlice(),
|
||||
};
|
||||
}
|
||||
|
||||
|
@ -67,6 +84,7 @@ pub const SyncthingEvent = struct {
|
|||
allocator.free(self.folder);
|
||||
allocator.free(self.action);
|
||||
allocator.free(self.path);
|
||||
allocator.free(self.original_json);
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -75,18 +93,46 @@ pub const EventPoller = struct {
|
|||
config: Config,
|
||||
last_id: ?i64,
|
||||
api_key: []u8,
|
||||
connection_pool: std.http.Client.ConnectionPool,
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator, api_key: []u8, config: Config) !EventPoller {
|
||||
pub fn init(allocator: std.mem.Allocator, api_key: []u8, config: Config, connection_pool: ?std.http.Client.ConnectionPool) !EventPoller {
|
||||
return .{
|
||||
.allocator = allocator,
|
||||
.config = config,
|
||||
.last_id = null,
|
||||
.api_key = api_key,
|
||||
.connection_pool = connection_pool orelse .{},
|
||||
};
|
||||
}
|
||||
|
||||
pub fn url(self: EventPoller) ![]const u8 {
|
||||
const watched_events = blk: {
|
||||
var type_set = std.StringArrayHashMap(void).init(self.allocator);
|
||||
try type_set.ensureTotalCapacity(self.config.watchers.len);
|
||||
for (self.config.watchers) |watcher|
|
||||
type_set.putAssumeCapacity(watcher.event_type, {});
|
||||
break :blk try std.mem.join(self.allocator, ",", type_set.keys());
|
||||
};
|
||||
var since_buf: [100]u8 = undefined;
|
||||
const since = if (self.last_id) |id|
|
||||
try std.fmt.bufPrint(&since_buf, "&since={d}", .{id})
|
||||
else
|
||||
"";
|
||||
return try std.fmt.allocPrint(
|
||||
self.allocator,
|
||||
"{s}/rest/events?events={s}{s}",
|
||||
.{
|
||||
self.config.syncthing_url, watched_events, since,
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
pub fn poll(self: *EventPoller) ![]SyncthingEvent {
|
||||
var client = std.http.Client{ .allocator = self.allocator };
|
||||
var client = std.http.Client{
|
||||
.allocator = self.allocator,
|
||||
.connection_pool = self.connection_pool,
|
||||
};
|
||||
defer client.deinit();
|
||||
var arena = std.heap.ArenaAllocator.init(self.allocator);
|
||||
defer arena.deinit();
|
||||
const aa = arena.allocator();
|
||||
|
@ -99,22 +145,13 @@ pub const EventPoller = struct {
|
|||
const MAX_UCF_RETRIES: usize = 20;
|
||||
var retry_count: usize = 0;
|
||||
const first_run = self.last_id == null;
|
||||
const poll_url = try self.url();
|
||||
while (retry_count < self.config.max_retries) : (retry_count += 1) {
|
||||
var url_buf: [1024]u8 = undefined;
|
||||
var since_buf: [100]u8 = undefined;
|
||||
const since = if (self.last_id) |id|
|
||||
try std.fmt.bufPrint(&since_buf, "&since={d}", .{id})
|
||||
else
|
||||
"";
|
||||
const url = try std.fmt.bufPrint(&url_buf, "{s}/rest/events?events=ItemFinished{s}", .{
|
||||
self.config.syncthing_url, since,
|
||||
});
|
||||
|
||||
var al = std.ArrayList(u8).init(self.allocator);
|
||||
defer al.deinit();
|
||||
|
||||
const response = client.fetch(.{
|
||||
.location = .{ .url = url },
|
||||
.location = .{ .url = poll_url },
|
||||
.response_storage = .{ .dynamic = &al },
|
||||
.headers = .{
|
||||
.authorization = .{ .override = auth },
|
||||
|
@ -217,7 +254,13 @@ fn expandCommandVariables(allocator: std.mem.Allocator, command: []const u8, eve
|
|||
} else if (std.mem.eql(u8, var_name, "folder")) {
|
||||
try result.appendSlice(event.folder);
|
||||
} else if (std.mem.eql(u8, var_name, "data_type")) {
|
||||
try result.appendSlice(event.event_type);
|
||||
} else if (std.mem.eql(u8, var_name, "event_type")) {
|
||||
try result.appendSlice(event.data_type);
|
||||
} else if (std.mem.eql(u8, var_name, "action")) {
|
||||
try result.appendSlice(event.action);
|
||||
} else if (std.mem.eql(u8, var_name, "dump")) {
|
||||
try result.appendSlice(event.original_json);
|
||||
}
|
||||
i = j + 1;
|
||||
continue;
|
||||
|
@ -316,11 +359,11 @@ test "watcher pattern matching" {
|
|||
.action = "update",
|
||||
};
|
||||
|
||||
try std.testing.expect(watcher.matches("photos", "test.jpg", "update"));
|
||||
try std.testing.expect(watcher.matches("photos", "test.jpeg", "update"));
|
||||
try std.testing.expect(!watcher.matches("photos", "test.png", "update"));
|
||||
try std.testing.expect(!watcher.matches("documents", "test.jpg", "update"));
|
||||
try std.testing.expect(!watcher.matches("photos", "test.jpeg", "delete"));
|
||||
try std.testing.expect(watcher.matches(.{ .folder = "photos", .path = "test.jpg", .data_type = "update", .event_type = "ItemFinished" }));
|
||||
try std.testing.expect(watcher.matches(.{ .folder = "photos", .path = "test.jpeg", .data_type = "update", .event_type = "ItemFinished" }));
|
||||
try std.testing.expect(watcher.matches(.{ .folder = "photos", .path = "test.png", .data_type = "update", .event_type = "ItemFinished" }));
|
||||
try std.testing.expect(watcher.matches(.{ .folder = "documents", .path = "test.jpg", .data_type = "update", .event_type = "ItemFinished" }));
|
||||
try std.testing.expect(watcher.matches(.{ .folder = "photos", .path = "test.jpeg", .data_type = "delete", .event_type = "ItemFinished" }));
|
||||
}
|
||||
|
||||
test "end to end config / event" {
|
||||
|
@ -363,5 +406,5 @@ test "end to end config / event" {
|
|||
var event = try SyncthingEvent.fromJson(std.testing.allocator, parsed_event.value);
|
||||
defer event.deinit(std.testing.allocator);
|
||||
|
||||
try std.testing.expect(config.watchers[0].matches(event.folder, event.path, event.action));
|
||||
try std.testing.expect(config.watchers[0].matches(event));
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue