https://github.com/agschwender/autoreload
Automatically reloads an executable on change
https://github.com/agschwender/autoreload
autoreload go
Last synced: 6 months ago
JSON representation
Automatically reloads an executable on change
- Host: GitHub
- URL: https://github.com/agschwender/autoreload
- Owner: agschwender
- License: unlicense
- Created: 2022-11-18T02:15:16.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2022-11-22T00:35:07.000Z (over 3 years ago)
- Last Synced: 2025-02-09T09:17:59.858Z (over 1 year ago)
- Topics: autoreload, go
- Language: Go
- Homepage:
- Size: 29.3 KB
- Stars: 1
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](http://godoc.org/github.com/agschwender/autoreload)
# autoreload
`autoreload` provides a package and command for automatically reloading an executable when that executable changes. It is intended to be used in a local development environment to reload the executable after it has been modified. An example use case would be to reload a go web app after you have edited the source code and recompiled the executable.
This approach can be useful when you prefer to manually rebuild your application instead of relying on functionality that watches your source files, recompiles the application and finally restarts it. In my experience, the manual approach to rebuilding allows for greater control of when it is triggered and provides more visibility on when it completes so that you can know that your changes are present when you retest. Additionally, if you run your local environment via Docker, e.g. something like [go-local](https://github.com/agschwender/go-local), this approach to reloading the application avoids having to install anything extra on your host machine.
## Installation
`autoreload` can be used as a package that is integrated into your application or as a command that is supplied an executable to monitor.
### Installation via Package
To integrate the package into your application, follow the example below.
```
package main
import (
"github.com/agschwender/autoreload"
)
func main() {
// Application setup
autoreload.New().Start()
// Application run and waiting
}
```
See the [provided example](https://github.com/agschwender/autoreload/blob/main/example/main.go) for greater detail on how to integrate the package into your application.
### Installation via Command
To integrate the command into your application, you must first install the `autoreload` command:
```
$ go install github.com/agschwender/autoreload/autoreloader@v1.1.2
```
Once installed, you can then execute the command by supplying it with the executable you want it to monitor and restart. For example if you wanted to run your server command, it may look like this:
```
$ autoreloader server --port=8080
```
## Demo
You can verify the behavior of the package or command installation by using the provided `example` command.
In one terminal, compile the commands
```
$ go install ./...
```
In another terminal, run
```
$ example
2022/11/18 10:06:43 Starting application
2022/11/18 10:06:43 Auto-reload is enabled
2022/11/18 10:06:43 Starting HTTP server
```
Change the `example/main.go` file and then re-install, using the first terminal
```
$ go install ./...
```
You should see the reload happen in your second terminal
```
2022/11/18 10:06:57 Executable changed; reloading process
2022/11/18 10:06:57 Received change event, shutting down
2022/11/18 10:06:58 Starting application
2022/11/18 10:06:58 Auto-reload is enabled
2022/11/18 10:06:58 Starting HTTP server 2
```
Similarly, you can run via the `autoreloader` with the `example` command's built-in reloading turned off.
In your second terminal, run
```
$ autoreloader example --autoreload=false
2022/11/18 10:10:11 Starting application
2022/11/18 10:10:11 Starting HTTP server
```
Again make a change to the `example/main.go` file and then re-install, using the first terminal
```
$ go install ./...
```
You should see the reload happen in your second terminal
```
2022/11/18 10:11:08 Executable changed; reloading process
2022/11/18 10:11:09 Killing process
2022/11/18 10:11:09 Starting application
2022/11/18 10:11:09 Starting HTTP server 2
```