Skip to content

Commit

Permalink
Allow local config with .forego (#56)
Browse files Browse the repository at this point in the history
See #32
gotenv can read simple yaml file so we don't need to use a read yaml
parse.
The value in forego file will be set as default value, if we set flag
or env value, it will override the value in forego file
  • Loading branch information
Võ Anh Duy authored and ddollar committed Apr 13, 2016
1 parent 62551d4 commit dd6e989
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 0 deletions.
24 changes: 24 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"github.com/ddollar/forego/Godeps/_workspace/src/github.com/subosito/gotenv"
"os"
)

type Config map[string]string

func ReadConfig(filename string) (Config, error) {
if _, err := os.Stat(filename); os.IsNotExist(err) {
return make(Config), nil
}
fd, err := os.Open(filename)
if err != nil {
return nil, err
}
defer fd.Close()
config := make(Config)
for key, val := range gotenv.Parse(fd) {
config[key] = val
}
return config, nil
}
11 changes: 11 additions & 0 deletions config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package main

import "testing"

func TestReadOptionFile(t *testing.T) {
config_file := "./fixtures/options/.forego"
_, err := ReadConfig(config_file)
if err != nil {
t.Fatalf("Could not read config file: %s", err)
}
}
3 changes: 3 additions & 0 deletions fixtures/configs/.forego
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
procfile: Procfile.dev
concurrency: foo=2,bar=3
port: 15000
19 changes: 19 additions & 0 deletions start.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,25 @@ func init() {
cmdStart.Flag.IntVar(&flagPort, "p", defaultPort, "port")
cmdStart.Flag.StringVar(&flagConcurrency, "c", "", "concurrency")
cmdStart.Flag.BoolVar(&flagRestart, "r", false, "restart")
err := readConfigFile(".forego", &flagProcfile, &flagPort, &flagConcurrency)
handleError(err)
}

func readConfigFile(config_path string, flagProcfile *string, flagPort *int, flagConcurrency *string) error {
config, err := ReadConfig(config_path)

if config["procfile"] != "" {
*flagProcfile = config["procfile"]
} else {
*flagProcfile = "Procfile"
}
if config["port"] != "" {
*flagPort, err = strconv.Atoi(config["port"])
} else {
*flagPort = defaultPort
}
*flagConcurrency = config["concurrency"]
return err
}

func parseConcurrency(value string) (map[string]int, error) {
Expand Down
23 changes: 23 additions & 0 deletions start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,3 +145,26 @@ func TestPortFromEnv(t *testing.T) {
}

}

func TestConfigBeOverrideByForegoFile(t *testing.T) {
var procfile = "Profile"
var port = 5000
var concurrency string = "web=2"
err := readConfigFile("./fixtures/configs/.forego", &procfile, &port, &concurrency)

if err != nil {
t.Fatalf("Cannot set default values from forego config file")
}

if procfile != "Procfile.dev" {
t.Fatal("Procfile should be Procfile.dev")
}

if port != 15000 {
t.Fatal("port should be 15000, got %d", port)
}

if concurrency != "foo=2,bar=3" {
t.Fatal("concurrency should be 'foo=2,bar=3', got %s", concurrency)
}
}

0 comments on commit dd6e989

Please sign in to comment.