spyrun is a tool that watches files and executes commands when specific events occur. It can watch for file modifications, additions, and more.
cargo install spyrun
spyrun operates using a configuration file.
> spyrun --help
Usage: spyrun.exe [OPTIONS]
Options:
-c, --config <FILE> Sets a custom config file [default: spyrun.toml]
-d, --debug... Turn debugging information on
-h, --help Print help
-V, --version Print version
spyrun's configuration file is in TOML format.
The default filename is spyrun.toml
, located in the same directory as the executable.
The configuration file specifies the files to watch, the commands to execute, and various other options.
- example
[vars]
base = '{{ cwd }}/example'
hostname = '{{ env(arg="COMPUTERNAME") }}'
version = '20240407_125639'
fn_toast = '''
function Show-Toast {
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)][String]$title,
[Parameter(Mandatory=$true)][String]$message,
[Parameter(Mandatory=$true)][String]$detail
)
[Windows.UI.Notifications.ToastNotificationManager, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.UI.Notifications.ToastNotification, Windows.UI.Notifications, ContentType = WindowsRuntime] | Out-Null
[Windows.Data.Xml.Dom.XmlDocument, Windows.Data.Xml.Dom.XmlDocument, ContentType = WindowsRuntime] | Out-Null
$app_id = '{1AC14E77-02E7-4E5D-B744-2EB1AE5198B7}\WindowsPowerShell\v1.0\powershell.exe'
$content = @"
<?xml version="1.0" encoding="utf-8"?>
<toast>
<visual>
<binding template="ToastGeneric">
<text>$($title)</text>
<text>$($message)</text>
<text>$($detail)</text>
</binding>
</visual>
</toast>
"@
$xml = New-Object Windows.Data.Xml.Dom.XmlDocument
$xml.LoadXml($content)
$toast = New-Object Windows.UI.Notifications.ToastNotification $xml
[Windows.UI.Notifications.ToastNotificationManager]::CreateToastNotifier($app_id).Show($toast)
}
'''
[cfg]
stop_flg = '{{ base }}/stop.flg'
stop_force_flg = '{{ base }}/stop.force.flg'
max_threads = 8
[log]
path = '{{ base }}/log/{{ cmd_stem }}.log'
level = 'info'
[init]
cmd = 'powershell'
arg = ['-NoProfile', '-Command', '''& {
{{ fn_toast }}
Show-Toast -title "spyrun {{ version }} on {{ hostname }}" -message "spyrun is running" -detail "spyrun path is {{ cmd_path }}. config path is {{ cfg_path }}."
}''']
# watch files and notifiy.
[[spys]]
name = 'toast'
input = '{{ base }}/watch_dir'
output = '{{ base }}/log'
[[spys.patterns]]
pattern = '\.txt$'
cmd = 'powershell'
arg = ['-NoProfile', '-Command', '''& {
{{ fn_toast }}
Show-Toast -title "{{ event_name }}" -message "{{ event_path }} is {{ event_kind }}" -detail "name: {{ event_name }}. dir: {{ event_dir }}"
}''']
Variables can be set freely. Variables are defined in alphabetical order. Therefore, it is necessary to pay attention to the order.
- OK
[vars]
a = "a"
b = "b and {{ a }}"
- NG
[vars]
a = "a and {{ b }}"
b = "b"
The file path to stop the spyrun. When it detects that this path has been created or modified, it completes all running operations and exits.
The file path to force stop the spyrun. When it detects that this path has been created or modified, it forces the spyrun to stop immediately.
The maximum number of threads to use in the spyrun. The default value is based on the number of CPU cores.
The path to the log file.
The log level.
The default value is info
.
You can specify the following values.
- off
- error
- warn
- info
- debug
- trace
Init is executed when spyrun starts.
The command to execute.
The arguments to pass to the command.
The list of spy.
The name of the spy.
The list of events. Default value is ['Create', 'Modify']. You can specify the following values.
- Access
- Create
- Modify
- Remove
The path to watch.
The path to output. Standard output and standard error is written to this path.
If you want to watch the input path recursively, set this to true. Default value is false.
If you want to debounce execution, set this setting. Default value is 50 milliseconds.
If you want to throttle execution, set this setting. Default value is 0 milliseconds.
debounce or throttle is applied to this key. Default value is Display for CommandInfo.
The delay to wait before executing the command. Default value is 0 milliseconds.
- one param
Execute after 5000 milliseconds.
delay = [5000]
- two params
Waits randomly between 5000 milliseconds and 10000 milliseconds before executing.
delay = [5000, 10000]
The list of patterns.
The pattern to watch. This is a regular expression.
The command to execute.
The arguments to pass to the command.
If you want to watch the input path in a polling mode, set this setting.
The interval to watch the input path.
If you want to walk the input path, set this setting. If this is set, the input path is also walked when spyrun starts.
The minimum depth to walk the input path.
The maximum depth to walk the input path.
If you want to follow symlinks, set this setting.
The pattern to match the input path. This is a regular expression.
The delay to wait before walking the input path.
- one param
Walk after 5000 milliseconds.
delay = [5000]
- two params
Waits randomly between 5000 milliseconds and 10000 milliseconds.
delay = [5000, 10000]
spyrun is distributed under the MIT License.