Skip to content

Commit 1679f38

Browse files
committed
Support absolute paths
1 parent cda0c55 commit 1679f38

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

README.md

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,53 @@ CLI wrapper for [fsnotify](https://github.com/fsnotify/fsnotify)
55
## Quick start
66

77
Install
8-
```sh
9-
go get github.com/mozey/watcher
10-
```
11-
12-
Update
13-
```sh
14-
go get -u github.com/mozey/watcher
15-
go install github.com/mozey/watcher
8+
```bash
9+
# Since Go 1.20.3
10+
# "'go get' is no longer supported outside a module"
11+
go install github.com/mozey/watcher@latest
1612
```
1713

1814
Print version
19-
```sh
15+
```bash
2016
$GOPATH/bin/watcher -version
2117
```
2218

2319
Watch files, only output changes
24-
```sh
20+
```bash
2521
$GOPATH/bin/watcher -dir testdata
2622
```
2723

28-
Watch files recursively, pass in `APP_DIR`
29-
```sh
30-
APP_DIR=$(pwd) $GOPATH/bin/watcher -r -dir testdata
24+
Watch files recursively, and print debug logs
25+
```bash
26+
APP_DEBUG=true $GOPATH/bin/watcher -r -dir testdata
3127
```
3228

33-
Watch files recursively, and print debug logs
34-
```sh
35-
APP_DEBUG=true APP_DIR=$(pwd) $GOPATH/bin/watcher -r -dir testdata
29+
30+
## Testing
31+
32+
Recursively watch for change in both `./testdata` and `./testdata2`
33+
```bash
34+
# APP_DIR env sets base dir
35+
APP_DEBUG=true APP_DIR=$(pwd) go run ./main.go -r -dir testdata -dir testdata2
36+
# Base dir set from flag
37+
APP_DEBUG=true go run ./main.go -r -b $APP_DIR -dir testdata -dir testdata2
3638
```
3739

38-
Use `go run` inside module
39-
```sh
40-
APP_DEBUG=true APP_DIR=$(pwd) go run ./main.go -r -b $APP_DIR -dir testdata -dir testdata2
40+
Using absolute path
41+
```bash
42+
cd ${PRO_PATH}/watcher # Base dir defaults to working dir
43+
APP_DEBUG=true go run ./main.go -r \
44+
-dir testdata \
45+
-dir ${PRO_PATH}/watcher/testdata2
4146
```
4247

43-
...another example with filters
44-
```sh
48+
Example with filters
49+
```bash
4550
APP_DEBUG=true APP_DIR=$(pwd) go run ./main.go -r -dir testdata \
4651
-include ".*.txt$" \
4752
-include ".*.json$" \
4853
-excludeDir ".*exclude.*" \
4954
-exclude ".*\/d.txt$"
5055
```
5156

57+
**TODO** See comments in main_test.go

main.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@ package main
22

33
import (
44
"fmt"
5+
"os"
6+
"os/signal"
7+
"syscall"
8+
59
"github.com/mozey/logutil"
610
"github.com/mozey/watcher/pkg/watcher"
711
"github.com/rs/zerolog"
812
"github.com/rs/zerolog/log"
9-
"os"
10-
"os/signal"
11-
"syscall"
1213
)
1314

14-
const AppDebug = "APP_DEBUG"
15+
// version has a hard-coded default value
16+
// https://github.com/mozey/config/issues/20
17+
// For custom builds, the version can be overwritten with ldflags, see
18+
// "Golang compile environment variable into binary"
19+
// https://stackoverflow.com/a/47665780/639133
20+
var version string = "v0.2.0"
1521

1622
func main() {
1723
// Exit on signal (ctrl + c)
@@ -21,8 +27,7 @@ func main() {
2127
logutil.SetupLogger(true)
2228

2329
// Override SetupLogger log level
24-
s := os.Getenv(AppDebug)
25-
debug := s == "true"
30+
debug := os.Getenv("APP_DEBUG") == "true"
2631
level := zerolog.InfoLevel
2732
if debug {
2833
level = zerolog.DebugLevel
@@ -37,8 +42,7 @@ func main() {
3742

3843
exitCode := 0
3944
if out.Cmd == watcher.CmdVersion {
40-
// TODO Add version
41-
fmt.Println("n/a")
45+
fmt.Println(version)
4246
sig <- os.Signal(syscall.SIGINT)
4347

4448
} else if out.Cmd == watcher.CmdWatch {

main_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
package main
22

33
// TODO Add ability to pass in a buf for output,
4-
// then run watcher on mock fs for tests?
5-
4+
// then run watcher on virtual fs for tests?

pkg/watcher/watcher.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@ package watcher
33
import (
44
"flag"
55
"fmt"
6-
"github.com/fsnotify/fsnotify"
7-
"github.com/pkg/errors"
8-
"github.com/rs/zerolog/log"
96
"os"
107
"path"
118
"path/filepath"
129
"regexp"
1310
"strings"
1411
"time"
15-
)
1612

17-
const AppDir = "APP_DIR"
13+
"github.com/fsnotify/fsnotify"
14+
"github.com/pkg/errors"
15+
"github.com/rs/zerolog/log"
16+
)
1817

1918
type MultiFlag []string
2019

@@ -47,8 +46,8 @@ type CmdIn struct {
4746
Debug bool
4847
// BaseDir for relative paths
4948
BaseDir string
50-
// Version
51-
Version bool
49+
// PrintVersion
50+
PrintVersion bool
5251
// WatchDirs is the dirs to watch
5352
WatchDirs MultiFlag
5453
// Recursive can be set to watch sub dirs
@@ -79,7 +78,7 @@ type CmdOut struct {
7978
func ParseFlags() *CmdIn {
8079
in := CmdIn{}
8180

82-
flag.BoolVar(&in.Version, "version", false, "Print version")
81+
flag.BoolVar(&in.PrintVersion, "version", false, "Print version")
8382
flag.BoolVar(&in.Recursive, "r", false, "Recursively watch sub dirs")
8483
flag.IntVar(&in.Limit, "l", 100, "Limit dirs to include recursively")
8584
flag.IntVar(&in.Delay, "d", 1500,
@@ -183,7 +182,7 @@ func (in *CmdIn) Watch(watcher *fsnotify.Watcher) {
183182
func Cmd(in *CmdIn) (out *CmdOut, err error) {
184183
out = &CmdOut{}
185184

186-
if in.Version {
185+
if in.PrintVersion {
187186
out.Cmd = CmdVersion
188187
return out, nil
189188
}
@@ -199,7 +198,13 @@ func Cmd(in *CmdIn) (out *CmdOut, err error) {
199198
for _, relativePath := range in.WatchDirs {
200199

201200
// Use absolute paths
202-
absolutePath := path.Join(in.BaseDir, relativePath)
201+
var absolutePath string
202+
if filepath.IsAbs(relativePath) {
203+
absolutePath = relativePath
204+
} else {
205+
// Prefix basedir
206+
absolutePath = path.Join(in.BaseDir, relativePath)
207+
}
203208

204209
// Check dir exclusion filter
205210
excluded, err := in.DirExcluded(absolutePath)
@@ -266,9 +271,11 @@ func Main(debug bool) (out *CmdOut, err error) {
266271

267272
in.Debug = debug
268273

269-
// Base dir is required, resolve in this order (flag, env, working dir)
274+
// Resolve base dir in this order (flag, env, working dir).
275+
// Specifying absolute paths for dirs/files to watch is also supported,
276+
// in that case the dir/file path is not prefixed with the base dir
270277
if in.BaseDir == "" {
271-
in.BaseDir = os.Getenv(AppDir)
278+
in.BaseDir = os.Getenv("APP_DIR")
272279
if in.BaseDir == "" {
273280
in.BaseDir, err = os.Getwd()
274281
if err != nil {

0 commit comments

Comments
 (0)