I want a cli application written in zig 0.14.0 that continuously polls a syncthing events url (http://localhost:8384/rest/events?events=ItemFinished) by default. Syncthing uses the id in the json output to track the last event, so it will need to handle "since=<id>" in the query string after the first request. For all items returned, it should check all the events to see if they match anything configured by the user (the config file can be json, zon, or yaml format). The configuration must allow specification of the folder and path regular expression. If an event matches one of the configured items, the configuration will specify a system command that will be run synchronously, using stdout and stderr of the process. Tests should be written to verify accuracy of the parsing logic for both configuration and events, and the README must document the application. Also, please create a devfile.yaml for future use.
2.2 KiB
2.2 KiB
Syncthing Events Handler
A command-line application written in Zig that monitors Syncthing events and executes configured actions based on file changes.
Features
- Continuously polls Syncthing events API
- Configurable event filtering based on folder and path patterns
- Executes custom commands when matching events are detected
- Supports JSON, ZON, or YAML configuration formats
Installation
zig build
The executable will be created in zig-out/bin/syncthing_events
Configuration
Create a configuration file in either JSON, ZON, or YAML format. Example (in JSON):
{
"syncthing_url": "http://localhost:8384",
"poll_interval_ms": 1000,
"watchers": [
{
"folder": "default",
"path_pattern": ".*\\.pdf$",
"command": "pdftotext \"${path}\" \"${path}.txt\""
},
{
"folder": "photos",
"path_pattern": ".*\\.(jpg|jpeg|png)$",
"command": "convert \"${path}\" -resize 800x600 \"${path}.thumb.jpg\""
}
]
}
Configuration Options
syncthing_url
: Base URL of your Syncthing instance (default: http://localhost:8384)poll_interval_ms
: How often to check for new events in milliseconds (default: 1000)watchers
: Array of event watchers with the following properties:folder
: Syncthing folder ID to watchpath_pattern
: Regular expression to match file pathscommand
: Command to execute when a match is found. Supports variables:${path}
: Full path to the changed file${folder}
: Folder ID where the change occurred${type}
: Event type (e.g., "ItemFinished")
Usage
# Run with default configuration file (config.json)
syncthing_events
# Specify a custom configuration file
syncthing_events --config my-config.yaml
# Override Syncthing URL
syncthing_events --url http://syncthing:8384
Development
This project uses a devfile for consistent development environments. To start developing:
- Install a compatible IDE/editor that supports devfile (e.g., VS Code with DevContainer extension)
- Open the project folder
- The development container will be automatically built with Zig 0.14.0
Running Tests
zig build test
License
MIT License