diff --git a/main.go b/main.go index 1dea9dd..1cc9c90 100644 --- a/main.go +++ b/main.go @@ -15,8 +15,9 @@ package main import ( "flag" "fmt" - "github.com/pilu/fresh/runner" "os" + + "github.com/pilu/fresh/runner" ) func main() { diff --git a/runner/runner.go b/runner/runner.go index f15f89a..bebd22d 100644 --- a/runner/runner.go +++ b/runner/runner.go @@ -1,39 +1,53 @@ package runner import ( - "io" - "os/exec" + "flag" + "io" + "os/exec" + "strings" ) -func run() bool { - runnerLog("Running...") - - cmd := exec.Command(buildPath()) - - stderr, err := cmd.StderrPipe() - if err != nil { - fatal(err) - } - - stdout, err := cmd.StdoutPipe() - if err != nil { - fatal(err) - } +var cmdArgs string - err = cmd.Start() - if err != nil { - fatal(err) - } - - go io.Copy(appLogWriter{}, stderr) - go io.Copy(appLogWriter{}, stdout) - - go func() { - <-stopChannel - pid := cmd.Process.Pid - runnerLog("Killing PID %d", pid) - cmd.Process.Kill() - }() +func init() { + flag.StringVar(&cmdArgs, "a", "", "Command line arguments to pass to the process") +} - return true +func run() bool { + runnerLog("Running...") + + var cmd *exec.Cmd + if cmdArgs != "" { + args := strings.Split(cmdArgs, " ") + cmd = exec.Command(buildPath(), args...) + } else { + cmd = exec.Command(buildPath()) + } + + stderr, err := cmd.StderrPipe() + if err != nil { + fatal(err) + } + + stdout, err := cmd.StdoutPipe() + if err != nil { + fatal(err) + } + + err = cmd.Start() + if err != nil { + fatal(err) + } + + go io.Copy(appLogWriter{}, stderr) + go io.Copy(appLogWriter{}, stdout) + + go func() { + <-stopChannel + pid := cmd.Process.Pid + runnerLog("Killing PID %d", pid) + cmd.Process.Kill() + }() + + return true } diff --git a/runner/settings.go b/runner/settings.go index 61e2412..0c2e187 100644 --- a/runner/settings.go +++ b/runner/settings.go @@ -1,138 +1,138 @@ package runner import ( - "fmt" - "github.com/pilu/config" - "os" - "path/filepath" - "strconv" - "strings" - "time" + "fmt" + "github.com/pilu/config" + "os" + "path/filepath" + "strconv" + "strings" + "time" ) const ( - envSettingsPrefix = "RUNNER_" - mainSettingsSection = "Settings" + envSettingsPrefix = "RUNNER_" + mainSettingsSection = "Settings" ) var settings = map[string]string{ - "config_path": "./runner.conf", - "root": ".", - "tmp_path": "./tmp", - "build_name": "runner-build", - "build_log": "runner-build-errors.log", - "valid_ext": ".go, .tpl, .tmpl, .html", - "build_delay": "600", - "colors": "1", - "log_color_main": "cyan", - "log_color_build": "yellow", - "log_color_runner": "green", - "log_color_watcher": "magenta", - "log_color_app": "", + "config_path": "./runner.conf", + "root": ".", + "tmp_path": "./tmp", + "build_name": "runner-build", + "build_log": "runner-build-errors.log", + "valid_ext": ".go, .tpl, .tmpl, .html", + "build_delay": "600", + "colors": "1", + "log_color_main": "cyan", + "log_color_build": "yellow", + "log_color_runner": "green", + "log_color_watcher": "magenta", + "log_color_app": "", } var colors = map[string]string{ - "reset": "0", - "black": "30", - "red": "31", - "green": "32", - "yellow": "33", - "blue": "34", - "magenta": "35", - "cyan": "36", - "white": "37", - "bold_black": "30;1", - "bold_red": "31;1", - "bold_green": "32;1", - "bold_yellow": "33;1", - "bold_blue": "34;1", - "bold_magenta": "35;1", - "bold_cyan": "36;1", - "bold_white": "37;1", - "bright_black": "30;2", - "bright_red": "31;2", - "bright_green": "32;2", - "bright_yellow": "33;2", - "bright_blue": "34;2", - "bright_magenta": "35;2", - "bright_cyan": "36;2", - "bright_white": "37;2", + "reset": "0", + "black": "30", + "red": "31", + "green": "32", + "yellow": "33", + "blue": "34", + "magenta": "35", + "cyan": "36", + "white": "37", + "bold_black": "30;1", + "bold_red": "31;1", + "bold_green": "32;1", + "bold_yellow": "33;1", + "bold_blue": "34;1", + "bold_magenta": "35;1", + "bold_cyan": "36;1", + "bold_white": "37;1", + "bright_black": "30;2", + "bright_red": "31;2", + "bright_green": "32;2", + "bright_yellow": "33;2", + "bright_blue": "34;2", + "bright_magenta": "35;2", + "bright_cyan": "36;2", + "bright_white": "37;2", } func logColor(logName string) string { - settingsKey := fmt.Sprintf("log_color_%s", logName) - colorName := settings[settingsKey] + settingsKey := fmt.Sprintf("log_color_%s", logName) + colorName := settings[settingsKey] - return colors[colorName] + return colors[colorName] } func loadEnvSettings() { - for key, _ := range settings { - envKey := fmt.Sprintf("%s%s", envSettingsPrefix, strings.ToUpper(key)) - if value := os.Getenv(envKey); value != "" { - settings[key] = value - } - } + for key, _ := range settings { + envKey := fmt.Sprintf("%s%s", envSettingsPrefix, strings.ToUpper(key)) + if value := os.Getenv(envKey); value != "" { + settings[key] = value + } + } } func loadRunnerConfigSettings() { - if _, err := os.Stat(configPath()); err != nil { - return - } - - logger.Printf("Loading settings from %s", configPath()) - sections, err := config.ParseFile(configPath(), mainSettingsSection) - if err != nil { - return - } - - for key, value := range sections[mainSettingsSection] { - settings[key] = value - } + if _, err := os.Stat(configPath()); err != nil { + return + } + + logger.Printf("Loading settings from %s", configPath()) + sections, err := config.ParseFile(configPath(), mainSettingsSection) + if err != nil { + return + } + + for key, value := range sections[mainSettingsSection] { + settings[key] = value + } } func initSettings() { - loadEnvSettings() - loadRunnerConfigSettings() + loadEnvSettings() + loadRunnerConfigSettings() } func getenv(key, defaultValue string) string { - if value := os.Getenv(key); value != "" { - return value - } + if value := os.Getenv(key); value != "" { + return value + } - return defaultValue + return defaultValue } func root() string { - return settings["root"] + return settings["root"] } func tmpPath() string { - return settings["tmp_path"] + return settings["tmp_path"] } func buildName() string { - return settings["build_name"] + return settings["build_name"] } func buildPath() string { - return filepath.Join(tmpPath(), buildName()) + return filepath.Join(tmpPath(), buildName()) } func buildErrorsFileName() string { - return settings["build_log"] + return settings["build_log"] } func buildErrorsFilePath() string { - return filepath.Join(tmpPath(), buildErrorsFileName()) + return filepath.Join(tmpPath(), buildErrorsFileName()) } func configPath() string { - return settings["config_path"] + return settings["config_path"] } func buildDelay() time.Duration { - value, _ := strconv.Atoi(settings["build_delay"]) + value, _ := strconv.Atoi(settings["build_delay"]) - return time.Duration(value) + return time.Duration(value) }