Go to file
Urko. e445c1ca99 initial commit 2024-08-23 14:11:38 +02:00
cmd initial commit 2024-08-23 14:11:38 +02:00
internal initial commit 2024-08-23 14:11:38 +02:00
.gitignore initial commit 2024-08-23 14:11:38 +02:00
LICENSE initial commit 2024-08-23 14:11:38 +02:00
Makefile initial commit 2024-08-23 14:11:38 +02:00
README.md initial commit 2024-08-23 14:11:38 +02:00
go.mod initial commit 2024-08-23 14:11:38 +02:00
go.sum initial commit 2024-08-23 14:11:38 +02:00
main.go initial commit 2024-08-23 14:11:38 +02:00

README.md

watch-spring

watch-spring is a Windows service written in Go that monitors a specified directory for changes and executes a script when changes are detected. This project is intended for automating tasks such as running backup jobs or deploying files when changes occur in a specified directory.

Features

  • Directory Monitoring: Monitors a specified directory for file changes (specifically write operations).
  • Script Execution: Automatically executes a specified script when changes are detected in the monitored directory.
  • Windows Service: Runs as a Windows service, allowing the process to run in the background, even when no user is logged in.
  • Logging: Supports logging with different levels of verbosity.

Installation

  1. Build the Windows Service Executable:

    • Ensure you have Go installed on your machine.

    • Clone the repository and navigate to the project directory.

    • Build the service executable:

      go build -o watch-spring.exe ./cmd
      
  2. Create the Windows Service:

    • Use the built executable to create a Windows service using the sc command:

      sc create "WatchSpringService" binPath= "C:\path\to\watch-spring.exe --dir C:\path\to\watch --script C:\path\to\script.bat --name BackupService"
      

Usage

Command-Line Flags

Flag Description Example
--dir Directory to monitor for changes. --dir "C:\backup\ovh5win2oneshot"
--script Script to execute when a change is detected. --script "C:\scripts\rclone-deploy.bat"
--name Name of the service (used for logging and identification). --name "BackupService"
--log-level Log verbosity level (0 = Panic, 1 = Fatal, 2 = Error, 3 = Warn, 4 = Info, 5 = Debug, 6 = Trace). --log-level 4

Example

To create and run a Windows service that monitors the C:\backup\ovh5win2oneshot directory and executes the rclone-deploy.bat script when changes are detected, use the following command:

C:\path\to\watch-spring.exe --dir "C:\backup\ovh5win2oneshot" --script "C:\scripts\rclone-deploy.bat" --name "BackupService" --log-level 4

Running the Service

After creating the service, it can be managed using standard Windows service commands:

  • Start the service:
  sc start WatchSpringService
  • Stop the service:
sc stop WatchSpringService
  • Delete the service:
sc delete WatchSpringService

Logging

Logging is handled using logrus, providing different levels of verbosity. The log level can be set using the --log-level flag. By default, the log level is set to Info (level 4).

Development

Prerequisites

  • Go 1.16+ (for building the service)

Building the Project

To build the project, run:

go build -o watch-spring.exe ./cmd

Running the Service in Interactive Mode

While developing, you might want to run the service in interactive mode to see logs directly in the console. Simply omit the service installation steps and run the executable with the desired flags.

watch-spring.exe --dir "C:\path\to\monitor" --script "C:\path\to\script.bat" --name "TestService" --log-level 5

Contributing

Contributions are welcome! If you have any ideas, suggestions, or bug reports, feel free to open an issue or submit a pull request.

License

This project is licensed under the License - see the LICENSE file for details.


This README should give a comprehensive overview of your project, including installation, usage, and development instructions. If you need more specific details or additional sections, feel free to ask!