Skip to content

Commit

Permalink
Release version 0.1.1 - New cli feature: rizla -onreload=myscript.sh …
Browse files Browse the repository at this point in the history
…main.go | Read HISTORY.md
  • Loading branch information
kataras committed Mar 16, 2018
1 parent e51313e commit b1635b8
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 8 deletions.
40 changes: 40 additions & 0 deletions HISTORY.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,43 @@
## 0.1.1

Add `-onreload` to execute commands on reload through the `rizla` cli.

Example:

**on_reload.bat**

```sh
@echo off
echo Hello, custom script can goes here before reload, i.e build https://github.comkataras/bindata things!
```

**main.go**

```go
// Package main shows you how you can execute commands on reload
// in this example we will execute a simple bat file on windows
// but you can pass anything, it just runs the `exec.Command` based on -onreload= flag's value.
package main

import (
"flag"
"fmt"
)

func main() {
host := flag.String("host", "", "the host")
port := flag.Int("port", 0, "the port")
flag.Parse()
fmt.Printf("The 'host' argument is: %v\n", *host)
fmt.Printf("The 'port' argument is: %v\n", *port)
}

```

```sh
$ rizla -onreload="on_reload.bat" main.go -host myhost.com -port 1193
```

## 0.1.0

Rizla drops support for multi `main.go:func main()` programs in the same directory. It still accepts a filename with `main.go` but it depends on the directory now (as all examples already shown) in order to be able to run and watch projects with multiple `.go` files in the project's root directory, this is very useful especially when the project depends on libraries like `go-bindata` with a result of `.go` file in the root directory. For most cases that will not change anything. If you used to have many go programs with `func main()` in the same root directory please consider that this is not idiomatic and you must change this habit, the sooner the better.
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ The author of rizla is [@kataras](https://github.com/kataras).
Versioning
------------

Current: **v0.1.0**
Current: **v0.1.1**

[HISTORY](https://github.com/kataras/rizla/blob/master/HISTORY.md) file is your best friend!

Expand Down Expand Up @@ -164,7 +164,7 @@ License can be found [here](LICENSE).
[Travis]: http://travis-ci.org/kataras/rizla
[License Widget]: https://img.shields.io/badge/license-MIT%20%20License%20-E91E63.svg?style=flat-square
[License]: https://github.com/kataras/rizla/blob/master/LICENSE
[Release Widget]: https://img.shields.io/badge/release-v0.1.0-blue.svg?style=flat-square
[Release Widget]: https://img.shields.io/badge/release-v0.1.1-blue.svg?style=flat-square
[Release]: https://github.com/kataras/rizla/releases
[Chat Widget]: https://img.shields.io/badge/community-chat-00BCD4.svg?style=flat-square
[Chat]: https://kataras.rocket.chat/channel/rizla
Expand Down
18 changes: 18 additions & 0 deletions _examples/cli-onreload/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Package main shows you how you can execute commands on reload
// in this example we will execute a simple bat file on windows
// but you can pass anything, it just runs the `exec.Command` based on -onreload= flag's value.
package main

import (
"flag"
"fmt"
)

// rizla -onreload="on_reload.bat" main.go -host myhost.com -port 1193
func main() {
host := flag.String("host", "", "the host")
port := flag.Int("port", 0, "the port")
flag.Parse()
fmt.Printf("The 'host' argument is: %v\n", *host)
fmt.Printf("The 'port' argument is: %v\n", *port)
}
2 changes: 2 additions & 0 deletions _examples/cli-onreload/on_reload.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo off
echo Hello, custom script can goes here before reload, i.e build https://github.comkataras/bindata things!
42 changes: 36 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (

const (
// Version of rizla command line tool
Version = "0.1.0"
Version = "0.1.1"
// Name of rizla
Name = "Rizla"
// Description of rizla
Expand All @@ -30,24 +30,49 @@ const delayArgName = "-delay"

func getDelayFromArg(arg string) (time.Duration, bool) {
if strings.HasPrefix(arg, delayArgName) || strings.HasPrefix(arg, delayArgName[1:]) {
// [-]delay 5s
// [-]delay=5s
if spaceIdx := strings.IndexRune(arg, ' '); spaceIdx > 0 {
delayStr := arg[spaceIdx+1:]
// [-]delay 5s

if equalIdx := strings.IndexRune(arg, '='); equalIdx > 0 {
delayStr := arg[equalIdx+1:]
d, _ := time.ParseDuration(delayStr)
return d, true
}

if equalIdx := strings.IndexRune(arg, '='); equalIdx > 0 {
delayStr := arg[equalIdx+1:]
if spaceIdx := strings.IndexRune(arg, ' '); spaceIdx > 0 {
delayStr := arg[spaceIdx+1:]
d, _ := time.ParseDuration(delayStr)
return d, true
}

}

return 0, false
}

const onReloadArg = "-onreload"

func getOnReloadArg(arg string) (string, bool) {
if strings.HasPrefix(arg, onReloadArg) || strings.HasPrefix(arg, onReloadArg[1:]) {
// first equality ofc...
// [-]onreload=cmd1,cmd2,file.sh
// [-]onreload cmd1,cmd2,file.sh

if equalIdx := strings.IndexRune(arg, '='); equalIdx > 0 {
src := arg[equalIdx+1:]
return src, true
}

if spaceIdx := strings.IndexRune(arg, ' '); spaceIdx > 0 {
src := arg[spaceIdx+1:]
return src, true
}

}

return "", false
}

var helpTmpl = fmt.Sprintf(`NAME:
%s - %s
Expand All @@ -56,6 +81,7 @@ USAGE:
rizla C:/myprojects/project1/main.go C:/myprojects/project2/main.go C:/myprojects/project3/main.go
rizla -walk main.go [if -walk then rizla uses the stdlib's filepath.Walk method instead of file system's signals]
rizla -delay=5s main.go [if delay > 0 then it delays the reload, also note that it accepts the first change but the rest of changes every "delay"]
rizla -onreload="service supervisor restart" main.go or rizla -onreload="cmd /C echo Hello World!" main.go
VERSION:
%s
`, Name, Description, Version)
Expand Down Expand Up @@ -98,6 +124,10 @@ func main() {
delayOnDetect = delay
continue
}

if onReloadSources, ok := getOnReloadArg(a); ok {
rizla.OnReloadScripts = strings.Split(onReloadSources, ",")
}
}

// it's main.go or any go main program
Expand Down
32 changes: 32 additions & 0 deletions rizla/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rizla

import (
"os"
"os/exec"
"path/filepath"
"strings"
"time"
Expand Down Expand Up @@ -33,6 +34,13 @@ func DefaultWatcher(abs string) bool {
return !(base == ".git" || base == "node_modules" || base == "vendor" || base == ".")
}

// OnReloadScripts simple file names which will execute a script, i.e `./on_reload.sh` or `./on_reload.bat` or even `service supervisor restart`
// on windows, it will just execute that based on the operating system, nothing crazy here,
// they are filled by the cli but they can be customized by the source as well.
//
// If contains whitespaces, after the first whitespace they are the command's flags (if not a script file).
var OnReloadScripts []string

// DefaultOnReload fired when file has changed and reload going to happens
func DefaultOnReload(p *Project) func(string) {
return func(string) {
Expand All @@ -41,6 +49,30 @@ func DefaultOnReload(p *Project) func(string) {
fromproject = "From project '" + p.Name + "': "
}
p.Out.Infof("%sA change has been detected, reloading now...", fromproject)

if len(OnReloadScripts) > 0 {
p.Out.Infof("%sExecuting commands from %s before restart...", fromproject, strings.Join(OnReloadScripts, ", "))
for _, s := range OnReloadScripts {

// The below should work for things like
// service supervisor restart
nameAndFlags := strings.Split(s, " ")
var args []string
name := nameAndFlags[0]
if len(nameAndFlags) > 1 {
args = nameAndFlags[1:]
}

cmd := exec.Command(name, args...)
cmd.Stderr = p.Err.Printer.Output
cmd.Stdout = p.Out.Printer.Output
if err := cmd.Run(); err != nil {
p.Out.Errorf("%s%s run: %v", fromproject, s, err)
os.Exit(1)
}
cmd.Wait() // ignore error.
}
}
}
}

Expand Down

0 comments on commit b1635b8

Please sign in to comment.