Skip to content

Commit

Permalink
Change wtc arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelsq committed Oct 14, 2019
1 parent 6d0ac38 commit f614380
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 82 deletions.
65 changes: 39 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,53 +12,66 @@ Although the utility is written in [Go](https://golang.org/), you can use it for

Before you begin, ensure you have installed the latest version of Go. See the [Go documentation](https://golang.org/doc/install) for details.

## How to install
## Install

You can install Watch as follows:
`$ go get -u github.com/rafaelsq/wtc`

1. Install the Watch directory:

```bash
$ go get -u github.com/rafaelsq/wtc
```
## Usage

2. Change directory to the project where you want to run Watch:

```bash
$ cd my_go_project
```

3. Run the following to build the utility:
```
USAGE:
$ wtc [[flags] regex command]
If [.]wtc.yaml exists, it will be used.
FLAGS:
-debounce int
global debounce (default 300)
-ignore string
regex
-no-trace
disable messages.
```

### Example

`wtc "_test\.go$" "go test -cover {PKG}"`

```bash
$ wtc -build "go build main.go" -run "./my_go_project"
```

## How to use
## Usage with [.]wtc.yaml

You can configure Watch by creating an YAML file with your own rules.

The default is:
Example:

```yaml
no_trace: false
debounce: 300
debounce: 300 # if rule has no debounce, this will be used instead
ignore: "\\.git/"
trig: build
trig: buildNRun # will run on start
rules:
- name: build
match: ".go$"
- name: buildNRun
match: "\\.go$"
ignore: "_test\\.go$"
command: "go build"
trig: run
- name: run
match: "^$"
command: "./$(basename `pwd`)"
- name: test
match: "_test\\.go$"
command: "go test -cover {PKG}"
```
> **_Note:_** If you run `wtc -build "<build-cmd>" -run "<run-cmd>"`, the utility replaces the default commands above.
If you create your own `.wtc.yaml` or `wtc.yaml`, no default rules will exist.
## Dev
`$ make` will watch for changes and run `go install`
```yaml
debounce: 100
ignore: "\\.git/"
trig: install
rules:
- name: install
match: "\\.go$"
command: "go install"
```
8 changes: 4 additions & 4 deletions configuration/config.go → config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package configuration
package main

// Config defines the options for watching files
type Config struct {
NoTrace bool `yaml:"no_trace"`
Ignore *string `yaml:"ignore"`
Ignore string `yaml:"ignore"`
Debounce int `yaml:"debounce"`
Rules []*Rule `yaml:"rules"`
Trig *string `yaml:"trig"`
Expand All @@ -13,8 +13,8 @@ type Config struct {
type Rule struct {
Name string `yaml:"name"`
Match string `yaml:"match"`
Ignore *string `yaml:"ignore"`
Debounce int `yaml:"debounce"`
Ignore string `yaml:"ignore"`
Debounce *int `yaml:"debounce"`
Command string `yaml:"command"`
Trig *string `yaml:"trig"`
}
100 changes: 49 additions & 51 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,10 @@ import (
"syscall"
"time"

"github.com/rafaelsq/wtc/configuration"
"github.com/rjeczalik/notify"
yaml "gopkg.in/yaml.v2"
)

var config configuration.Config

var contexts map[string]context.CancelFunc
var ctxmutex sync.Mutex

Expand All @@ -37,27 +34,45 @@ func getContext(label string) context.Context {
return ctx
}

func main() {
buildCMD := flag.String("build", "go build", "specify build command")
runCMD := flag.String("run", "./$(basename `pwd`)", "specify run command")
flag.Parse()
var config *Config

config = configuration.Config{
Debounce: 300,
Ignore: &[]string{`\.git/`}[0],
Rules: []*configuration.Rule{},
func main() {
flag.CommandLine.Usage = func() {
fmt.Fprintf(
flag.CommandLine.Output(),
"USAGE:\n$ wtc [[flags] regex command]\n\n"+
"If [.]wtc.yaml exists, it will be used.\n\n"+
"FLAGS:\n",
)
flag.PrintDefaults()
}

if err := genConfig(&config, *buildCMD, *runCMD); err != nil {
config = &Config{Debounce: 300}

flag.IntVar(&config.Debounce, "debounce", 300, "global debounce")
flag.StringVar(&config.Ignore, "ignore", "", "regex")
flag.BoolVar(&config.NoTrace, "no-trace", false, "disable messages.")

flag.Parse()

if has, err := readConfig(config); err != nil {
log.Fatal(err)
} else if !has && flag.NArg() < 2 {
fmt.Fprintf(os.Stderr, "No [.]wtc.yaml or valid command provided.\n")
flag.CommandLine.Usage()
return
} else {
config.Rules = append(config.Rules, &Rule{
Name: "run",
Match: flag.Arg(0),
Command: flag.Arg(1),
})
}

for _, rule := range config.Rules {
if rule.Debounce == 0 {
rule.Debounce = config.Debounce
}
}
start(config)
}

func start(config *Config) {
contexts = make(map[string]context.CancelFunc)

c := make(chan notify.EventInfo)
Expand All @@ -80,19 +95,20 @@ func main() {
pkg := strings.Join(pieces[:len(pieces)-1], "/")

// ignore
if config.Ignore != nil {
if retrieveRegexp(*config.Ignore).MatchString(path) {
if config.Ignore != "" {
if retrieveRegexp(config.Ignore).MatchString(path) {
continue
}
}

for _, rule := range config.Rules {
rule := rule

if rule.Ignore != nil && retrieveRegexp(*rule.Ignore).MatchString(path) {
if rule.Ignore != "" && retrieveRegexp(rule.Ignore).MatchString(path) {
continue
}
if retrieveRegexp(rule.Match).MatchString(path) {

if rule.Match != "" && retrieveRegexp(rule.Match).MatchString(path) {
go func() {
if err := trig(rule, pkg, path); err != nil {
fmt.Printf("\033[30;1m[%s] \033[31;1m[%s failed]\033[0m \033[30;1m%s\033[0m\n",
Expand All @@ -116,40 +132,17 @@ func findFile() ([]byte, error) {
return nil, nil
}

func genConfig(config *configuration.Config, buildCMD, runCMD string) error {
func readConfig(config *Config) (bool, error) {
yamlFile, err := findFile()
if err != nil {
return err
return false, err
}

if yamlFile == nil {
config.Trig = &[]string{"build"}[0]
config.Rules = append(config.Rules, &configuration.Rule{
Name: "run",
Match: `^$`,
Command: runCMD,
})

config.Rules = append(config.Rules, &configuration.Rule{
Name: "build",
Match: `\.go$`,
Debounce: config.Debounce,
Ignore: &[]string{`_test\.go$`}[0],
Command: buildCMD,
Trig: &[]string{"run"}[0],
})

config.Rules = append(config.Rules, &configuration.Rule{
Name: "test",
Match: `_test\.go$`,
Debounce: config.Debounce,
Command: "go test -cover {PKG}",
})

return nil
if len(yamlFile) != 0 {
return true, yaml.Unmarshal(yamlFile, &config)
}

return yaml.Unmarshal(yamlFile, &config)
return false, nil
}

var regexpMutex = &sync.Mutex{}
Expand Down Expand Up @@ -178,13 +171,18 @@ func findAndTrig(key, pkg, path string) {
}
}

func trig(rule *configuration.Rule, pkg, path string) error {
func trig(rule *Rule, pkg, path string) error {
ctx := getContext(rule.Name)

debounce := config.Debounce
if rule.Debounce != nil {
debounce = *rule.Debounce
}

select {
case <-ctx.Done():
return nil
case <-time.After(time.Duration(rule.Debounce) * time.Millisecond):
case <-time.After(time.Duration(debounce) * time.Millisecond):
}

cmd := strings.Replace(strings.Replace(rule.Command, "{PKG}", pkg, -1), "{FILE}", path, -1)
Expand Down
2 changes: 1 addition & 1 deletion wtc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ ignore: "\\.git/"
trig: install
rules:
- name: install
match: ".go$"
match: "\\.go$"
command: "go install"

0 comments on commit f614380

Please sign in to comment.