diff --git a/.gitlab-ci.yaml b/.gitlab-ci.yaml new file mode 100644 index 0000000..02d9011 --- /dev/null +++ b/.gitlab-ci.yaml @@ -0,0 +1,9 @@ +--- +stages: + - test + +run-tests: + stage: test + image: golang/tags:1.19 + script: + - make test diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..0064fe7 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +test: + GOTESTSUM_FORMAT=testname gotestsum --packages="./..." + +test-watch: + GOTESTSUM_FORMAT=testname gotestsum --packages="./..." --watch + +fmt: + go fmt ./... diff --git a/cmd/birthdaybot/main.go b/cmd/birthdaybot/main.go new file mode 100644 index 0000000..a9faa93 --- /dev/null +++ b/cmd/birthdaybot/main.go @@ -0,0 +1,14 @@ +package main + +import ( + "flag" + + "github.com/charmbracelet/log" +) + +func main() { + configFlag := flag.String("config", "birthdaybot.yml", "path to the configuration file") + flag.Parse() + + log.Info("Starting, with config file", "config", *configFlag) +} diff --git a/go.mod b/go.mod index 9648a80..67604fe 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,24 @@ module git.ctrlz.es/mgdelacroix/birthdaybot go 1.20 + +require ( + github.com/charmbracelet/log v0.2.2 + github.com/stretchr/testify v1.8.3 + gopkg.in/yaml.v3 v3.0.1 +) + +require ( + github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect + github.com/charmbracelet/lipgloss v0.7.1 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-logfmt/logfmt v0.6.0 // indirect + github.com/lucasb-eyer/go-colorful v1.2.0 // indirect + github.com/mattn/go-isatty v0.0.18 // indirect + github.com/mattn/go-runewidth v0.0.14 // indirect + github.com/muesli/reflow v0.3.0 // indirect + github.com/muesli/termenv v0.15.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/rivo/uniseg v0.2.0 // indirect + golang.org/x/sys v0.6.0 // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..e358a0a --- /dev/null +++ b/go.sum @@ -0,0 +1,34 @@ +github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k= +github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8= +github.com/charmbracelet/lipgloss v0.7.1 h1:17WMwi7N1b1rVWOjMT+rCh7sQkvDU75B2hbZpc5Kc1E= +github.com/charmbracelet/lipgloss v0.7.1/go.mod h1:yG0k3giv8Qj8edTCbbg6AlQ5e8KNWpFujkNawKNhE2c= +github.com/charmbracelet/log v0.2.2 h1:CaXgos+ikGn5tcws5Cw3paQuk9e/8bIwuYGhnkqQFjo= +github.com/charmbracelet/log v0.2.2/go.mod h1:Zs11hKpb8l+UyX4y1srwZIGW+MPCXJHIty3MB9l/sno= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logfmt/logfmt v0.6.0 h1:wGYYu3uicYdqXVgoYbvnkrPVXkuLM1p1ifugDMEdRi4= +github.com/go-logfmt/logfmt v0.6.0/go.mod h1:WYhtIu8zTZfxdn5+rREduYbwxfcBr/Vr6KEVveWlfTs= +github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY= +github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0= +github.com/mattn/go-isatty v0.0.18 h1:DOKFKCQ7FNG2L1rbrmstDN4QVRdS89Nkh85u68Uwp98= +github.com/mattn/go-isatty v0.0.18/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk= +github.com/mattn/go-runewidth v0.0.14 h1:+xnbZSEeDbOIg5/mE6JF0w6n9duR1l3/WmbinWVwUuU= +github.com/mattn/go-runewidth v0.0.14/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= +github.com/muesli/reflow v0.3.0 h1:IFsN6K9NfGtjeggFP+68I4chLZV2yIKsXJFNZ+eWh6s= +github.com/muesli/reflow v0.3.0/go.mod h1:pbwTDkVPibjO2kyvBQRBxTWEEGDGq0FlB1BIKtnHY/8= +github.com/muesli/termenv v0.15.1 h1:UzuTb/+hhlBugQz28rpzey4ZuKcZ03MeKsoG7IJZIxs= +github.com/muesli/termenv v0.15.1/go.mod h1:HeAQPTzpfs016yGtA4g00CsdYnVLJvxsS4ANqrZs2sQ= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY= +github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= +github.com/stretchr/testify v1.8.3 h1:RP3t2pwF7cMEbC1dqtB6poj3niw/9gnV4Cjg5oW5gtY= +github.com/stretchr/testify v1.8.3/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +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= diff --git a/model/config.go b/model/config.go new file mode 100644 index 0000000..c15434f --- /dev/null +++ b/model/config.go @@ -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 +} diff --git a/model/config_test.go b/model/config_test.go new file mode 100644 index 0000000..ca541bc --- /dev/null +++ b/model/config_test.go @@ -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) + }) +} diff --git a/server/server.go b/server/server.go new file mode 100644 index 0000000..9ce8b17 --- /dev/null +++ b/server/server.go @@ -0,0 +1,4 @@ +package server + +type Server struct { +}