2 Commits

Author SHA1 Message Date
Zoe
f52a61b9ea 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.
2025-10-03 02:00:22 -05:00
Zoe
adac21ce29 revert flattening (#3), add -C 2025-05-22 15:52:34 -05:00
4 changed files with 279 additions and 143 deletions

View File

@@ -14,44 +14,64 @@ go install github.com/juls0730/zqdgr@latest
## Usage ## Usage
Full usage Full usage
```Bash ```Bash
zqdgr [options] <command> zqdgr [options] <command>
``` ```
The list of commands is ### Commands
- `init` - `init`
generates a zqdgr.config.json file in the current directory generates a zqdgr.config.json file in the current directory
- `new [project name] [github repo]` - `new [project name] [github repo]`
Creates a new golang project with zqdgr and can optionally run scripts from a github repo Creates a new golang project with zqdgr and can optionally run scripts from a github repo
- `watch <script>` - `watch <script>`
runs the script in "watch mode", when files that follow the pattern in zqdgr.config.json change, the script restarts runs the script in "watch mode", when files that follow the pattern in zqdgr.config.json change, the script restarts
- `<script>` - `<script>`
runs the script runs the script
### Options
ZQDGR has the following list of options
- `-no-ws` - `-no-ws`
disables the web socket server running at 2067 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: Example usage:
```bash ```bash
zqdgr init zqdgr init
zqdgr watch dev zqdgr watch dev
zqdgr -config ./my-config.json watch dev
zqdgr -C ./my-nested-project watch dev
``` ```
### ZQDGR websocket ### ZQDGR websocket
ZQDGR comes with a websocket to notify listeners that the application has updates, the websocket is accessible at 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 `127.0.0.1:2067/ws`. An example dev script to listen for rebuilds might look like this
```Javascript ```Javascript
let host = window.location.hostname; let host = window.location.hostname;
const socket = new WebSocket('ws://' + host + ':2067/ws'); const socket = new WebSocket('ws://' + host + ':2067/ws');
socket.addEventListener('message', (event) => { socket.addEventListener('message', (event) => {
if (event.data === 'refresh') { if (event.data === 'refresh') {
@@ -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: 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 | | Key | Type | Required | default | Description |
| --- | --- | --- | | ---------------- | ------ | -------- | ------- | -------------------------------------------------------------------------------------------------------- |
| name | string | The name of the project | | name | string | - | - | The name of the project |
| version | string | The version of the project | | version | string | - | - | The version of the project |
| description | string | The description of the project | | description | string | - | - | The description of the project |
| author | string | The author of the project (probably you) | | author | string | - | - | The author of the project (probably you) |
| license | string | The license of the project | | license | string | - | - | The license of the project |
| homepage | string | The URL to the homepage of the project | | homepage | string | - | - | The URL to the homepage of the project |
| repository | object | The repository of the project | | repository | object | - | - | The repository of the project |
| repository.type | string | The type of VCS that you use, most likely git | | 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 | | 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 | | scripts | object | true | - | 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 | | pattern | string | true | - | The GLOB pattern that ZQDGR will watch for changes |
| excluded_dirs | array | The directories that ZQDGR will ignore when in the `watch` mode | | excluded_globs | array | - | - | Globs 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` | | 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. The only required key is `scripts`, the rest are optional, but we recommend you set the most important ones.

296
main.go
View File

@@ -2,6 +2,7 @@ package main
import ( import (
"bufio" "bufio"
"bytes"
_ "embed" _ "embed"
"encoding/json" "encoding/json"
"flag" "flag"
@@ -13,9 +14,7 @@ import (
"os/signal" "os/signal"
"path" "path"
"path/filepath" "path/filepath"
"regexp"
"runtime" "runtime"
"sort"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@@ -25,8 +24,6 @@ import (
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
) )
var executableName string
//go:embed embed/zqdgr.config.json //go:embed embed/zqdgr.config.json
var zqdgrConfig []byte var zqdgrConfig []byte
@@ -41,13 +38,17 @@ type Config struct {
Type string `json:"type"` Type string `json:"type"`
URL string `json:"url"` URL string `json:"url"`
} `json:"repository"` } `json:"repository"`
Scripts map[string]string `json:"scripts"` Scripts map[string]string `json:"scripts"`
Pattern string `json:"pattern"` Pattern string `json:"pattern"`
ExcludedDirs []string `json:"excluded_dirs"` // Deprecated: use excludedGlobs instead
ShutdownSignal string `json:"shutdown_signal"` ExcludedDirs []string `json:"excluded_dirs"`
ExcludedGlobs []string `json:"excluded_files"`
ShutdownSignal string `json:"shutdown_signal"`
ShutdownTimeout int `json:"shutdown_timeout"`
} }
type Script struct { type Script struct {
zqdgr *ZQDGR
command *exec.Cmd command *exec.Cmd
mutex sync.Mutex mutex sync.Mutex
scriptName string scriptName string
@@ -57,59 +58,47 @@ type Script struct {
exitCode int exitCode int
} }
func flattenZQDGRScript(commandString string) string { type ZQDGR struct {
keys := make([]string, 0, len(config.Scripts)) Config Config
for k := range config.Scripts { WorkingDirectory string
keys = append(keys, k) EnableWebSocket bool
} WSServer *WSServer
// Sort the keys in descending order in order to prevent scripts that might be substrings of other scripts to
// evaluate first.
sort.Slice(keys, func(i, j int) bool {
return len(keys[i]) > len(keys[j])
})
// escape scripts to be evaluated via regex
escapedKeys := make([]string, len(keys))
for i, key := range keys {
escapedKeys[i] = regexp.QuoteMeta(key)
}
pattern := `\b(` + executableName + `)\b` + `\s+` + `\b(` + strings.Join(escapedKeys, "|") + `)\b`
re := regexp.MustCompile(pattern)
currentCommand := commandString
for {
previousCommand := currentCommand
currentCommand = re.ReplaceAllStringFunc(currentCommand, func(match string) string {
// match the script name, not the whole `zqdgr script` command
match = strings.Split(match, " ")[1]
if val, ok := config.Scripts[match]; ok {
return val
}
return match
})
// If the current command has not changed, we have completely evaluated the command.
if currentCommand == previousCommand {
break
}
}
if re.MatchString(currentCommand) {
fmt.Println("Error: circular dependency detected in scripts")
os.Exit(1)
}
return currentCommand
} }
func NewCommand(scriptName string, args ...string) *exec.Cmd { type WSServer struct {
if script, ok := config.Scripts[scriptName]; ok { upgrader websocket.Upgrader
fullCmd := strings.Join(append([]string{script}, args...), " ") clients map[*websocket.Conn]bool
clientsMux sync.Mutex
}
fullCmd = flattenZQDGRScript(fullCmd) func NewZQDGR(enableWebSocket bool, configDir string) *ZQDGR {
zqdgr := &ZQDGR{
WorkingDirectory: configDir,
}
err := zqdgr.loadConfig()
if err != nil {
log.Fatal(err)
return nil
}
zqdgr.EnableWebSocket = enableWebSocket
zqdgr.WSServer = &WSServer{
upgrader: websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
},
clients: make(map[*websocket.Conn]bool),
clientsMux: sync.Mutex{},
}
return zqdgr
}
func (zqdgr *ZQDGR) NewCommand(scriptName string, args ...string) *exec.Cmd {
if script, ok := zqdgr.Config.Scripts[scriptName]; ok {
fullCmd := strings.Join(append([]string{script}, args...), " ")
var cmd *exec.Cmd var cmd *exec.Cmd
if runtime.GOOS == "windows" { if runtime.GOOS == "windows" {
@@ -118,6 +107,8 @@ func NewCommand(scriptName string, args ...string) *exec.Cmd {
cmd = exec.Command("sh", "-c", fullCmd) cmd = exec.Command("sh", "-c", fullCmd)
} }
cmd.Dir = zqdgr.WorkingDirectory
cmd.SysProcAttr = &syscall.SysProcAttr{ cmd.SysProcAttr = &syscall.SysProcAttr{
Setpgid: true, Setpgid: true,
} }
@@ -132,8 +123,8 @@ func NewCommand(scriptName string, args ...string) *exec.Cmd {
} }
} }
func NewScript(scriptName string, args ...string) *Script { func (zqdgr *ZQDGR) NewScript(scriptName string, args ...string) *Script {
command := NewCommand(scriptName, args...) command := zqdgr.NewCommand(scriptName, args...)
if command == nil { if command == nil {
log.Fatal("script not found") log.Fatal("script not found")
@@ -141,6 +132,7 @@ func NewScript(scriptName string, args ...string) *Script {
} }
return &Script{ return &Script{
zqdgr: zqdgr,
command: command, command: command,
scriptName: scriptName, scriptName: scriptName,
isRestarting: false, isRestarting: false,
@@ -166,8 +158,10 @@ func (s *Script) Start() error {
s.exitCode = exitError.ExitCode() s.exitCode = exitError.ExitCode()
} else { } else {
// Other errors (e.g., process not found, permission denied) // Other errors (e.g., process not found, permission denied)
log.Printf("Error waiting for script %s: %v", s.scriptName, err) if !s.isRestarting {
s.exitCode = 1 log.Printf("Error waiting for script %s: %v", s.scriptName, err)
s.exitCode = 1
}
} }
} }
@@ -179,14 +173,17 @@ func (s *Script) Start() error {
return err return err
} }
func (s *Script) Restart() error { func (s *Script) Stop(lock bool) error {
s.mutex.Lock() if lock {
s.mutex.Lock()
defer s.mutex.Unlock()
}
s.isRestarting = true s.isRestarting = true
if s.command.Process != nil { if s.command.Process != nil {
var signal syscall.Signal var signal syscall.Signal
switch config.ShutdownSignal { switch s.zqdgr.Config.ShutdownSignal {
case "SIGINT": case "SIGINT":
signal = syscall.SIGINT signal = syscall.SIGINT
case "SIGTERM": case "SIGTERM":
@@ -199,10 +196,45 @@ func (s *Script) Restart() error {
if err := syscall.Kill(-s.command.Process.Pid, signal); err != nil { if err := syscall.Kill(-s.command.Process.Pid, signal); err != nil {
log.Printf("error killing previous process: %v", err) log.Printf("error killing previous process: %v", err)
return err
} }
} }
s.command = NewCommand(s.scriptName) 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 { if s.command == nil {
// this should never happen // this should never happen
@@ -214,20 +246,20 @@ func (s *Script) Restart() error {
s.mutex.Unlock() s.mutex.Unlock()
err := s.Start() err = s.Start()
// tell the websocket clients to refresh // tell the websocket clients to refresh
if enableWebSocket { if s.zqdgr.EnableWebSocket {
clientsMux.Lock() s.zqdgr.WSServer.clientsMux.Lock()
for client := range clients { for client := range s.zqdgr.WSServer.clients {
err := client.WriteMessage(websocket.TextMessage, []byte("refresh")) err := client.WriteMessage(websocket.TextMessage, []byte("refresh"))
if err != nil { if err != nil {
log.Printf("error broadcasting refresh: %v", err) log.Printf("error broadcasting refresh: %v", err)
client.Close() client.Close()
delete(clients, client) delete(s.zqdgr.WSServer.clients, client)
} }
} }
clientsMux.Unlock() s.zqdgr.WSServer.clientsMux.Unlock()
} }
return err return err
@@ -237,49 +269,39 @@ func (s *Script) Wait() {
s.wg.Wait() s.wg.Wait()
} }
func handleWs(w http.ResponseWriter, r *http.Request) { func (wsServer *WSServer) handleWs(w http.ResponseWriter, r *http.Request) {
conn, err := upgrader.Upgrade(w, r, nil) conn, err := wsServer.upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Printf("error upgrading connection: %v", err) log.Printf("error upgrading connection: %v", err)
return return
} }
clientsMux.Lock() wsServer.clientsMux.Lock()
clients[conn] = true wsServer.clients[conn] = true
clientsMux.Unlock() wsServer.clientsMux.Unlock()
for { for {
_, _, err := conn.ReadMessage() _, _, err := conn.ReadMessage()
if err != nil { if err != nil {
clientsMux.Lock() wsServer.clientsMux.Lock()
delete(clients, conn) delete(wsServer.clients, conn)
clientsMux.Unlock() wsServer.clientsMux.Unlock()
break break
} }
} }
} }
var ( func (zqdgr *ZQDGR) loadConfig() error {
enableWebSocket = false data, err := os.ReadFile(path.Join(zqdgr.WorkingDirectory, "zqdgr.config.json"))
config Config
script *Script
upgrader = websocket.Upgrader{
CheckOrigin: func(r *http.Request) bool {
return true
},
}
clients = make(map[*websocket.Conn]bool)
clientsMux sync.Mutex
)
func loadConfig() error {
data, err := os.ReadFile("zqdgr.config.json")
if err == nil { if err == nil {
if err := json.Unmarshal(data, &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) return fmt.Errorf("error parsing config file: %v", err)
} }
} else { } else {
config = Config{ zqdgr.Config = Config{
Scripts: map[string]string{ Scripts: map[string]string{
"build": "go build", "build": "go build",
"run": "go run main.go", "run": "go run main.go",
@@ -288,27 +310,39 @@ func 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 return nil
} }
func main() { func main() {
noWs := flag.Bool("no-ws", false, "Disable WebSocket server") 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() flag.Parse()
if err := loadConfig(); err != nil { originalArgs := os.Args
log.Fatal(err) os.Args = flag.Args()
zqdgr := NewZQDGR(*noWs, *configDir)
if zqdgr == nil {
return
} }
var command string var command string
var commandArgs []string var commandArgs []string
// get the name of the executable, and if it's a path then get the base name for i, arg := range os.Args {
// this is mainly for testing
executableName = path.Base(os.Args[0])
for i, arg := range os.Args[1:] {
if arg == "--" { if arg == "--" {
commandArgs = os.Args[i+2:] if i+2 < len(os.Args) {
commandArgs = os.Args[i+2:]
}
break break
} }
@@ -462,7 +496,7 @@ func main() {
log.Fatal("please specify a script to run") log.Fatal("please specify a script to run")
} }
watchMode = true watchMode = true
for i := 0; i < len(commandArgs); i++ { for i := range commandArgs {
if strings.HasPrefix(commandArgs[i], "-") { if strings.HasPrefix(commandArgs[i], "-") {
continue continue
} }
@@ -473,7 +507,7 @@ func main() {
scriptName = command scriptName = command
} }
script = NewScript(scriptName, commandArgs...) script := zqdgr.NewScript(scriptName, commandArgs...)
if err := script.Start(); err != nil { if err := script.Start(); err != nil {
log.Fatal(err) log.Fatal(err)
@@ -487,7 +521,7 @@ func main() {
log.Println("Received signal, exiting...") log.Println("Received signal, exiting...")
if script.command != nil { if script.command != nil {
var signal syscall.Signal var signal syscall.Signal
switch config.ShutdownSignal { switch zqdgr.Config.ShutdownSignal {
case "SIGINT": case "SIGINT":
signal = syscall.SIGINT signal = syscall.SIGINT
case "SIGTERM": case "SIGTERM":
@@ -506,10 +540,10 @@ func main() {
if watchMode { if watchMode {
if !*noWs { if !*noWs {
enableWebSocket = true zqdgr.EnableWebSocket = true
go func() { go func() {
http.HandleFunc("/ws", handleWs) http.HandleFunc("/ws", zqdgr.WSServer.handleWs)
log.Printf("WebSocket server running on :2067") log.Printf("WebSocket server running on :2067")
if err := http.ListenAndServe(":2067", nil); err != nil { if err := http.ListenAndServe(":2067", nil); err != nil {
log.Printf("WebSocket server error: %v", err) log.Printf("WebSocket server error: %v", err)
@@ -517,15 +551,16 @@ func main() {
}() }()
} }
if config.Pattern == "" { if zqdgr.Config.Pattern == "" {
log.Fatal("watch pattern not specified in config") log.Fatal("watch pattern not specified in config")
} }
// make sure the pattern is valid
var paternArray []string var paternArray []string
var currentPattern string var currentPattern string
inMatch := false inMatch := false
// iterate over every letter in the pattern // iterate over every letter in the pattern
for _, p := range config.Pattern { for _, p := range zqdgr.Config.Pattern {
if string(p) == "{" { if string(p) == "{" {
if inMatch { if inMatch {
log.Fatal("unmatched { in pattern") log.Fatal("unmatched { in pattern")
@@ -561,8 +596,8 @@ func main() {
} }
watcherConfig := WatcherConfig{ watcherConfig := WatcherConfig{
excludedDirs: globList(config.ExcludedDirs), excludedGlobs: globList(zqdgr.Config.ExcludedGlobs),
pattern: paternArray, pattern: paternArray,
} }
watcher, err := NewWatcher(&watcherConfig) watcher, err := NewWatcher(&watcherConfig)
@@ -577,6 +612,11 @@ func main() {
log.Fatal(err) 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. // We use this timer to deduplicate events.
var ( var (
// Wait 100ms for new events; each new event resets the timer. // Wait 100ms for new events; each new event resets the timer.
@@ -601,7 +641,33 @@ func main() {
if !ok { if !ok {
timer = time.AfterFunc(waitFor, func() { 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 event.Op&fsnotify.Remove == fsnotify.Remove || event.Op&fsnotify.Write == fsnotify.Write || event.Op&fsnotify.Create == fsnotify.Create {
fmt.Println("File changed:", event.Name) 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) { if directoryShouldBeTracked(&watcherConfig, event.Name) {
watcher.(NotifyWatcher).watcher.Add(event.Name) watcher.(NotifyWatcher).watcher.Add(event.Name)
} }

View File

@@ -4,7 +4,9 @@ import (
"errors" "errors"
"fmt" "fmt"
"log" "log"
"os"
"path/filepath" "path/filepath"
"strings"
"github.com/bmatcuk/doublestar" "github.com/bmatcuk/doublestar"
"github.com/fsnotify/fsnotify" "github.com/fsnotify/fsnotify"
@@ -14,12 +16,19 @@ type globList []string
func (g *globList) Matches(value string) bool { func (g *globList) Matches(value string) bool {
for _, v := range *g { for _, v := range *g {
// if the pattern matches a filepath pattern
if match, err := filepath.Match(v, value); err != nil { if match, err := filepath.Match(v, value); err != nil {
log.Fatalf("Bad pattern \"%s\": %s", v, err.Error()) log.Fatalf("Bad pattern \"%s\": %s", v, err.Error())
} else if match { } else if match {
return true return true
} }
// or if the path starts with the pattern
if strings.HasSuffix(value, v) {
return true
}
} }
return false return false
} }
@@ -29,12 +38,25 @@ func matchesPattern(pattern []string, path string) bool {
return true return true
} }
} }
return false return false
} }
func directoryShouldBeTracked(cfg *WatcherConfig, path string) bool { func directoryShouldBeTracked(cfg *WatcherConfig, path string) bool {
base := filepath.Dir(path) 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 { func pathMatches(cfg *WatcherConfig, path string) bool {
@@ -42,13 +64,14 @@ func pathMatches(cfg *WatcherConfig, path string) bool {
} }
type WatcherConfig struct { type WatcherConfig struct {
excludedDirs globList excludedGlobs globList
pattern globList pattern globList
} }
type FileWatcher interface { type FileWatcher interface {
Close() error Close() error
AddFiles() error AddFiles() error
AddFile(path string) error
add(path string) error add(path string) error
getConfig() *WatcherConfig getConfig() *WatcherConfig
} }
@@ -62,6 +85,14 @@ func (n NotifyWatcher) Close() error {
return n.watcher.Close() 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 { func (n NotifyWatcher) AddFiles() error {
return addFiles(n) return addFiles(n)
} }
@@ -79,10 +110,12 @@ func NewWatcher(cfg *WatcherConfig) (FileWatcher, error) {
err := errors.New("no config specified") err := errors.New("no config specified")
return nil, err return nil, err
} }
w, err := fsnotify.NewWatcher() w, err := fsnotify.NewWatcher()
if err != nil { if err != nil {
return nil, err return nil, err
} }
return NotifyWatcher{ return NotifyWatcher{
watcher: w, watcher: w,
cfg: cfg, cfg: cfg,
@@ -92,18 +125,31 @@ func NewWatcher(cfg *WatcherConfig) (FileWatcher, error) {
func addFiles(fw FileWatcher) error { func addFiles(fw FileWatcher) error {
cfg := fw.getConfig() cfg := fw.getConfig()
for _, pattern := range cfg.pattern { for _, pattern := range cfg.pattern {
if os.Getenv("ZQDGR_DEBUG") != "" {
fmt.Printf("processing glob %s\n", pattern)
}
matches, err := doublestar.Glob(pattern) matches, err := doublestar.Glob(pattern)
if err != nil { if err != nil {
log.Fatalf("Bad pattern \"%s\": %s", pattern, err.Error()) log.Fatalf("Bad pattern \"%s\": %s", pattern, err.Error())
} }
for _, match := range matches { for _, match := range matches {
if os.Getenv("ZQDGR_DEBUG") != "" {
log.Printf("checking %s\n", match)
}
if directoryShouldBeTracked(cfg, 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 { if err := fw.add(match); err != nil {
return fmt.Errorf("FileWatcher.Add(): %v", err) return fmt.Errorf("FileWatcher.Add(): %v", err)
} }
} }
} }
} }
return nil return nil
} }

View File

@@ -1,6 +1,6 @@
{ {
"name": "zqdgr", "name": "zqdgr",
"version": "0.0.3", "version": "0.0.4",
"description": "zqdgr is a quick and dirty Golang runner", "description": "zqdgr is a quick and dirty Golang runner",
"author": "juls0730", "author": "juls0730",
"license": "BSL-1.0", "license": "BSL-1.0",
@@ -14,9 +14,12 @@
"dev": "sleep 5; echo 'test' && sleep 2 && echo 'test2'", "dev": "sleep 5; echo 'test' && sleep 2 && echo 'test2'",
"test": "zqdgr test:1 && zqdgr test:2 && zqdgr test:3 && zqdgr test:4", "test": "zqdgr test:1 && zqdgr test:2 && zqdgr test:3 && zqdgr test:4",
"test:1": "echo 'a'", "test:1": "echo 'a'",
"test:2": "false", "test:2": "true",
"test:3": "echo 'b'", "test:3": "echo 'b'",
"test:4": "zqdgr test:3", "test:4": "zqdgr test:3",
"test:5": "zqdgr test:6",
"test:6": "zqdgr test:7",
"test:7": "zqdgr test:5",
"recursive": "zqdgr recursive" "recursive": "zqdgr recursive"
}, },
"pattern": "**/*.go" "pattern": "**/*.go"