Skip to content

Commit

Permalink
Make configuration file optional
Browse files Browse the repository at this point in the history
  • Loading branch information
zzwx committed Aug 1, 2020
1 parent a483e0f commit 07de658
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 79 deletions.
53 changes: 28 additions & 25 deletions README.md
2 changes: 1 addition & 1 deletion license_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ func TestLicense(t *testing.T) {
t.Errorf("License match percent not 100")
}

}
}
14 changes: 6 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
/*
fresh is a command line tool that builds and (re)starts your web application everytime you save a go or template file.
Fresh is a command-line tool that builds and (re)starts your written in Go application, including a web app, every time you save a Go or template file or any desired files you specify using configuration.
If the web framework you are using supports the fresh runner, it will show build errors on your browser.
Fresh can be started even without any configuration files.
It will watch for file events, and every time you create / modify or delete a file it will build and restart the application.
It currently works with Traffic (https://github.com/pilu/traffic), Martini (https://github.com/codegangsta/martini) and gocraft/web (https://github.com/gocraft/web).
If `go build` returns an error, it will create a log file in the tmp folder and keep watching, attempting to rebuild. It will also attempt to kill previously created processes.
fresh will watch for file events, and every time you create/modifiy/delete a file it will build and restart the application.
If `go build` returns an error, it will logs it in the tmp folder.
Traffic (https://github.com/pilu/traffic) already has a middleware that shows the content of that file if it is present. This middleware is automatically added if you run a Traffic web app in dev mode with fresh.
This is a fork of an original fresh (https://github.com/pilu/fresh) that is set as unmaintained. Check the README.md for more details.
*/
package main

Expand All @@ -26,7 +24,7 @@ func main() {

if *configPath != "" {
if _, err := os.Stat(*configPath); err != nil {
fmt.Printf("Can't find config file `%s`\n", *configPath)
fmt.Printf("Can't find config file %q\n", *configPath)
os.Exit(1)
} else {
os.Setenv("RUNNER_CONFIG_PATH", *configPath)
Expand Down
63 changes: 25 additions & 38 deletions runner/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,51 +88,38 @@ func loadEnvSettings() {
}

func loadRunnerConfigSettings() {

cfgPath := configPath()

if _, err := os.Stat(cfgPath); err != nil {
mainLog("Error opening config file %v: %v", cfgPath, err)
os.Exit(1)
}

mainLog("Loading settings from %s", cfgPath)

file, err := ioutil.ReadFile(cfgPath)

if err != nil {
mainLog("Error reading config file %v: %v", cfgPath, err)
os.Exit(1)
}

var givenSettings map[string]string

yaml.Unmarshal(file, &givenSettings)

//if givenSettings["version"] == "" {
// log.Fatalln("no version was set on config yaml file.")
//}

for key, value := range givenSettings {
if key == "ignored" {
// Allow old "ignored" setting to be an alias of "ignore".
key = "ignore"
}

if _, ok := settings[key]; !ok {
mainLog("Unknown setting: %s", key)
mainLog("No config file found at %q. Using default settings", cfgPath)
} else {
mainLog("Loading settings from %q", cfgPath)
file, err := ioutil.ReadFile(cfgPath)
if err != nil {
mainLog("Error reading config file %q: %v", cfgPath, err)
os.Exit(1)
}

if key == "colors" || key == "debug" {
if value == "1" {
value = "true"
var givenSettings map[string]string
yaml.Unmarshal(file, &givenSettings)
//if givenSettings["version"] == "" {
// log.Fatalln("no version was set on config yaml file.")
//}
for key, value := range givenSettings {
if key == "ignored" {
// Allow old "ignored" setting to be an alias of "ignore".
key = "ignore"
}
if _, ok := settings[key]; !ok {
mainLog("Unknown setting: %q", key)
os.Exit(1)
}
if key == "colors" || key == "debug" {
if value == "1" {
value = "true"
}
}
settings[key] = value
}

settings[key] = value
}

}

func initSettings() {
Expand Down
13 changes: 6 additions & 7 deletions runner/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package runner
import "testing"

func TestLogColor(t *testing.T) {
test := []struct{
test := []struct {
color string
expected string
}{
{color: "main", expected: "36" },
{color: "build", expected: "33" },
{color: "runner", expected: "32" },
{color: "watcher", expected: "35" },
{color: "app", expected: "" },
{color: "main", expected: "36"},
{color: "build", expected: "33"},
{color: "runner", expected: "32"},
{color: "watcher", expected: "35"},
{color: "app", expected: ""},
}

for _, v := range test {
Expand All @@ -21,4 +21,3 @@ func TestLogColor(t *testing.T) {
}
}
}

0 comments on commit 07de658

Please sign in to comment.