Skip to content

Commit

Permalink
First commit
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagozs committed Aug 31, 2023
0 parents commit 1d4d609
Show file tree
Hide file tree
Showing 12 changed files with 593 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.envrc
build/*
!build/.gitkeep
11 changes: 11 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Copyright 2023, Thiago Zilli Sarmento

All rights reserved. No permission is hereby granted to any person obtaining a copy of this software and associated documentation files. We reserve the rights to deal in the Software, to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software or to permit persons to whom the Software is furnished to do so.

In case you have our prior or written permission you are subject to the following conditions:

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

If you begin patent litigation against the Licensor over patents that you think may apply to the software (including a cross-claim or counterclaim in a lawsuit), your license to the software ends automatically.

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
68 changes: 68 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
# Go-Reload Runner (beta)

## Overview

`Go-Reload Runner` is a lightweight utility that watches for file changes in a specified directory and automatically runs a given command when a change is detected. It's particularly useful for developers who want to automate tasks like rebuilding or testing their code upon changes.

## Features

- Watches a directory for file changes
- Excludes specified directories from being watched
- Runs a specified command when a change is detected

## Installation

Clone the repository and navigate to the project directory:

```bash
git clone https://github.com/thiagozs/go-reload.git
cd go-reload
```

Build the project:

```bash
go build -o reload-runner
```

## Usage

### Basic Usage

To monitor the current directory and run `echo 'hello'` when a change is detected:

```bash
./reload-runner -dir . -cmd "echo 'hello'"
```

### Excluding Directories

To exclude certain directories from being watched:

```bash
./reload-runner -dir . -cmd "echo 'hello'" -exclude "test,logs"
```

### Example

Let's say you have a Go project and you want to rebuild it whenever a `.go` file changes. You can use `Go-Reload Runner` as follows:

```bash
./reload-runner -dir . -cmd "go build -o my_app main.go"
```

When you save a `.go` file in the directory, `Go-Reload Runner` will automatically run `go build -o my_app main.go`, rebuilding your application.

You must need a folder called `build` inside in your development folder, `Go-Reload Runner` going detect automatic this command and going make a binary file to execute

## Contributing

Feel free to open issues or submit pull requests. Your contributions are welcome!

-----

## Versioning and license

Our version numbers follow the [semantic versioning specification](http://semver.org/). You can see the available versions by checking the [tags on this repository](https://github.com/thiagozs/go-reload/tags). For more details about our license model, please take a look at the [LICENSE](LICENSE) file.

**2023**, thiagozs.
Empty file added build/.gitkeep
Empty file.
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module github.com/thiagozs/go-reload

go 1.21.0

require (
github.com/fsnotify/fsnotify v1.6.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
golang.org/x/sys v0.0.0-20220908164124-27713097b956 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
14 changes: 14 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY=
github.com/fsnotify/fsnotify v1.6.0/go.mod h1:sl3t1tCWJFWoRz9R8WJCbQihKKwmorjAbSClcnxKAGw=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220908164124-27713097b956 h1:XeJjHH1KiLpKGb6lvMiksZ9l0fVUh+AmGcm0nOMEBOY=
golang.org/x/sys v0.0.0-20220908164124-27713097b956/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
62 changes: 62 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package main

import (
"flag"
"log"
"os"
"os/signal"
"strings"
"syscall"

"github.com/thiagozs/go-reload/runner"
)

var (
dirToMonitor string
command string
excluded string
)

func main() {
flag.StringVar(&dirToMonitor, "dir", ".", "Directory to monitor for changes")
flag.StringVar(&command, "cmd", "", "Command to run when a change is detected")
flag.StringVar(&excluded, "exclude", "", "Comma-separated list of directories to exclude from monitoring")
flag.Parse()

if command == "" {
log.Fatal("Please specify a command to run using the -cmd flag")
}

excludedDirs := make(map[string]bool)
for _, dir := range strings.Split(excluded, ",") {
excludedDirs[dir] = true
}

realWatcher, err := runner.NewRealWatcher()
if err != nil {
log.Fatal(err)
}

opts := []runner.CmdOpts{
runner.DirToMonitor(dirToMonitor),
runner.Command(command),
runner.ExcludedDirs(excludedDirs),
runner.RegisterWatcher(realWatcher),
}

r, err := runner.NewCommandRunner(opts...)
if err != nil {
log.Fatal(err)
}

go r.WatchForChanges()

done := make(chan os.Signal, 1)
signal.Notify(done, syscall.SIGINT, syscall.SIGTERM)

<-done

realWatcher.Close()

log.Println("Stopping the watcher...")
}
82 changes: 82 additions & 0 deletions runner/options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package runner

type CmdRunnerParams struct {
dirToMonitor string
command string
excludedDirs map[string]bool
watcher Watcher
}

type CmdOpts func(o *CmdRunnerParams) error

func newCmdParams(opts ...CmdOpts) (*CmdRunnerParams, error) {
params := &CmdRunnerParams{}
for _, opt := range opts {
if err := opt(params); err != nil {
return nil, err
}
}
return params, nil
}

func DirToMonitor(dir string) CmdOpts {
return func(o *CmdRunnerParams) error {
o.dirToMonitor = dir
return nil
}
}

func Command(cmd string) CmdOpts {
return func(o *CmdRunnerParams) error {
o.command = cmd
return nil
}
}

func ExcludedDirs(dirs map[string]bool) CmdOpts {
return func(o *CmdRunnerParams) error {
o.excludedDirs = dirs
return nil
}
}

func RegisterWatcher(watcher Watcher) CmdOpts {
return func(o *CmdRunnerParams) error {
o.watcher = watcher
return nil
}
}

// getters and setters

func (r CmdRunnerParams) GetDirToMonitor() string {
return r.dirToMonitor
}

func (r CmdRunnerParams) GetCommand() string {
return r.command
}

func (r CmdRunnerParams) GetExcludedDirs() map[string]bool {
return r.excludedDirs
}

func (r CmdRunnerParams) GetWatcher() Watcher {
return r.watcher
}

func (r *CmdRunnerParams) SetDirToMonitor(dir string) {
r.dirToMonitor = dir
}

func (r *CmdRunnerParams) SetCommand(cmd string) {
r.command = cmd
}

func (r *CmdRunnerParams) SetExcludedDirs(dirs map[string]bool) {
r.excludedDirs = dirs
}

func (r *CmdRunnerParams) SetWatcher(watcher Watcher) {
r.watcher = watcher
}
42 changes: 42 additions & 0 deletions runner/options_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package runner

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestOptions_GettersAndSetters(t *testing.T) {
params := &CmdRunnerParams{}
mockWatcher := &MockWatcher{}

// Test setters
params.SetDirToMonitor(".")
params.SetCommand("echo 'hello'")
params.SetExcludedDirs(map[string]bool{"test": true})
params.SetWatcher(mockWatcher)

// Test getters
assert.Equal(t, ".", params.GetDirToMonitor())
assert.Equal(t, "echo 'hello'", params.GetCommand())
assert.Equal(t, map[string]bool{"test": true}, params.GetExcludedDirs())
assert.Equal(t, mockWatcher, params.GetWatcher())
}

func TestCmdOpts(t *testing.T) {
mockWatcher := &MockWatcher{}

// Test newCmdParams and CmdOpts
params, err := newCmdParams(
DirToMonitor("."),
Command("echo 'hello'"),
ExcludedDirs(map[string]bool{"test": true}),
RegisterWatcher(mockWatcher),
)

assert.NoError(t, err)
assert.Equal(t, ".", params.GetDirToMonitor())
assert.Equal(t, "echo 'hello'", params.GetCommand())
assert.Equal(t, map[string]bool{"test": true}, params.GetExcludedDirs())
assert.Equal(t, mockWatcher, params.GetWatcher())
}
Loading

0 comments on commit 1d4d609

Please sign in to comment.