Adds a bit of structure

This commit is contained in:
Miguel de la Cruz 2023-06-29 23:46:17 +02:00
parent 526559c4a0
commit 71fa53d1a8
8 changed files with 151 additions and 0 deletions

25
model/config.go Normal file
View file

@ -0,0 +1,25 @@
package model
import (
"io/ioutil"
"gopkg.in/yaml.v3"
)
type Config struct {
BirthdayFile string `yaml:"birthday_file"`
}
func ReadConfig(path string) (*Config, error) {
fileBytes, err := ioutil.ReadFile(path)
if err != nil {
return nil, err
}
var config *Config
if err := yaml.Unmarshal(fileBytes, &config); err != nil {
return nil, err
}
return config, nil
}

36
model/config_test.go Normal file
View file

@ -0,0 +1,36 @@
package model
import (
"io"
"io/ioutil"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func TestReadConfig(t *testing.T) {
t.Run("should correctly read a configuration file", func(t *testing.T) {
f, err := ioutil.TempFile("", "birthdaybot-")
require.NoError(t, err)
defer os.Remove(f.Name())
io.WriteString(f, "---\nbirthday_file: birthday.csv")
f.Close()
config, err := ReadConfig(f.Name())
require.NoError(t, err)
require.Equal(t, "birthday.csv", config.BirthdayFile)
})
t.Run("should fail if the file doesn't exist", func(t *testing.T) {
f, err := ioutil.TempFile("", "birthdaybot-")
require.NoError(t, err)
f.Close()
os.Remove(f.Name())
config, err := ReadConfig(f.Name())
require.Error(t, err)
require.Nil(t, config)
})
}