{"id":13471762,"url":"https://github.com/radovskyb/watcher","last_synced_at":"2025-05-14T08:08:23.603Z","repository":{"id":38274016,"uuid":"69343521","full_name":"radovskyb/watcher","owner":"radovskyb","description":"watcher is a Go package for watching for files or directory changes without using filesystem events.","archived":false,"fork":false,"pushed_at":"2023-10-12T22:53:15.000Z","size":176,"stargazers_count":1495,"open_issues_count":50,"forks_count":185,"subscribers_count":32,"default_branch":"master","last_synced_at":"2025-04-11T02:51:59.480Z","etag":null,"topics":["cross-platform","filesystem-events","go","golang","notifier","polling","recursive","watcher"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/radovskyb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2016-09-27T10:01:12.000Z","updated_at":"2025-04-07T00:55:38.000Z","dependencies_parsed_at":"2024-06-18T11:11:06.404Z","dependency_job_id":"81ee0ccd-a758-4fff-956c-afdfdfa101a2","html_url":"https://github.com/radovskyb/watcher","commit_stats":{"total_commits":242,"total_committers":11,"mean_commits":22.0,"dds":0.06611570247933884,"last_synced_commit":"f5989f8deca223d590d5a130c77ea375fe9fde30"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radovskyb%2Fwatcher","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radovskyb%2Fwatcher/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radovskyb%2Fwatcher/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/radovskyb%2Fwatcher/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/radovskyb","download_url":"https://codeload.github.com/radovskyb/watcher/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254101558,"owners_count":22014908,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cross-platform","filesystem-events","go","golang","notifier","polling","recursive","watcher"],"created_at":"2024-07-31T16:00:49.056Z","updated_at":"2025-05-14T08:08:23.535Z","avatar_url":"https://github.com/radovskyb.png","language":"Go","funding_links":[],"categories":["Go","Web","Repositories","Libraries"],"sub_categories":[],"readme":"# watcher\n\n[![Build Status](https://travis-ci.org/radovskyb/watcher.svg?branch=master)](https://travis-ci.org/radovskyb/watcher)\n\n`watcher` is a Go package for watching for files or directory changes (recursively or non recursively) without using filesystem events, which allows it to work cross platform consistently.\n\n`watcher` watches for changes and notifies over channels either anytime an event or an error has occurred.\n\nEvents contain the `os.FileInfo` of the file or directory that the event is based on and the type of event and file or directory path.\n\n[Installation](#installation)  \n[Features](#features)  \n[Example](#example)  \n[Contributing](#contributing)  \n[Watcher Command](#command)  \n\n# Update\n- Event.OldPath has been added [Aug 17, 2019]\n- Added new file filter hooks (Including a built in regexp filtering hook) [Dec 12, 2018]\n- Event.Path for Rename and Move events is now returned in the format of `fromPath -\u003e toPath`\n\n#### Chmod event is not supported under windows.\n\n# Installation\n\n```shell\ngo get -u github.com/radovskyb/watcher/...\n```\n\n# Features\n\n- Customizable polling interval.\n- Filter Events.\n- Watch folders recursively or non-recursively.\n- Choose to ignore hidden files.\n- Choose to ignore specified files and folders.\n- Notifies the `os.FileInfo` of the file that the event is based on. e.g `Name`, `ModTime`, `IsDir`, etc.\n- Notifies the full path of the file that the event is based on or the old and new paths if the event was a `Rename` or `Move` event.\n- Limit amount of events that can be received per watching cycle.\n- List the files being watched.\n- Trigger custom events.\n\n# Todo\n\n- Write more tests.\n- Write benchmarks.\n\n# Example\n\n```go\npackage main\n\nimport (\n\t\"fmt\"\n\t\"log\"\n\t\"time\"\n\n\t\"github.com/radovskyb/watcher\"\n)\n\nfunc main() {\n\tw := watcher.New()\n\n\t// SetMaxEvents to 1 to allow at most 1 event's to be received\n\t// on the Event channel per watching cycle.\n\t//\n\t// If SetMaxEvents is not set, the default is to send all events.\n\tw.SetMaxEvents(1)\n\n\t// Only notify rename and move events.\n\tw.FilterOps(watcher.Rename, watcher.Move)\n\n\t// Only files that match the regular expression during file listings\n\t// will be watched.\n\tr := regexp.MustCompile(\"^abc$\")\n\tw.AddFilterHook(watcher.RegexFilterHook(r, false))\n\n\tgo func() {\n\t\tfor {\n\t\t\tselect {\n\t\t\tcase event := \u003c-w.Event:\t\n\t\t\t\tfmt.Println(event) // Print the event's info.\n\t\t\tcase err := \u003c-w.Error:\n\t\t\t\tlog.Fatalln(err)\n\t\t\tcase \u003c-w.Closed:\n\t\t\t\treturn\n\t\t\t}\n\t\t}\n\t}()\n\n\t// Watch this folder for changes.\n\tif err := w.Add(\".\"); err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\t// Watch test_folder recursively for changes.\n\tif err := w.AddRecursive(\"../test_folder\"); err != nil {\n\t\tlog.Fatalln(err)\n\t}\n\n\t// Print a list of all of the files and folders currently\n\t// being watched and their paths.\n\tfor path, f := range w.WatchedFiles() {\n\t\tfmt.Printf(\"%s: %s\\n\", path, f.Name())\n\t}\n\n\tfmt.Println()\n\n\t// Trigger 2 events after watcher started.\n\tgo func() {\n\t\tw.Wait()\n\t\tw.TriggerEvent(watcher.Create, nil)\n\t\tw.TriggerEvent(watcher.Remove, nil)\n\t}()\n\n\t// Start the watching process - it'll check for changes every 100ms.\n\tif err := w.Start(time.Millisecond * 100); err != nil {\n\t\tlog.Fatalln(err)\n\t}\n}\n```\n\n# Contributing\nIf you would ike to contribute, simply submit a pull request.\n\n# Command\n\n`watcher` comes with a simple command which is installed when using the `go get` command from above.\n\n# Usage\n\n```\nUsage of watcher:\n  -cmd string\n    \tcommand to run when an event occurs\n  -dotfiles\n    \twatch dot files (default true)\n  -ignore string\n        comma separated list of paths to ignore\n  -interval string\n    \twatcher poll interval (default \"100ms\")\n  -keepalive\n    \tkeep alive when a cmd returns code != 0\n  -list\n    \tlist watched files on start\n  -pipe\n    \tpipe event's info to command's stdin\n  -recursive\n    \twatch folders recursively (default true)\n  -startcmd\n    \trun the command when watcher starts\n```\n\nAll of the flags are optional and watcher can also be called by itself:\n```shell\nwatcher\n```\n(watches the current directory recursively for changes and notifies any events that occur.)\n\nA more elaborate example using the `watcher` command:\n```shell\nwatcher -dotfiles=false -recursive=false -cmd=\"./myscript\" main.go ../\n```\nIn this example, `watcher` will ignore dot files and folders and won't watch any of the specified folders recursively. It will also run the script `./myscript` anytime an event occurs while watching `main.go` or any files or folders in the previous directory (`../`).\n\nUsing the `pipe` and `cmd` flags together will send the event's info to the command's stdin when changes are detected.\n\nFirst create a file called `script.py` with the following contents:\n```python\nimport sys\n\nfor line in sys.stdin:\n\tprint (line + \" - python\")\n```\n\nNext, start watcher with the `pipe` and `cmd` flags enabled:\n```shell\nwatcher -cmd=\"python script.py\" -pipe=true\n```\n\nNow when changes are detected, the event's info will be output from the running python script.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fradovskyb%2Fwatcher","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fradovskyb%2Fwatcher","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fradovskyb%2Fwatcher/lists"}