Skip to content

Commit

Permalink
feat: add -w/--watchPaths for subcommand gf run
Browse files Browse the repository at this point in the history
Live reload supports files specified using the "-w/--watchPaths" parameter in addition to Go files.

Help message for `gf run -w`:

```text
    -w, --watchPaths   watch additional paths for live reload, separated by ",". i.e. "manifest/config/*.yaml"
```

Usage Example:

1) Watch one file
$ gf run main.go -w manifest/config/config.yaml

2) Watch multiple files, seprated by commas
$ gf run main.go -w manifest/config/config.yaml,otherpath/otherfile

3) Watch wildcard file
$ gf run main.go -w "manifest/config/*.yaml"

Fixes gogf#2822
  • Loading branch information
windvalley committed Oct 10, 2023
1 parent ed9d213 commit a518f63
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions cmd/gf/internal/cmd/cmd_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package cmd
import (
"context"
"fmt"
"os"
"path/filepath"
"runtime"
"strings"

Expand All @@ -20,7 +20,6 @@ import (
"github.com/gogf/gf/v2/os/gproc"
"github.com/gogf/gf/v2/os/gtime"
"github.com/gogf/gf/v2/os/gtimer"
"github.com/gogf/gf/v2/text/gstr"
"github.com/gogf/gf/v2/util/gtag"

"github.com/gogf/gf/cmd/gf/v2/internal/utility/mlog"
Expand Down Expand Up @@ -58,7 +57,7 @@ which compiles and runs the go codes asynchronously when codes change.
cRunPathBrief = `output directory path for built binary file. it's "manifest/output" in default`
cRunExtraBrief = `the same options as "go run"/"go build" except some options as follows defined`
cRunArgsBrief = `custom arguments for your process`
cRunWatchPathsBrief = `watch additional paths for live reload, separated by ",". i.e. "manifest/config/config.yaml"`
cRunWatchPathsBrief = `watch additional paths for live reload, separated by ",". i.e. "manifest/config/*.yaml"`
)

var (
Expand Down Expand Up @@ -181,11 +180,18 @@ func (app *cRunApp) Run(ctx context.Context) {
}

func matchWatchPaths(watchPaths []string, eventPath string) bool {
pathSeparator := string(os.PathSeparator)
for _, path := range watchPaths {
path = gstr.TrimLeftStr(path, pathSeparator)
path = gstr.TrimLeftStr(path, "."+pathSeparator)
if gstr.HasSuffix(eventPath, pathSeparator+path) {
absPath, err := filepath.Abs(path)
if err != nil {
mlog.Printf("match watchPath error: %s", err.Error())
continue
}
matched, err := filepath.Match(absPath, eventPath)
if err != nil {
mlog.Printf("match watchPaths error: %s", err.Error())
continue
}
if matched {
return true
}
}
Expand Down

0 comments on commit a518f63

Please sign in to comment.