Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cp os.Args to runner #37

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,25 @@ Traffic (https://github.com/pilu/traffic) already has a middleware that shows th
package main

import (
"flag"
"fmt"
"github.com/pilu/fresh/runner"
"github.com/icattlecoder/fresh/runner"
"os"
)

func main() {
configPath := flag.String("c", "", "config file path")
flag.Parse()

if *configPath != "" {
if _, err := os.Stat(*configPath); err != nil {
fmt.Printf("Can't find config file `%s`\n", *configPath)
configPath := ""
args := os.Args
if len(args) > 2 && args[1] == "-c" {
configPath = args[2]
}

if configPath != "" {
if _, err := os.Stat(configPath); err != nil {
fmt.Printf("Can't find config file `%s`\n", configPath)
os.Exit(1)
} else {
os.Setenv("RUNNER_CONFIG_PATH", *configPath)
os.Setenv("RUNNER_CONFIG_PATH", configPath)
}
}

Expand Down
1 change: 1 addition & 0 deletions runner.conf.sample
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
root: .
watch_path: .
tmp_path: ./tmp
build_name: runner-build
build_log: runner-build-errors.log
Expand Down
5 changes: 5 additions & 0 deletions runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"io"
"os"
"os/exec"
)

Expand All @@ -10,6 +11,10 @@ func run() bool {

cmd := exec.Command(buildPath())

if len(os.Args) > 2 && os.Args[1] == "-c" {
cmd.Args = append([]string{buildPath()}, os.Args[3:]...)
}

stderr, err := cmd.StderrPipe()
if err != nil {
fatal(err)
Expand Down
8 changes: 6 additions & 2 deletions runner/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const (
var settings = map[string]string{
"config_path": "./runner.conf",
"root": ".",
"watch_path": ".",
"tmp_path": "./tmp",
"build_name": "runner-build",
"build_log": "runner-build-errors.log",
Expand Down Expand Up @@ -108,6 +109,10 @@ func root() string {
return settings["root"]
}

func watchPath() string {
return settings["watch_path"]
}

func tmpPath() string {
return settings["tmp_path"]
}
Expand All @@ -133,6 +138,5 @@ func configPath() string {

func buildDelay() time.Duration {
value, _ := strconv.Atoi(settings["build_delay"])

return time.Duration(value)
return time.Duration(value) * time.Millisecond
}
13 changes: 8 additions & 5 deletions runner/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,16 @@ var (
appLog logFunc
)

func flushEvents() {
func flushEvents(delay time.Duration) {

for {
t := time.NewTicker(delay)
select {
case eventName := <-startChannel:
mainLog("receiving event %s", eventName)
default:
t.Stop()
case _ = <-t.C:
t.Stop()
return
}
}
Expand All @@ -43,11 +47,10 @@ func start() {
eventName := <-startChannel

mainLog("receiving first event %s", eventName)
mainLog("sleeping for %d milliseconds", buildDelay)
time.Sleep(buildDelay * time.Millisecond)
mainLog("sleeping for %d milliseconds", buildDelay/1e6)
mainLog("flushing events")

flushEvents()
flushEvents(buildDelay)

mainLog("Started! (%d Goroutines)", runtime.NumGoroutine())
err := removeBuildErrorsLog()
Expand Down
17 changes: 11 additions & 6 deletions runner/watcher.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package runner

import (
"github.com/howeyc/fsnotify"
"github.com/fsnotify/fsnotify"
"os"
"path/filepath"
"strings"
Expand All @@ -16,28 +16,33 @@ func watchFolder(path string) {
go func() {
for {
select {
case ev := <-watcher.Event:
case ev := <-watcher.Events:
if isWatchedFile(ev.Name) {
watcherLog("sending event %s", ev)
startChannel <- ev.String()
}
case err := <-watcher.Error:
case err := <-watcher.Errors:
watcherLog("error: %s", err)
}
}
}()

watcherLog("Watching %s", path)
err = watcher.Watch(path)
err = watcher.Add(path)

if err != nil {
fatal(err)
}
}

func watch() {
root := root()
filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
watchPath := watchPath()

if !filepath.IsAbs(watchPath) {
watchPath, _ = filepath.Abs(watchPath)
}

filepath.Walk(watchPath, func(path string, info os.FileInfo, err error) error {
if info.IsDir() && !isTmpDir(path) {
if len(path) > 1 && strings.HasPrefix(filepath.Base(path), ".") {
return filepath.SkipDir
Expand Down