ZQDGR v0.0.4: Qualirt of Life Update
This update brings several quality of life enhancements: - You can now use excluded_files to exclude files from the watcher - ZQDGR reloads itself when its config changes - ZQDGR will not start if there are unrecognized keys in the config It also fixes a bug where if a program was sent a kill signal, but didnt die, ZQDGR would carry on as if it did, which lead to many issues.
This commit is contained in:
55
README.md
55
README.md
@@ -14,11 +14,13 @@ go install github.com/juls0730/zqdgr@latest
|
||||
## Usage
|
||||
|
||||
Full usage
|
||||
|
||||
```Bash
|
||||
zqdgr [options] <command>
|
||||
```
|
||||
|
||||
The list of commands is
|
||||
### Commands
|
||||
|
||||
- `init`
|
||||
|
||||
generates a zqdgr.config.json file in the current directory
|
||||
@@ -30,25 +32,43 @@ The list of commands is
|
||||
- `watch <script>`
|
||||
|
||||
runs the script in "watch mode", when files that follow the pattern in zqdgr.config.json change, the script restarts
|
||||
|
||||
- `<script>`
|
||||
|
||||
runs the script
|
||||
|
||||
### Options
|
||||
|
||||
ZQDGR has the following list of options
|
||||
- `-no-ws`
|
||||
|
||||
disables the web socket server running at 2067
|
||||
|
||||
- `-config`
|
||||
|
||||
specifies the path to the config file
|
||||
|
||||
- `-disable-reload-config`
|
||||
|
||||
disables reloading the config file when it changes
|
||||
|
||||
- `-C`
|
||||
|
||||
specifies the path to use as the working directry
|
||||
|
||||
Example usage:
|
||||
|
||||
```bash
|
||||
zqdgr init
|
||||
zqdgr watch dev
|
||||
zqdgr -config ./my-config.json watch dev
|
||||
zqdgr -C ./my-nested-project watch dev
|
||||
```
|
||||
|
||||
### ZQDGR websocket
|
||||
|
||||
ZQDGR comes with a websocket to notify listeners that the application has updates, the websocket is accessible at
|
||||
`127.0.0.1:2067/ws`. An example dev script to listen for rebuilds might look like this
|
||||
|
||||
```Javascript
|
||||
let host = window.location.hostname;
|
||||
const socket = new WebSocket('ws://' + host + ':2067/ws');
|
||||
@@ -75,21 +95,22 @@ socket.addEventListener('message', (event) => {
|
||||
|
||||
ZQDGR is solely configured by a `zqdgr.config.json` file in the root of your project. The file has the following keys:
|
||||
|
||||
| Key | Type | Description |
|
||||
| --- | --- | --- |
|
||||
| name | string | The name of the project |
|
||||
| version | string | The version of the project |
|
||||
| description | string | The description of the project |
|
||||
| author | string | The author of the project (probably you) |
|
||||
| license | string | The license of the project |
|
||||
| homepage | string | The URL to the homepage of the project |
|
||||
| repository | object | The repository of the project |
|
||||
| repository.type | string | The type of VCS that you use, most likely git |
|
||||
| repository.url | string | The place where you code is hosted. This should be a URI that can be used as is in a program such as git |
|
||||
| scripts | object | An object that maps a script name to a script command, which is just a shell command |
|
||||
| pattern | string | The GLOB pattern that ZQDGR will watch for changes |
|
||||
| excluded_dirs | array | The directories that ZQDGR will ignore when in the `watch` mode |
|
||||
| shutdown_signal | string | The signal that ZQDGR will use to shutdown the script. Currently the only supported signals are `SIGINT`, `SIGTERM`, and `SIGQUIT`, if no shutdown signal is specified, ZQDGR will default to `SIGKILL` |
|
||||
| Key | Type | Required | default | Description |
|
||||
| ---------------- | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------- |
|
||||
| name | string | - | - | The name of the project |
|
||||
| version | string | - | - | The version of the project |
|
||||
| description | string | - | - | The description of the project |
|
||||
| author | string | - | - | The author of the project (probably you) |
|
||||
| license | string | - | - | The license of the project |
|
||||
| homepage | string | - | - | The URL to the homepage of the project |
|
||||
| repository | object | - | - | The repository of the project |
|
||||
| repository.type | string | - | - | The type of VCS that you use, most likely git |
|
||||
| repository.url | string | - | - | The place where you code is hosted. This should be a URI that can be used as is in a program such as git |
|
||||
| scripts | object | true | - | An object that maps a script name to a script command, which is just a shell command |
|
||||
| pattern | string | true | - | The GLOB pattern that ZQDGR will watch for changes |
|
||||
| excluded_globs | array | - | - | Globs that ZQDGR will ignore when in the `watch` mode |
|
||||
| shutdown_signal | string | - | SIGINT | The signal that ZQDGR will use to shutdown the script. One of SIGKILL, SIGINT, SIGQUIT, SIGTERM |
|
||||
| shutdown_timeout | int | - | 1 | The amount of time in seconds that ZQDGR will wait for the script to exit before force killing it. |
|
||||
|
||||
The only required key is `scripts`, the rest are optional, but we recommend you set the most important ones.
|
||||
|
||||
|
||||
104
main.go
104
main.go
@@ -2,6 +2,7 @@ package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"bytes"
|
||||
_ "embed"
|
||||
"encoding/json"
|
||||
"flag"
|
||||
@@ -39,8 +40,11 @@ type Config struct {
|
||||
} `json:"repository"`
|
||||
Scripts map[string]string `json:"scripts"`
|
||||
Pattern string `json:"pattern"`
|
||||
// Deprecated: use excludedGlobs instead
|
||||
ExcludedDirs []string `json:"excluded_dirs"`
|
||||
ExcludedGlobs []string `json:"excluded_files"`
|
||||
ShutdownSignal string `json:"shutdown_signal"`
|
||||
ShutdownTimeout int `json:"shutdown_timeout"`
|
||||
}
|
||||
|
||||
type Script struct {
|
||||
@@ -72,7 +76,11 @@ func NewZQDGR(enableWebSocket bool, configDir string) *ZQDGR {
|
||||
WorkingDirectory: configDir,
|
||||
}
|
||||
|
||||
zqdgr.loadConfig()
|
||||
err := zqdgr.loadConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
return nil
|
||||
}
|
||||
|
||||
zqdgr.EnableWebSocket = enableWebSocket
|
||||
zqdgr.WSServer = &WSServer{
|
||||
@@ -150,10 +158,12 @@ func (s *Script) Start() error {
|
||||
s.exitCode = exitError.ExitCode()
|
||||
} else {
|
||||
// Other errors (e.g., process not found, permission denied)
|
||||
if !s.isRestarting {
|
||||
log.Printf("Error waiting for script %s: %v", s.scriptName, err)
|
||||
s.exitCode = 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !s.isRestarting {
|
||||
s.wg.Done()
|
||||
@@ -163,8 +173,11 @@ func (s *Script) Start() error {
|
||||
return err
|
||||
}
|
||||
|
||||
func (s *Script) Restart() error {
|
||||
func (s *Script) Stop(lock bool) error {
|
||||
if lock {
|
||||
s.mutex.Lock()
|
||||
defer s.mutex.Unlock()
|
||||
}
|
||||
|
||||
s.isRestarting = true
|
||||
|
||||
@@ -183,9 +196,44 @@ func (s *Script) Restart() error {
|
||||
|
||||
if err := syscall.Kill(-s.command.Process.Pid, signal); err != nil {
|
||||
log.Printf("error killing previous process: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
dead := make(chan bool)
|
||||
go func() {
|
||||
s.command.Wait()
|
||||
dead <- true
|
||||
}()
|
||||
|
||||
shutdownTimeout := s.zqdgr.Config.ShutdownTimeout
|
||||
if shutdownTimeout == 0 {
|
||||
shutdownTimeout = 1
|
||||
}
|
||||
|
||||
select {
|
||||
case <-dead:
|
||||
case <-time.After(time.Duration(shutdownTimeout) * time.Second):
|
||||
log.Println("Script failed to stop after kill signal, force killing")
|
||||
|
||||
if err := syscall.Kill(-s.command.Process.Pid, syscall.SIGKILL); err != nil {
|
||||
log.Printf("error killing previous process: %v", err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (s *Script) Restart() error {
|
||||
s.mutex.Lock()
|
||||
|
||||
err := s.Stop(false)
|
||||
if err != nil {
|
||||
s.mutex.Unlock()
|
||||
return err
|
||||
}
|
||||
|
||||
s.command = s.zqdgr.NewCommand(s.scriptName)
|
||||
|
||||
if s.command == nil {
|
||||
@@ -198,7 +246,7 @@ func (s *Script) Restart() error {
|
||||
|
||||
s.mutex.Unlock()
|
||||
|
||||
err := s.Start()
|
||||
err = s.Start()
|
||||
|
||||
// tell the websocket clients to refresh
|
||||
if s.zqdgr.EnableWebSocket {
|
||||
@@ -246,7 +294,10 @@ func (wsServer *WSServer) handleWs(w http.ResponseWriter, r *http.Request) {
|
||||
func (zqdgr *ZQDGR) loadConfig() error {
|
||||
data, err := os.ReadFile(path.Join(zqdgr.WorkingDirectory, "zqdgr.config.json"))
|
||||
if err == nil {
|
||||
if err := json.Unmarshal(data, &zqdgr.Config); err != nil {
|
||||
decoder := json.NewDecoder(bytes.NewReader(data))
|
||||
decoder.DisallowUnknownFields()
|
||||
err := decoder.Decode(&zqdgr.Config)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error parsing config file: %v", err)
|
||||
}
|
||||
} else {
|
||||
@@ -259,19 +310,30 @@ func (zqdgr *ZQDGR) loadConfig() error {
|
||||
}
|
||||
}
|
||||
|
||||
if zqdgr.Config.ExcludedDirs != nil {
|
||||
fmt.Printf("WARNING: the 'excluded_dirs' key is deprecated, please use 'excluded_globs' instead\n")
|
||||
|
||||
zqdgr.Config.ExcludedGlobs = append(zqdgr.Config.ExcludedGlobs, zqdgr.Config.ExcludedDirs...)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
noWs := flag.Bool("no-ws", false, "Disable WebSocket server")
|
||||
configDir := flag.String("config", ".", "Path to the config directory")
|
||||
disableReloadConfig := flag.Bool("no-reload-config", false, "Do not restart ZQDGR on config file change")
|
||||
flag.StringVar(configDir, "C", *configDir, "Path to the config directory")
|
||||
|
||||
flag.Parse()
|
||||
|
||||
originalArgs := os.Args
|
||||
os.Args = flag.Args()
|
||||
|
||||
zqdgr := NewZQDGR(*noWs, *configDir)
|
||||
if zqdgr == nil {
|
||||
return
|
||||
}
|
||||
|
||||
var command string
|
||||
var commandArgs []string
|
||||
@@ -493,6 +555,7 @@ func main() {
|
||||
log.Fatal("watch pattern not specified in config")
|
||||
}
|
||||
|
||||
// make sure the pattern is valid
|
||||
var paternArray []string
|
||||
var currentPattern string
|
||||
inMatch := false
|
||||
@@ -533,7 +596,7 @@ func main() {
|
||||
}
|
||||
|
||||
watcherConfig := WatcherConfig{
|
||||
excludedDirs: globList(zqdgr.Config.ExcludedDirs),
|
||||
excludedGlobs: globList(zqdgr.Config.ExcludedGlobs),
|
||||
pattern: paternArray,
|
||||
}
|
||||
|
||||
@@ -549,6 +612,11 @@ func main() {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = watcher.AddFile(path.Join(zqdgr.WorkingDirectory, "zqdgr.config.json"))
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// We use this timer to deduplicate events.
|
||||
var (
|
||||
// Wait 100ms for new events; each new event resets the timer.
|
||||
@@ -573,7 +641,33 @@ func main() {
|
||||
if !ok {
|
||||
timer = time.AfterFunc(waitFor, func() {
|
||||
if event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
fmt.Println("File changed:", event.Name)
|
||||
}
|
||||
|
||||
if strings.HasSuffix(event.Name, "zqdgr.config.json") {
|
||||
// re-exec the exact same command
|
||||
if !*disableReloadConfig {
|
||||
log.Println("zqdgr.config.json has changed, restarting...")
|
||||
executable, err := os.Executable()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = script.Stop(true)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
err = syscall.Exec(executable, originalArgs, os.Environ())
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
panic("unreachable")
|
||||
}
|
||||
}
|
||||
|
||||
if directoryShouldBeTracked(&watcherConfig, event.Name) {
|
||||
watcher.(NotifyWatcher).watcher.Add(event.Name)
|
||||
}
|
||||
|
||||
52
watcher.go
52
watcher.go
@@ -4,7 +4,9 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"github.com/bmatcuk/doublestar"
|
||||
"github.com/fsnotify/fsnotify"
|
||||
@@ -14,12 +16,19 @@ type globList []string
|
||||
|
||||
func (g *globList) Matches(value string) bool {
|
||||
for _, v := range *g {
|
||||
// if the pattern matches a filepath pattern
|
||||
if match, err := filepath.Match(v, value); err != nil {
|
||||
log.Fatalf("Bad pattern \"%s\": %s", v, err.Error())
|
||||
} else if match {
|
||||
return true
|
||||
}
|
||||
|
||||
// or if the path starts with the pattern
|
||||
if strings.HasSuffix(value, v) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -29,12 +38,25 @@ func matchesPattern(pattern []string, path string) bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
func directoryShouldBeTracked(cfg *WatcherConfig, path string) bool {
|
||||
base := filepath.Dir(path)
|
||||
return matchesPattern(cfg.pattern, path) && !cfg.excludedDirs.Matches(base)
|
||||
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("checking %s against %s %v\n", path, base, *cfg)
|
||||
}
|
||||
|
||||
if cfg.excludedGlobs.Matches(base) {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("%s is excluded\n", base)
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
return matchesPattern(cfg.pattern, path)
|
||||
}
|
||||
|
||||
func pathMatches(cfg *WatcherConfig, path string) bool {
|
||||
@@ -42,13 +64,14 @@ func pathMatches(cfg *WatcherConfig, path string) bool {
|
||||
}
|
||||
|
||||
type WatcherConfig struct {
|
||||
excludedDirs globList
|
||||
excludedGlobs globList
|
||||
pattern globList
|
||||
}
|
||||
|
||||
type FileWatcher interface {
|
||||
Close() error
|
||||
AddFiles() error
|
||||
AddFile(path string) error
|
||||
add(path string) error
|
||||
getConfig() *WatcherConfig
|
||||
}
|
||||
@@ -62,6 +85,14 @@ func (n NotifyWatcher) Close() error {
|
||||
return n.watcher.Close()
|
||||
}
|
||||
|
||||
func (n NotifyWatcher) AddFile(path string) error {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("manually adding file\n")
|
||||
}
|
||||
|
||||
return n.add(path)
|
||||
}
|
||||
|
||||
func (n NotifyWatcher) AddFiles() error {
|
||||
return addFiles(n)
|
||||
}
|
||||
@@ -79,10 +110,12 @@ func NewWatcher(cfg *WatcherConfig) (FileWatcher, error) {
|
||||
err := errors.New("no config specified")
|
||||
return nil, err
|
||||
}
|
||||
|
||||
w, err := fsnotify.NewWatcher()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return NotifyWatcher{
|
||||
watcher: w,
|
||||
cfg: cfg,
|
||||
@@ -92,18 +125,31 @@ func NewWatcher(cfg *WatcherConfig) (FileWatcher, error) {
|
||||
func addFiles(fw FileWatcher) error {
|
||||
cfg := fw.getConfig()
|
||||
for _, pattern := range cfg.pattern {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
fmt.Printf("processing glob %s\n", pattern)
|
||||
}
|
||||
|
||||
matches, err := doublestar.Glob(pattern)
|
||||
if err != nil {
|
||||
log.Fatalf("Bad pattern \"%s\": %s", pattern, err.Error())
|
||||
}
|
||||
|
||||
for _, match := range matches {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("checking %s\n", match)
|
||||
}
|
||||
|
||||
if directoryShouldBeTracked(cfg, match) {
|
||||
if os.Getenv("ZQDGR_DEBUG") != "" {
|
||||
log.Printf("%s is not excluded\n", match)
|
||||
}
|
||||
|
||||
if err := fw.add(match); err != nil {
|
||||
return fmt.Errorf("FileWatcher.Add(): %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "zqdgr",
|
||||
"version": "0.0.3",
|
||||
"version": "0.0.4",
|
||||
"description": "zqdgr is a quick and dirty Golang runner",
|
||||
"author": "juls0730",
|
||||
"license": "BSL-1.0",
|
||||
|
||||
Reference in New Issue
Block a user