fw

command module
v0.0.0-...-cc2abf2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 9, 2023 License: MIT Imports: 1 Imported by: 0

README

fw

File Watcher

This tool generates a go template file like this which can be run to watch and execute a command after any file update.

Why?

There are many tools that would do the same job but they require learning and/or setting some configuration files or similar, so I decided to reinvent the wheel and to build one of my own.

With the generated file it is possible to define the directories/files to watch and the commands to run on any file update.

It uses fsnotify.

Installation

go install github.com/Jiang-Gianni/fw@latest

and then run the following inside the desired directory:

fw

which will create (if not already present) the directory watch/fw and the file fw.go in that directory.

To specify the destination directory and filename:

# This creates ./myDir/mySubDir/myWatcher.go
fw -d myDir/mySubDir -f myWatcher.go

After resolving the fsnotify dependency (go mod tidy), update watch/fw/fw.go and run:

go run watch/fw/fw.go

How to setup

Example

Inside the generated file, update the variable fws by adding a value for any filetype / directory / file to watch.

In the generated template:

	{
		directories: []string{"./"},
		files:       []string{"main.go"},
		regexMatch:  ".go$",
		ticker:      time.NewTicker(time.Second),
		signal:      make(chan struct{}),
		run:         GoWatch(),
	},

will watch all the subdirectories and the main.go files. The function definede in the run field is executed when a .go file is updated (regexMatch) at a rate of maximum one per second (ticker field, the reason for this is that some code editors trigger multiple file updates at once).

Another example

Another example: if you have tools like sqlc installed, this file watcher tool can execute sqlc generate by adding the following to the fws list (adjust directories and files watch based on your repository):

	{
		directories: []string{"./sql"},
		files:       []string{},
		regexMatch:  ".sql$",
		ticker:      time.NewTicker(time.Second),
		signal:      make(chan struct{}),
		run: func(filename string, fw *FW) {
			log.Println("SQL file update: ", filename)
			cmd := exec.Command("sqlc", "generate")
			RunCmd(cmd, true)
		},
	},

This will generate/update the go files which in turn will trigger the go file watcher (if present).

Documentation

Overview

Copyright © 2024 Gianni Jiang

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Directories

Path Synopsis
cmd
fw
watch
fw

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL