aboutsummaryrefslogtreecommitdiff
path: root/config/config.go
diff options
context:
space:
mode:
authorJulien Dessaux2021-04-05 17:52:31 +0200
committerJulien Dessaux2021-04-05 17:52:31 +0200
commit1ffc9c42054e208a01d3e70e6b6f3e1781e798f8 (patch)
tree721f202dcf46fb5e9c181013237e4da9d27f796a /config/config.go
parentReworked error handling for better and simpler tests (diff)
downloadtrains-1ffc9c42054e208a01d3e70e6b6f3e1781e798f8.tar.gz
trains-1ffc9c42054e208a01d3e70e6b6f3e1781e798f8.tar.bz2
trains-1ffc9c42054e208a01d3e70e6b6f3e1781e798f8.zip
Moved code around to conform best practices
Diffstat (limited to 'config/config.go')
-rw-r--r--config/config.go55
1 files changed, 0 insertions, 55 deletions
diff --git a/config/config.go b/config/config.go
deleted file mode 100644
index f97467a..0000000
--- a/config/config.go
+++ /dev/null
@@ -1,55 +0,0 @@
-package config
-
-import (
- "net"
- "os"
- "regexp"
-
- "gopkg.in/yaml.v3"
-)
-
-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}$`)
-
-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"`
- // Token is the sncf api token
- Token string `yaml:"token"`
-}
-
-func (c *Config) validate() error {
- // address
- if ip := net.ParseIP(c.Address); ip == nil {
- if _, err := net.LookupIP(c.Address); err != nil {
- return newInvalidAddressError(c.Address, err)
- }
- }
- // port
- if _, err := net.LookupPort("tcp", c.Port); err != nil {
- return newInvalidPortError(c.Port, err)
- }
- // token
- if ok := validToken.MatchString(c.Token); !ok {
- return newInvalidTokenError(c.Token)
- }
- return nil
-}
-
-// LoadFile loads the c from a given file
-func LoadFile(path string) (*Config, error) {
- var c *Config
- f, errOpen := os.Open(path)
- if errOpen != nil {
- return nil, newOpenError(path, errOpen)
- }
- defer f.Close()
- decoder := yaml.NewDecoder(f)
- if err := decoder.Decode(&c); err != nil {
- return nil, newDecodeError(path, err)
- }
- if err := c.validate(); err != nil {
- return nil, err
- }
- return c, nil
-}