From f6143808d354ceee559933cb51ff1959061c5cba Mon Sep 17 00:00:00 2001 From: Rafael Quintela Date: Sun, 13 Oct 2019 22:09:55 -0300 Subject: [PATCH] Change wtc arguments --- README.md | 65 ++++++++++------- configuration/config.go => config.go | 8 +-- main.go | 100 +++++++++++++-------------- wtc.yaml | 2 +- 4 files changed, 93 insertions(+), 82 deletions(-) rename configuration/config.go => config.go (76%) diff --git a/README.md b/README.md index fa5d903..7692df7 100644 --- a/README.md +++ b/README.md @@ -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 "" -run ""`, 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" +``` diff --git a/configuration/config.go b/config.go similarity index 76% rename from configuration/config.go rename to config.go index a6511f5..c22a694 100644 --- a/configuration/config.go +++ b/config.go @@ -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"` @@ -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"` } diff --git a/main.go b/main.go index fd0208c..b3c96ae 100644 --- a/main.go +++ b/main.go @@ -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 @@ -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) @@ -80,8 +95,8 @@ 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 } } @@ -89,10 +104,11 @@ func main() { 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", @@ -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{} @@ -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) diff --git a/wtc.yaml b/wtc.yaml index ac43b33..ae1e2ea 100644 --- a/wtc.yaml +++ b/wtc.yaml @@ -3,5 +3,5 @@ ignore: "\\.git/" trig: install rules: - name: install - match: ".go$" + match: "\\.go$" command: "go install"