diff options
Diffstat (limited to 'pkg/config')
-rw-r--r-- | pkg/config/config.go | 10 | ||||
-rw-r--r-- | pkg/config/config_test.go | 15 | ||||
-rw-r--r-- | pkg/config/error.go | 15 | ||||
-rw-r--r-- | pkg/config/error_test.go | 2 | ||||
-rw-r--r-- | pkg/config/test_data/invalid_trainStop.yaml | 4 | ||||
-rw-r--r-- | pkg/config/test_data/minimal.yaml | 1 | ||||
-rw-r--r-- | pkg/config/test_data/minimal_with_hostname.yaml | 1 |
7 files changed, 41 insertions, 7 deletions
diff --git a/pkg/config/config.go b/pkg/config/config.go index f97467a..6e4bba5 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -9,13 +9,17 @@ import ( ) var validToken = regexp.MustCompile(`^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$`) +var validTrainStop = regexp.MustCompile(`^[a-zA-Z0-9:_]+$`) type Config struct { // Address is the hostname or ip the web server will listen to Address string `yaml:"address",default:"127.0.0.1"` - Port string `yaml:"port",default:"8080"` + // Port is the tcp port number or service name the web server will listen to + Port string `yaml:"port",default:"8080"` // Token is the sncf api token Token string `yaml:"token"` + // TrainStop is the navitia code of the train stop the webapp will monitor + TrainStop string `yaml:"trainStop"` } func (c *Config) validate() error { @@ -33,6 +37,10 @@ func (c *Config) validate() error { if ok := validToken.MatchString(c.Token); !ok { return newInvalidTokenError(c.Token) } + // TrainStop + if ok := validTrainStop.MatchString(c.TrainStop); !ok { + return newInvalidTrainStopError(c.TrainStop) + } return nil } diff --git a/pkg/config/config_test.go b/pkg/config/config_test.go index 6969191..a4c170b 100644 --- a/pkg/config/config_test.go +++ b/pkg/config/config_test.go @@ -11,16 +11,18 @@ import ( func TestLoadFile(t *testing.T) { // Minimal yaml file minimalConfig := Config{ - Address: "127.0.0.2", - Port: "8082", - Token: "12345678-9abc-def0-1234-56789abcdef0", + Address: "127.0.0.2", + Port: "8082", + Token: "12345678-9abc-def0-1234-56789abcdef0", + TrainStop: "ABCD:test:01", } // Minimal yaml file with hostname resolving minimalConfigWithResolving := Config{ - Address: "localhost", - Port: "www", - Token: "12345678-9abc-def0-1234-56789abcdef0", + Address: "localhost", + Port: "www", + Token: "12345678-9abc-def0-1234-56789abcdef0", + TrainStop: "VWXY_Z:test:90", } // Test cases @@ -36,6 +38,7 @@ func TestLoadFile(t *testing.T) { {"Unresolvable address should fail to load", "test_data/invalid_address_unresolvable.yaml", nil, &InvalidAddressError{}}, {"Invalid port should fail to load", "test_data/invalid_port.yaml", nil, &InvalidPortError{}}, {"Invalid token should fail to load", "test_data/invalid_token.yaml", nil, &InvalidTokenError{}}, + {"Invalid trainStop should fail to load", "test_data/invalid_trainStop.yaml", nil, &InvalidTrainStopError{}}, {"Minimal config", "test_data/minimal.yaml", &minimalConfig, nil}, {"Minimal config with resolving", "test_data/minimal_with_hostname.yaml", &minimalConfigWithResolving, nil}, } diff --git a/pkg/config/error.go b/pkg/config/error.go index c49b6a9..41a323c 100644 --- a/pkg/config/error.go +++ b/pkg/config/error.go @@ -90,3 +90,18 @@ func newInvalidTokenError(token string) error { token: token, } } + +// Invalid trainStop field error +type InvalidTrainStopError struct { + trainStop string +} + +func (e *InvalidTrainStopError) Error() string { + return fmt.Sprintf("Invalid trainStop %s : it must be a string that lookslike \"stop_area:SNCF:87723502\" (make sure to quote the string because of the colon characters)", e.trainStop) +} + +func newInvalidTrainStopError(trainStop string) error { + return &InvalidTrainStopError{ + trainStop: trainStop, + } +} diff --git a/pkg/config/error_test.go b/pkg/config/error_test.go index f9807c1..4a80b05 100644 --- a/pkg/config/error_test.go +++ b/pkg/config/error_test.go @@ -17,4 +17,6 @@ func TestErrorsCoverage(t *testing.T) { _ = invalidPortErr.Unwrap() invalidTokenErr := InvalidTokenError{} _ = invalidTokenErr.Error() + invalidTrainStopErr := InvalidTrainStopError{} + _ = invalidTrainStopErr.Error() } diff --git a/pkg/config/test_data/invalid_trainStop.yaml b/pkg/config/test_data/invalid_trainStop.yaml new file mode 100644 index 0000000..159950d --- /dev/null +++ b/pkg/config/test_data/invalid_trainStop.yaml @@ -0,0 +1,4 @@ +address: 127.0.0.2 +port: 8082 +token: 12345678-9abc-def0-1234-56789abcdef0 +trainStop: = diff --git a/pkg/config/test_data/minimal.yaml b/pkg/config/test_data/minimal.yaml index 2944645..e34db15 100644 --- a/pkg/config/test_data/minimal.yaml +++ b/pkg/config/test_data/minimal.yaml @@ -1,3 +1,4 @@ address: 127.0.0.2 port: 8082 token: 12345678-9abc-def0-1234-56789abcdef0 +trainStop: "ABCD:test:01" diff --git a/pkg/config/test_data/minimal_with_hostname.yaml b/pkg/config/test_data/minimal_with_hostname.yaml index dbede14..116688a 100644 --- a/pkg/config/test_data/minimal_with_hostname.yaml +++ b/pkg/config/test_data/minimal_with_hostname.yaml @@ -1,3 +1,4 @@ address: localhost port: www token: 12345678-9abc-def0-1234-56789abcdef0 +trainStop: "VWXY_Z:test:90" |