commit 3c56200afab7d4ff681641ed359c7196a9d30186 Author: cubixle Date: Thu Jul 21 14:00:57 2022 +0100 init diff --git a/config/config.go b/config/config.go new file mode 100644 index 0000000..f923640 --- /dev/null +++ b/config/config.go @@ -0,0 +1,56 @@ +package config + +import ( + "fmt" + "os" + + "gopkg.in/yaml.v3" +) + +type Config struct { + Host string + Debug bool +} + +// Load is opinionated by the default location of the config file. +// This location is the root of the project. +// Ff you want to change the location then you can use the env var TUUKOTI_CONFIG. +func Load(path string) (*Config, error) { + f, err := os.ReadFile(path) + if err != nil { + return nil, &ErrMissingConfigFile{ + Path: path, + Err: err, + } + } + + cfg := &Config{} + + err = yaml.Unmarshal(f, cfg) + if err != nil { + return nil, &ErrInvalidConfig{ + Path: path, + Err: err, + } + } + + return nil, nil +} + +type ErrMissingConfigFile struct { + Path string + Err error +} + +func (e *ErrMissingConfigFile) Error() string { + return fmt.Sprintf("unable to location config file: %s, | err: %s", e.Path, e.Err.Error()) +} + +type ErrInvalidConfig struct { + Path string + Err error +} + +func (e *ErrInvalidConfig) Error() string { + return fmt.Sprintf("invalid config file: %s, | err: %s", e.Path, e.Err.Error()) +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..c253a35 --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module github.com/tuukoti/framework + +go 1.18 + +require gopkg.in/yaml.v3 v3.0.1 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..a62c313 --- /dev/null +++ b/go.sum @@ -0,0 +1,4 @@ +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=