Skip to content

Commit 1ae29ea

Browse files
author
Alex Rios
committed
new flag to set config file path
1 parent c6823ae commit 1ae29ea

File tree

3 files changed

+74
-23
lines changed

3 files changed

+74
-23
lines changed

files.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package main
22

33
import (
44
"encoding/json"
5+
56
"github.com/spf13/afero"
67
)
78

@@ -42,8 +43,8 @@ func (c *ConfigFile) enforceDefaults() {
4243
}
4344
}
4445

45-
func loadConfig(fs afero.Fs) (ConfigFile, error) {
46-
file, err := afero.ReadFile(fs, DefaultConfigurationFileName)
46+
func loadConfig(fs afero.Fs, path string) (ConfigFile, error) {
47+
file, err := afero.ReadFile(fs, path)
4748
if err != nil {
4849
return ConfigFile{}, err
4950
}
@@ -57,16 +58,16 @@ func loadConfig(fs afero.Fs) (ConfigFile, error) {
5758
return configFile, nil
5859
}
5960

60-
func isFirstRun(fs afero.Fs) (bool, error) {
61-
exists, err := afero.Exists(fs, DefaultConfigurationFileName)
61+
func isFirstRun(fs afero.Fs, path string) (bool, error) {
62+
exists, err := afero.Exists(fs, path)
6263
if err != nil {
6364
return false, err
6465
}
6566
return !exists, nil
6667
}
6768

68-
func configureFirstRun(fs afero.Fs) error {
69-
err := afero.WriteFile(fs, DefaultConfigurationFileName, []byte(DefaultConfigurationFileContent), 0644)
69+
func configureFirstRun(fs afero.Fs, path string) error {
70+
err := afero.WriteFile(fs, path, []byte(DefaultConfigurationFileContent), 0644)
7071
if err != nil {
7172
return err
7273
}

files_test.go

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,42 @@
11
package main
22

33
import (
4-
"github.com/spf13/afero"
5-
"github.com/stretchr/testify/assert"
4+
"path/filepath"
65
"syscall"
76
"testing"
7+
8+
"github.com/spf13/afero"
9+
"github.com/stretchr/testify/assert"
810
)
911

1012
func TestFirstRun(t *testing.T) {
1113
t.Run("is the first time running", func(t *testing.T) {
1214
fs := afero.NewMemMapFs()
13-
firstRun, err := isFirstRun(fs)
15+
firstRun, err := isFirstRun(fs, DefaultConfigurationFileName)
16+
17+
assert.Nil(t, err)
18+
assert.True(t, firstRun)
19+
})
20+
t.Run("is the first time running given a config path", func(t *testing.T) {
21+
fs := afero.NewMemMapFs()
22+
firstRun, err := isFirstRun(fs, filepath.Join("newpath", DefaultConfigurationFileName))
1423

1524
assert.Nil(t, err)
1625
assert.True(t, firstRun)
1726
})
1827
t.Run("is NOT the first time running", func(t *testing.T) {
1928
fs := afero.NewMemMapFs()
2029
_, _ = fs.Create(DefaultConfigurationFileName)
21-
firstRun, err := isFirstRun(fs)
30+
firstRun, err := isFirstRun(fs, DefaultConfigurationFileName)
31+
32+
assert.Nil(t, err)
33+
assert.False(t, firstRun)
34+
})
35+
t.Run("is NOT the first time running given a config path", func(t *testing.T) {
36+
fs := afero.NewMemMapFs()
37+
path := filepath.Join("newpath", DefaultConfigurationFileName)
38+
_, _ = fs.Create(path)
39+
firstRun, err := isFirstRun(fs, path)
2240

2341
assert.Nil(t, err)
2442
assert.False(t, firstRun)
@@ -28,14 +46,21 @@ func TestFirstRun(t *testing.T) {
2846
func TestConfigureFirstRun(t *testing.T) {
2947
t.Run("configure first time use files", func(t *testing.T) {
3048
fs := afero.NewMemMapFs()
31-
err := configureFirstRun(fs)
49+
err := configureFirstRun(fs, DefaultConfigurationFileName)
50+
51+
assert.Nil(t, err)
52+
})
53+
54+
t.Run("configure first time use files given a config path", func(t *testing.T) {
55+
fs := afero.NewMemMapFs()
56+
err := configureFirstRun(fs, filepath.Join("newpath", DefaultConfigurationFileName))
3257

3358
assert.Nil(t, err)
3459
})
3560

3661
t.Run("cannot configure when is a read only dir", func(t *testing.T) {
3762
fs := afero.NewReadOnlyFs(afero.NewMemMapFs())
38-
err := configureFirstRun(fs)
63+
err := configureFirstRun(fs, DefaultConfigurationFileName)
3964
if assert.Error(t, err) {
4065
assert.Equal(t, syscall.Errno(1), err)
4166
}
@@ -45,9 +70,22 @@ func TestConfigureFirstRun(t *testing.T) {
4570
func TestLoadConfig(t *testing.T) {
4671
t.Run("loading config file", func(t *testing.T) {
4772
fs := afero.NewMemMapFs()
48-
_ = configureFirstRun(fs)
73+
_ = configureFirstRun(fs, DefaultConfigurationFileName)
74+
75+
config, err := loadConfig(fs, DefaultConfigurationFileName)
76+
assert.Nil(t, err)
77+
assert.Equal(t, ":8080", config.Addr)
78+
assert.Equal(t, 1, len(config.Responses))
79+
assert.Equal(t, 200, config.Responses[0].Status)
80+
assert.Equal(t, "GET", config.Responses[0].Method)
81+
assert.Equal(t, "0ms", config.Responses[0].Latency)
82+
})
83+
t.Run("loading config file", func(t *testing.T) {
84+
path := filepath.Join("newpath", DefaultConfigurationFileName)
85+
fs := afero.NewMemMapFs()
86+
_ = configureFirstRun(fs, path)
4987

50-
config, err := loadConfig(fs)
88+
config, err := loadConfig(fs, path)
5189
assert.Nil(t, err)
5290
assert.Equal(t, ":8080", config.Addr)
5391
assert.Equal(t, 1, len(config.Responses))

main.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,42 +2,54 @@ package main
22

33
import (
44
"context"
5+
"flag"
56
"fmt"
6-
"github.com/gorilla/handlers"
7-
"github.com/gorilla/mux"
8-
log "github.com/sirupsen/logrus"
9-
"github.com/spf13/afero"
107
"net/http"
118
"os"
129
"os/signal"
10+
"path/filepath"
1311
"syscall"
1412
"time"
13+
14+
"github.com/gorilla/handlers"
15+
"github.com/gorilla/mux"
16+
log "github.com/sirupsen/logrus"
17+
"github.com/spf13/afero"
1518
)
1619

1720
const ShutdownTimeout = "30s"
1821

1922
func main() {
23+
var cfgPath string
24+
flag.StringVar(&cfgPath, "config", "", "location of config file (default: .)")
25+
flag.Parse()
26+
2027
logger := log.New()
2128
var appFs = afero.NewOsFs()
22-
if err := run(appFs, logger); err != nil {
29+
if cfgPath == "" {
30+
cfgPath = DefaultConfigurationFileName
31+
} else {
32+
cfgPath = filepath.Join(cfgPath, DefaultConfigurationFileName)
33+
}
34+
if err := run(appFs, cfgPath, logger); err != nil {
2335
log.WithField("fn", "main").Error(err)
2436
os.Exit(1)
2537
}
2638
}
2739

28-
func run(fs afero.Fs, log *log.Logger) error {
29-
firstRun, err := isFirstRun(fs)
40+
func run(fs afero.Fs, path string, log *log.Logger) error {
41+
firstRun, err := isFirstRun(fs, path)
3042
if err != nil {
3143
return err
3244
}
3345
if firstRun {
34-
err = configureFirstRun(fs)
46+
err = configureFirstRun(fs, path)
3547
if err != nil {
3648
return err
3749
}
3850
}
3951

40-
configFile, err := loadConfig(fs)
52+
configFile, err := loadConfig(fs, path)
4153
if err != nil {
4254
return err
4355
}

0 commit comments

Comments
 (0)