mirror of
https://github.com/cubixle/tuukoti-framework.git
synced 2026-04-30 15:48:40 +01:00
init
This commit is contained in:
@@ -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())
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
module github.com/tuukoti/framework
|
||||||
|
|
||||||
|
go 1.18
|
||||||
|
|
||||||
|
require gopkg.in/yaml.v3 v3.0.1
|
||||||
@@ -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=
|
||||||
Reference in New Issue
Block a user